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繪圖。