一、前言
Dubbo 作為SOA服務(wù)化治理方案的核心框架,用于提高業(yè)務(wù)邏輯的復(fù)用、整合、集中管理,具有極高的可靠性(HA)和伸縮性,被應(yīng)用于阿里巴巴各成員站點(diǎn),同時(shí)在包括JD、當(dāng)當(dāng)在內(nèi)的眾多互聯(lián)網(wǎng)項(xiàng)目中有著廣泛應(yīng)用。dubbo?通過(guò)高性能 RPC 實(shí)現(xiàn)服務(wù)的輸出和輸入功能,框架基于?Spring Framework 進(jìn)行無(wú)縫集成,使用過(guò)程中基本看不到 Dubbo API的直接調(diào)用,Dubbo服務(wù)支持RMI、Hessian、Dubbo、WebService等眾多通信協(xié)議,同時(shí)提供了對(duì)服務(wù)的監(jiān)控和管理平臺(tái),屬于一套完整的SOA解決方案。
詳細(xì)介紹請(qǐng)參照官方地址: http://alibaba.github.io/dubbo-doc-static/Home-zh.htm ,不再重復(fù)描述,本文主要記錄了詳細(xì)的開(kāi)發(fā)整合步驟。 引用一張官方提供的dubbo架構(gòu)圖:
-
Provider ? ?暴露服務(wù)方稱之為“服務(wù)提供者”。
-
Consumer?調(diào)用遠(yuǎn)程服務(wù)方稱之為“服務(wù)消費(fèi)者”。
-
Registry ? ?服務(wù)注冊(cè)與發(fā)現(xiàn)的中心目錄服務(wù)稱之為“服務(wù)注冊(cè)中心”。
-
Monitor ? ? 統(tǒng)計(jì)服務(wù)的調(diào)用次調(diào)和調(diào)用時(shí)間的日志服務(wù)稱之為“服務(wù)監(jiān)控中心”。
- Container ?服務(wù)運(yùn)行“容器”。
?
二、源碼構(gòu)建
1、dubbo基于maven構(gòu)建,需要先配置maven環(huán)境,當(dāng)然如果你不想構(gòu)建源碼,也可以直接下載對(duì)應(yīng)版本的發(fā)行包。
2、dubbo構(gòu)建需要依賴opensesame組件,先通過(guò)?git 獲取并安裝opensesame,源碼地址:
https: // github.com/alibaba/opensesame
?執(zhí)行maven安裝: mvn install
3、通過(guò)下面?git 地址獲取對(duì)應(yīng)版本的?dubbo 源碼,官方推薦使用 dubbo 2.4.9,本文基于trunk版本構(gòu)建。
https: // github.com/alibaba/dubbo
maven構(gòu)建: mvn clean install?-Dmaven.test.skip ,在漫長(zhǎng)的等待后(過(guò)程需要通過(guò)網(wǎng)絡(luò)下載依賴資源)會(huì)有如下提示,生成的?jar 包位于各自目錄的target文件夾。

[INFO] ------------------------------------------------------------------------ [INFO] Reactor Summary: [INFO] [INFO] dubbo -parent ....................................... SUCCESS [ 1.272 s] [INFO] dubbo -common ....................................... SUCCESS [ 4.572 s] [INFO] dubbo -container .................................... SUCCESS [ 0.024 s] [INFO] dubbo -container-api ................................ SUCCESS [ 1.148 s] [INFO] dubbo -container-spring ............................. SUCCESS [ 0.524 s] [INFO] dubbo -container-jetty .............................. SUCCESS [ 0.420 s] [INFO] dubbo -container-log4j .............................. SUCCESS [ 0.448 s] [INFO] dubbo -container-logback ............................ SUCCESS [ 0.468 s] [INFO] dubbo -remoting ..................................... SUCCESS [ 0.016 s] [INFO] dubbo -remoting-api ................................. SUCCESS [ 1.864 s] [INFO] dubbo -remoting-netty ............................... SUCCESS [ 1.125 s] [INFO] dubbo -remoting-mina ................................ SUCCESS [ 0.696 s] [INFO] dubbo -remoting-grizzly ............................. SUCCESS [ 0.680 s] [INFO] dubbo -remoting-p2p ................................. SUCCESS [ 1.048 s] [INFO] dubbo -remoting-http ................................ SUCCESS [ 0.616 s] [INFO] dubbo -remoting-zookeeper ........................... SUCCESS [ 0.716 s] [INFO] dubbo -rpc .......................................... SUCCESS [ 0.016 s] [INFO] dubbo -rpc-api ...................................... SUCCESS [ 1.608 s] [INFO] dubbo -rpc- default .................................. SUCCESS [ 1.396 s] [INFO] dubbo -rpc-injvm .................................... SUCCESS [ 0.760 s] [INFO] dubbo -rpc-rmi ...................................... SUCCESS [ 0.416 s] [INFO] dubbo -rpc-hessian .................................. SUCCESS [ 0.584 s] [INFO] dubbo -rpc-http ..................................... SUCCESS [ 0.580 s] [INFO] dubbo -rpc-webservice ............................... SUCCESS [ 0.536 s] [INFO] dubbo -cluster ...................................... SUCCESS [ 2.208 s] [INFO] dubbo -registry ..................................... SUCCESS [ 0.020 s] [INFO] dubbo -registry-api ................................. SUCCESS [ 1.241 s] [INFO] dubbo -monitor ...................................... SUCCESS [ 0.016 s] [INFO] dubbo -monitor-api .................................. SUCCESS [ 0.464 s] [INFO] dubbo - filter ....................................... SUCCESS [ 0.016 s] [INFO] dubbo - filter -validation ............................ SUCCESS [ 0.608 s] [INFO] dubbo - filter -cache ................................. SUCCESS [ 0.604 s] [INFO] dubbo -registry- default ............................. SUCCESS [ 0.540 s] [INFO] dubbo -monitor- default .............................. SUCCESS [ 0.588 s] [INFO] dubbo -registry-multicast ........................... SUCCESS [ 0.632 s] [INFO] dubbo -config ....................................... SUCCESS [ 0.016 s] [INFO] dubbo -config-api ................................... SUCCESS [ 1.500 s] [INFO] dubbo -config-spring ................................ SUCCESS [ 1.520 s] [INFO] dubbo -rpc-thrift ................................... SUCCESS [ 1.032 s] [INFO] dubbo -rpc-memcached ................................ SUCCESS [ 0.464 s] [INFO] dubbo -rpc-redis .................................... SUCCESS [ 0.500 s] [INFO] dubbo -registry-zookeeper ........................... SUCCESS [ 0.656 s] [INFO] dubbo -registry-redis ............................... SUCCESS [ 0.640 s] [INFO] dubbo .............................................. SUCCESS [ 1.009 s] [INFO] dubbo -simple ....................................... SUCCESS [ 0.016 s] [INFO] dubbo -registry-simple .............................. SUCCESS [01:43 min] [INFO] dubbo -monitor-simple ............................... SUCCESS [ 10.379 s] [INFO] dubbo -admin ........................................ SUCCESS [04:03 min] [INFO] dubbo -demo ......................................... SUCCESS [ 0.016 s] [INFO] dubbo -demo-api ..................................... SUCCESS [ 0.308 s] [INFO] dubbo -demo-provider ................................ SUCCESS [ 2.700 s] [INFO] dubbo -demo-consumer ................................ SUCCESS [ 2.745 s] [INFO] dubbo -test ......................................... SUCCESS [ 0.012 s] [INFO] dubbo -test-benchmark ............................... SUCCESS [01:01 min] [INFO] dubbo -test-compatibility ........................... SUCCESS [ 0.188 s] [INFO] dubbo -test-integration ............................. SUCCESS [ 0.052 s] [INFO] dubbo -test-examples ................................ SUCCESS [ 1.640 s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 07:42 min [INFO] Finished at: 2015-01-20T14:36:43+08:00 [INFO] Final Memory: 282M / 895M [INFO] ------------------------------------------------------------------------
4、?構(gòu)建過(guò)程中如果出現(xiàn)maven網(wǎng)絡(luò)連接相關(guān)的異常信息,可以嘗試將以下maven mirror 地址添加到 settings.xml 文件。
< mirror > < id > CN </ id > < mirrorOf > central </ mirrorOf > < name > OSChina Central </ name > < url > http://maven.oschina.net/content/groups/public/ </ url > </ mirror > < mirror > < id > kafeitu </ id > < mirrorOf > central </ mirrorOf > < name > Human Readable Name for this Mirror. </ name > < url > http://maven.kafeitu.me/nexus/content/repositories/public </ url > </ mirror > < mirror > < id > lvu.cn </ id > < name > lvu.cn </ name > < url > http://lvu.cn/nexus/content/groups/public </ url > < mirrorOf > * </ mirrorOf > </ mirror >
?
三、Zookeeper部署(Registry 服務(wù)注冊(cè)中心)
D ubbo 缺省配置通過(guò) multicast 注冊(cè)中心廣播實(shí)現(xiàn) Provider 和 Consumer 之間的簡(jiǎn)單遠(yuǎn)程過(guò)程調(diào)用(Simple RPC),不需要通過(guò)?Registry 注冊(cè)中心進(jìn)行注冊(cè)調(diào)度 ,類似于spring rmi remoting調(diào)用,但由于不是Cluster部署,所以作為分布 式RPC框架官方建議使用 Zookeeper 作為Registry注冊(cè)中心服務(wù)器(同樣支持Redis)實(shí)現(xiàn)服務(wù)的注冊(cè)、發(fā)現(xiàn)、路由功能。Dubbo在Zookeeper服務(wù)器端只增加了dubbo數(shù)據(jù)節(jié)點(diǎn)(如下圖),無(wú)需其他任何配置,所以只需安裝或使用現(xiàn)有 Zookeeper 服務(wù)器即可,關(guān)于Zookeeper的安裝部署可以參照之前的博文: http://www.cnblogs.com/lengfo/p/4146797.html ,不再重復(fù)介紹。
Zookeeper同樣支持集群部署,提供最終一致性數(shù)據(jù)服務(wù),本文實(shí)驗(yàn)環(huán)境只使用了一臺(tái)Linux服務(wù)器作為ZK Registry注冊(cè)服務(wù)器,zoo.cfg配置文件如下。
# The number of milliseconds of each tick tickTime =2000 # The number of ticks that the initial # synchronization phase can take initLimit =10 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit =5 # the directory where the snapshot is stored. # do not use /tmp for storage , /tmp here is just # example sakes. dataDir =/home/glf/zookeeperServer/data dataLogDir= /home/glf/zookeeperServer/logs # the port at which the clients will connect clientPort =2181 # the maximum number of client connections. # increase this if you need to handle more clients #maxClientCnxns =60 # # Be sure to read the maintenance section of the # administrator guide before turning on autopurge. # # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance # # The number of snapshots to retain in dataDir #autopurge.snapRetainCount =3 # Purge task interval in hours # Set to "0" to disable auto purge feature #autopurge.purgeInterval =1
?
四、服務(wù)提供方開(kāi)發(fā)(Provider)
1、創(chuàng)建Java測(cè)試項(xiàng)目 DubboDemo?,集成Spring Framework。由于dubbo基于Spring的Schema擴(kuò)展進(jìn)行加載,依賴于低版本spring,所以如果開(kāi)發(fā)項(xiàng)目中使用高版本(spring 3.0及以上)會(huì)出現(xiàn)xml解析沖突,官方QA中也有相關(guān)提及: http://alibaba.github.io/dubbo-doc-static/FAQ-zh.htm ,所以本文只集成了2.5.6版本spring,更多相關(guān)版本問(wèn)題也有待進(jìn)一步研究, Todo 。
2、集成dubbo及相關(guān)依賴包,參照下圖,如果使用dubbo其他特性可能需要依賴更多包,比如jedis。
3、創(chuàng)建測(cè)試服務(wù)接口(IGoodsManager)?

package ibusiness; public interface IGoodsManager { public String LoadGoods(); }
4、創(chuàng)建測(cè)試服務(wù)實(shí)現(xiàn)類(GoodsManager)

package business; import com.alibaba.dubbo.rpc.RpcContext; import ibusiness.IGoodsManager; public class GoodsManager implements IGoodsManager { @Override public String LoadGoods() { return "可口可樂(lè),雀巢咖啡 " + "/n response form provider: " + RpcContext.getContext().getLocalAddress(); } }
5、通過(guò)spring 集成并配置dubbo測(cè)試服務(wù),同時(shí)指定registry的zookeeper服務(wù)器地址。
<? 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 服務(wù)提供者應(yīng)用名稱 --> < dubbo:application name ="demo-dubbo-provider-app" /> <!-- dubbo 注冊(cè)中心 --> < dubbo:registry address ="zookeeper://10.129.221.64:2181" /> <!-- 服務(wù)提供者 端口 --> < dubbo:protocol name ="dubbo" port ="30001" /> <!-- dubbo提供服務(wù) --> < dubbo:service interface ="ibusiness.IGoodsManager" ref ="goodsService" /> <!-- spring bean 對(duì)象 --> < bean id ="goodsService" class ="business.GoodsManager" /> </ beans >
6、編寫控制臺(tái)程序啟動(dòng)spring容器,編譯并打包Provider.jar

package dubboProvider; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class DubboProvider { public static void main(String[] args) { try { // 初始化Spring ApplicationContext ctx = new ClassPathXmlApplicationContext("dubboProvider\\dubboProvider.xml" ); System.out.println( "dubbo provider is running..." ); System.in.read(); } catch (Exception ex) { ex.printStackTrace(); } } }
?
五、服務(wù)消費(fèi)方開(kāi)發(fā)(Consumer)
1、通過(guò)spring 配置指定registry的zookeeper地址,實(shí)現(xiàn)對(duì)dubbo遠(yuǎn)程服務(wù)的調(diào)用
<? 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 ="demo-dubbo-consumer-app" /> < dubbo:registry address ="zookeeper://10.129.221.64:2181" /> <!-- 和本地bean一樣實(shí)現(xiàn)服務(wù) --> < dubbo:reference id ="goodsService" interface ="ibusiness.IGoodsManager" /> </ beans >
2、編寫調(diào)用測(cè)試客戶端代碼,從容器中獲取遠(yuǎn)程bean并調(diào)用。

package dubboConsumer; import ibusiness.IGoodsManager; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class DubboConsumer { public static void main(String[] args) { // 初始化Spring ApplicationContext ctx = new ClassPathXmlApplicationContext("dubboConsumer\\dubboConsumer.xml" ); IGoodsManager goodsManager = (IGoodsManager) ctx.getBean("goodsService"); // 獲取遠(yuǎn)程服務(wù)代理 String goodsStr = goodsManager.LoadGoods() ; // 執(zhí)行遠(yuǎn)程方法 System.out.println(goodsStr); } }
3、 分別啟動(dòng)?provider、consumer 的測(cè)試程序發(fā)布和調(diào)用服務(wù),即簡(jiǎn)單實(shí)現(xiàn)了基于 zookeeper registry 的 dubbo 遠(yuǎn)程過(guò)程調(diào)用。
?
六、More about dubbo
以上簡(jiǎn)單的展示dubbo的RPC服務(wù)能力,更多關(guān)于dubbo的 集群管理、監(jiān)控、負(fù)載均衡、高可用性、可擴(kuò)展、伸縮性 等相關(guān)內(nèi)容請(qǐng)期待后續(xù)博文。
?
七、向開(kāi)源工作者和組織致敬, @dubbo?team ,感謝對(duì)開(kāi)源事業(yè)作出的任何貢獻(xiàn)
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
