一、前言
Dubbo 作為SOA服務化治理方案的核心框架,用於提高業務邏輯的復用、整合、集中管理,具有極高的可靠性(HA)和伸縮性,被應用於阿里巴巴各成員站點,同時在包括JD、當當在內的眾多互聯網項目中有着廣泛應用。dubbo 通過高性能 RPC 實現服務的輸出和輸入功能,框架基於 Spring Framework 進行無縫集成,使用過程中基本看不到 Dubbo API的直接調用,Dubbo服務支持RMI、Hessian、Dubbo、WebService等眾多通信協議,同時提供了對服務的監控和管理平台,屬於一套完整的SOA解決方案。
詳細介紹請參照官方地址:http://alibaba.github.io/dubbo-doc-static/Home-zh.htm,不再重復描述,本文主要記錄了詳細的開發整合步驟。引用一張官方提供的dubbo架構圖:
-
Provider 暴露服務方稱之為“服務提供者”。
-
Consumer 調用遠程服務方稱之為“服務消費者”。
-
Registry 服務注冊與發現的中心目錄服務稱之為“服務注冊中心”。
-
Monitor 統計服務的調用次調和調用時間的日志服務稱之為“服務監控中心”。
- Container 服務運行“容器”。
二、源碼構建
1、dubbo基於maven構建,需要先配置maven環境,當然如果你不想構建源碼,也可以直接下載對應版本的發行包。
2、dubbo構建需要依賴opensesame組件,先通過 git 獲取並安裝opensesame,源碼地址:
https://github.com/alibaba/opensesame
執行maven安裝:mvn install
3、通過下面 git 地址獲取對應版本的 dubbo 源碼,官方推薦使用 dubbo 2.4.9,本文基於trunk版本構建。
https://github.com/alibaba/dubbo
maven構建:mvn clean install -Dmaven.test.skip,在漫長的等待后(過程需要通過網絡下載依賴資源)會有如下提示,生成的 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、 構建過程中如果出現maven網絡連接相關的異常信息,可以嘗試將以下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 服務注冊中心)
Dubbo 缺省配置通過 multicast 注冊中心廣播實現 Provider 和 Consumer 之間的簡單遠程過程調用(Simple RPC),不需要通過 Registry 注冊中心進行注冊調度,類似於spring rmi remoting調用,但由於不是Cluster部署,所以作為分布式RPC框架官方建議使用 Zookeeper 作為Registry注冊中心服務器(同樣支持Redis)實現服務的注冊、發現、路由功能。Dubbo在Zookeeper服務器端只增加了dubbo數據節點(如下圖),無需其他任何配置,所以只需安裝或使用現有 Zookeeper 服務器即可,關於Zookeeper的安裝部署可以參照之前的博文:http://www.cnblogs.com/lengfo/p/4146797.html,不再重復介紹。
Zookeeper同樣支持集群部署,提供最終一致性數據服務,本文實驗環境只使用了一台Linux服務器作為ZK Registry注冊服務器,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
四、服務提供方開發(Provider)
1、創建Java測試項目 DubboDemo ,集成Spring Framework。由於dubbo基於Spring的Schema擴展進行加載,依賴於低版本spring,所以如果開發項目中使用高版本(spring 3.0及以上)會出現xml解析沖突,官方QA中也有相關提及:http://alibaba.github.io/dubbo-doc-static/FAQ-zh.htm,所以本文只集成了2.5.6版本spring,更多相關版本問題也有待進一步研究,Todo。
2、集成dubbo及相關依賴包,參照下圖,如果使用dubbo其他特性可能需要依賴更多包,比如jedis。
3、創建測試服務接口(IGoodsManager)

package ibusiness; public interface IGoodsManager { public String LoadGoods(); }
4、創建測試服務實現類(GoodsManager)

package business; import com.alibaba.dubbo.rpc.RpcContext; import ibusiness.IGoodsManager; public class GoodsManager implements IGoodsManager { @Override public String LoadGoods() { return "可口可樂,雀巢咖啡 " + "/n response form provider: " + RpcContext.getContext().getLocalAddress(); } }
5、通過spring 集成並配置dubbo測試服務,同時指定registry的zookeeper服務器地址。
<?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 服務提供者應用名稱 --> <dubbo:application name="demo-dubbo-provider-app" /> <!--dubbo 注冊中心--> <dubbo:registry address="zookeeper://10.129.221.64:2181" /> <!--服務提供者 端口--> <dubbo:protocol name="dubbo" port="30001" /> <!--dubbo提供服務--> <dubbo:service interface="ibusiness.IGoodsManager" ref="goodsService" /> <!--spring bean 對象--> <bean id="goodsService" class="business.GoodsManager" /> </beans>
6、編寫控制台程序啟動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(); } } }
五、服務消費方開發(Consumer)
1、通過spring 配置指定registry的zookeeper地址,實現對dubbo遠程服務的調用
<?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一樣實現服務 --> <dubbo:reference id="goodsService" interface="ibusiness.IGoodsManager" /> </beans>
2、編寫調用測試客戶端代碼,從容器中獲取遠程bean並調用。

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"); // 獲取遠程服務代理 String goodsStr = goodsManager.LoadGoods() ; // 執行遠程方法 System.out.println(goodsStr); } }
3、分別啟動 provider、consumer 的測試程序發布和調用服務,即簡單實現了基於 zookeeper registry 的 dubbo 遠程過程調用。
六、More about dubbo
以上簡單的展示dubbo的RPC服務能力,更多關於dubbo的集群管理、監控、負載均衡、高可用性、可擴展、伸縮性等相關內容請期待后續博文。