基於開源Dubbo分佈式RPC服務框架的部署整合

1、前言

  Dubbo 做爲SOA服務化治理方案的核心框架,用於提升業務邏輯的複用、整合、集中管理,具備極高的可靠性(HA)和伸縮性,被應用於阿里巴巴各成員站點,同時在包括JD、噹噹在內的衆多互聯網項目中有着普遍應用。dubbo 經過高性能 RPC 實現服務的輸出和輸入功能,框架基於 Spring Framework 進行無縫集成,使用過程當中基本看不到 Dubbo API的直接調用,Dubbo服務支持RMI、Hessian、Dubbo、WebService等衆多通訊協議,同時提供了對服務的監控和管理平臺,屬於一套完整的SOA解決方案。html

  詳細介紹請參照官方地址:http://alibaba.github.io/dubbo-doc-static/Home-zh.htm,再也不重複描述,本文主要記錄了詳細的開發整合步驟。引用一張官方提供的dubbo架構圖:git

  

  • Provider    暴露服務方稱之爲「服務提供者」。github

  • Consumer 調用遠程服務方稱之爲「服務消費者」。web

  • Registry    服務註冊與發現的中心目錄服務稱之爲「服務註冊中心」。redis

  • Monitor     統計服務的調用次調和調用時間的日誌服務稱之爲「服務監控中心」。spring

  • Container  服務運行「容器」。

 

2、源碼構建

  一、dubbo基於maven構建,須要先配置maven環境,固然若是你不想構建源碼,也能夠直接下載對應版本的發行包。apache

  二、dubbo構建須要依賴opensesame組件,先經過 git 獲取並安裝opensesame,源碼地址:api

https://github.com/alibaba/opensesame

     執行maven安裝:mvn install服務器

  三、經過下面 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] ------------------------------------------------------------------------
View Code

  四、 構建過程當中若是出現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>

 

3、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

 

4、服務提供方開發(Provider)

  一、建立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

  二、集成dubbo及相關依賴包,參照下圖,若是使用dubbo其餘特性可能須要依賴更多包,好比jedis。

  

  三、建立測試服務接口(IGoodsManager) 

package ibusiness;

public interface IGoodsManager {
    public String LoadGoods();
}
View Code

  四、建立測試服務實現類(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();
    }
}
View Code

  五、經過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>

  六、編寫控制檯程序啓動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();
        }
    }
}
View Code

 

5、服務消費方開發(Consumer)

  一、經過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>

  二、編寫調用測試客戶端代碼,從容器中獲取遠程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);

    } 
}
View Code

  三、分別啓動 provider、consumer 的測試程序發佈和調用服務,即簡單實現了基於 zookeeper registry 的 dubbo 遠程過程調用。

  

 

6、More about dubbo

  以上簡單的展現dubbo的RPC服務能力,更多關於dubbo的集羣管理、監控、負載均衡、高可用性、可擴展、伸縮性等相關內容請期待後續博文。

 

7、向開源工做者和組織致敬,@dubbo team,感謝對開源事業做出的任何貢獻

相關文章
相關標籤/搜索