[1]Dubbo是[2]阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输
出和输入功能,可以和 [3] Spring 框架无缝集成。主要核心部件
Remoting: 网络通信框架,实现了 sync-over-async 和 request-response 消息机制. RPC: 一个 远程过程调用 的抽象,支持 负载均衡 、 容灾 和 集群 功能 Registry: 服务目录框架用于服务的注册和服务事件发布和订阅编辑本段工作原理
Provider 暴露服务方称之为“服务提供者”。 Consumer 调用 远程服务 方称之为“服务消费者”。 Registry 服务注册与发现的中心目录服务称之为“服务注册中心”。 Monitor 统计服务的调用次调和调用时间的日志服务称之为“服务监控中心”。 (1) 连通性: 注册中心负责服务地址的注册与查找,相当于 目录服务 ,服务提供者和消费者只在启动时与注册中心交互,注册中心不转发请求,压力较小 监控中心负责统计各服务调用次数,调用时间等,统计先在内存汇总后每分钟一次发送到监控中心服务器,并以 报表 展示 服务提供者向注册中心注册其提供的服务,并汇报调用时间到监控中心,此时间不包含网络开销 服务消费者向注册中心获取服务提供者地址列表,并根据负载算法直接调用提供者,同时汇报调用时间到监控中心,此时间包含网络开销 注册中心,服务提供者,服务消费者三者之间均为长连接,监控中心除外 注册中心通过 长连接 感知服务提供者的存在,服务提供者宕机,注册中心将立即推送事件通知消费者 注册中心和监控中心全部宕机,不影响已运行的提供者和消费者,消费者在 本地缓存 了提供者列表 注册中心和监控中心都是可选的,服务消费者可以直连服务提供者 (2) 健状性: 监控中心宕掉不影响使用,只是丢失部分 采样数据 数据库宕掉后,注册中心仍能通过 缓存 提供服务列表查询,但不能注册新服务 注册中心对等 集群 ,任意一台宕掉后,将自动切换到另一台 注册中心全部宕掉后,服务提供者和服务消费者仍能通过本地缓存通讯 服务提供者无状态,任意一台宕掉后,不影响使用 服务提供者全部宕掉后,服务消费者应用将无法使用,并无限次重连等待服务提供者恢复 (3) 伸缩性: 注册中心为对等集群,可动态增加机器部署实例,所有客户端将自动发现新的注册中心 服务提供者无状态,可动态增加机器部署实例,注册中心将推送新的服务提供者信息给消费者编辑本段例子
服务端
定义一个Service Interface:( HelloService.java )package com.alibaba.hello.api; public interface HelloService { String sayHello(String name); } |
package com.alibaba.hello.impl; import com.alibaba.hello.api.HelloService; public class HelloServiceImpl implements HelloService { public String sayHello(String name) { return "Hello " + name; } } |
<?xml version="1.0" encoding="UTF-8"?> <beans ......> <!-- Application name --> <dubbo:application name="hello-world-app" /> <!-- registry address, used for service to register itself --> <dubbo:registry address="multicast://224.5.6.7:1234" /> <!-- expose this service through dubbo protocol, through port 2 0 8 8 0 --> <dubbo:protocol name="dubbo" port="2 0 8 8 0" /> <!-- which service interface do we expose? --> <dubbo:service interface="com.alibaba.hello.api.HelloService" ref="helloService" /> <!-- designate implementation --> <bean id="helloService" class="com.alibaba.hello.impl.HelloServiceImpl" /> </beans> |
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Provider { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"provider.xml"}); //启动成功,监听端口为2 0 8 8 0 System.in.read(); // 按任意键退出 } } |
客户端
Spring配置文件:( consumer.xml ) <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=......> <!-- consumer application name --> <dubbo:application name="consumer-of-helloworld-app" /> <!-- registry address, used for consumer to discover services --> <dubbo:registry address="multicast://224.5.6.7:1234" /> <!-- which service to consume? --> <dubbo:reference id="helloService" interface="com.alibaba.hello.api.HelloService" /> </beans> |
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.alibaba.hello.api.HelloService; public class Consumer { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"consumer.xml"}); HelloService helloService = (HelloService)context.getBean("helloService"); // get service invocation proxy String hello = helloService.sayHello("world"); // do invoke! System.out.println( hello ); // cool, how are you~ } } |