dubbo 原理和入门实践

本文深入解析Dubbo框架,涵盖远程服务调用、集群容错、自动发现等核心特性,介绍Dubbo架构及其实现步骤,演示如何通过Zookeeper进行服务注册与发现。

一。dubbo:

远程服务调用的分布式框架

    其核心部分包含:
1. 远程通讯: 提供对多种基于长连接的NIO框架抽象封装,包括多种线程模型,序列化,以及“请求-响应”模式的信息交换方式。
2. 集群容错: 提供基于接口方法的透明远程过程调用,包括多协议支持,以及软负载均衡,失败容错,地址路由,动态配置等集群支持。
3. 自动发现: 基于注册中心目录服务,使服务消费方能动态的查找服务提供方,使地址透明,使服务提供方可以平滑增加或减少机器

二。dubbo 框架架构图

2.1 流程分析

0 服务容器负责启动,加载,运行服务提供者。

1. 服务提供者在启动时,向注册中心注册自己提供的服务。

2. 服务消费者在启动时,向注册中心订阅自己所需的服务。

3. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。

4. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。

5. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

三。相关代码和demo 实现步骤

 

3.1  官网代码demo说明

 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-provider.xml"});
        context.start();
        System.in.read(); // press any key to exit

  程序启动读取对应的resouce 下的xml 文件,

 

   声明一个接口和服务,


public interface DemoService {

    String sayHello(String name);

}

 声明接口的实现类:

 

public class DemoServiceImpl implements DemoService {

    @Override
    public String sayHello(String name) {
        System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
        return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();
    }

}

  接口中的方法主要是通过字符串从服务器获取相应的字符串。

这里是一个demo Service 实现,需要在dobbu 中配置相应的配置文件,

 

 <!-- service implementation, as same as regular local bean -->
    <bean id="demoService" class="org.apache.dubbo.demo.provider.DemoServiceImpl"/>

    <!-- declare the service interface to be exported -->
    <dubbo:service interface="org.apache.dubbo.demo.DemoService" ref="demoService"/>

 <dubbo:registry address="multicast://224.5.6.7:1234"/>

    <!-- use dubbo protocol to export service on port 20880 -->
    <dubbo:protocol name="dubbo" port="20880"/>

  通过注册dubbo 协议服务和多播实现。

 

在服务器端中实现:

 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-consumer.xml"});
        context.start();
        DemoService demoService = (DemoService) context.getBean("demoService"); // get remote service proxy

在加载类路径下的*spring.xml 文件的

通过对注册的multicast 实现引用接口下的服务demoServer. 调用接口中的发布的service 的方法。

  DemoService demoService = (DemoService) context.getBean("demoService"); // get remote service proxy

  String hello = demoService.sayHello("world"); // call remote method

 

调用远程代理和远程方法。

3.2 注册到zookeeper 的实现

下载api代码,并install 供privider和customer 使用。

启动prider 代码,修改zk 地址。

启动customer 代码:

结果如下:

 

 

 

 

四。参考资料:

官方文档:

https://github.com/apache/incubator-dubbo/tree/2.5.x

管理控制台:

https://www.cnblogs.com/xhj123/p/8975840.html

https://www.cnblogs.com/leemz-coding/p/7113530.html

https://blog.youkuaiyun.com/houshaolin/article/details/76408399

 

五.附件代码:

入门demo:https://github.com/apache/incubator-dubbo/tree/2.5.x/dubbo-demo

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迅捷的软件产品制作专家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值