Dubbo[]是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。spring
其核心部分包含:安全
Provider:服务提供方。服务器
定义服务端、消费端公共接口:架构
package com.alibaba.dubbo.demo; public interface DemoService { String sayHello(String name); }
实现接口:app
package com.alibaba.dubbo.demo.provider; import com.alibaba.dubbo.demo.DemoService; public class DemoServiceImpl implements DemoService { public String sayHello(String name) { return "Hello " + name; } }
配置Provider端:负载均衡
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="hello-world-app" /> <!-- 使用zookeeper注册中心(单机)--> <dubbo:registry address="zookeeper://10.100.51.146:2181" /> <!-- 配置监控中心 --> <dubbo:monitor protocol="registry"/> <!-- 各类的协议以及暴露的服务端口 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明须要暴露的服务接口,并指定引用的协议(若是全局只配置了一种协议,那么这里无需配置protocol属性便可直接引用这个协议) --> <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" protocol="dubbo"/> <!-- 和本地bean同样实现服务 --> <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl" /> </beans>
因为Demo中采用是dubbo协议,dubbo协议默认使用hessian2的序列化方式,并采用Netty做为远程通信服务器。框架
完成Provider端配置,启动Provider。dom
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Provider { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"provider.xml"}); context.start(); System.in.read(); // 按任意键退出 } }
Provider端启动的整个流程: 分布式
Consumer:服务消费方。ide
配置Consumer端:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方同样 --> <dubbo:application name="consumer-of-helloworld-app" /> <!-- 使用zookeeper注册中心(单机)--> <dubbo:registry address="zookeeper://10.100.51.146:2181" /> <!-- 配置监控中心 --> <dubbo:monitor protocol="registry"/> <!-- 生成远程服务代理,能够和本地bean同样使用demoService --> <dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService" /> </beans>
完成Consumer端配置,启动Consumer。
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.alibaba.dubbo.demo.DemoService; public class Consumer { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"consumer.xml"}); context.start(); } }
Consumer端启动的整个流程:
Provider&Consumer:服务提供方与消费方的交互过程,完成远程方法调用的流程细则。
Provider和Consumer已经前后完成启动,而且Consumer已经打通了到Provider的Socket链接。
远程调用的代码片断:
// 获取远程服务代理 DemoService demoService = (DemoService)context.getBean("demoService"); // 执行远程方法 String hello = demoService.sayHello("world"); // 显示调用结果 System.out.println( hello );
执行程序,完成远程调用。
远程调用流程:
Registry&Router:注册中心与路由规则。
用户经过管理平台将路由规则添加到zookeeper中,zookeeper信息一旦发生改变,会通知Consumer接受数据。Consumer将接收到的数据进行处理,分类成远程主机信息和路由规则信息,并根据路由规则信息过滤不合格的远程主机,并交给Cluster进行调度。
Cluster:集群控制中心。
集群控制中心封装了集群调度访问远程主机的细则。从外面看来,Cluster让用户觉得只有一个invoker去调用目标主机的程序,其实Cluster采起的方式是从多个invoker中选择一个,完成远程调用,返回执行结果。
Monitor:监控平台。 本次不做为重点,简要介绍。 Monitor的主要做用是监控Provider和Consumer之间的调用次数和调用时间等信息。 基本思路是当Provider和Consumer两端都配置了监控中心信息以后,每次调用都会记录信息,并传递给监控中心,监控中心记录本地,并经过JFreeChart绘图。