dubbo+zookeeper例子


0.原理

 
Alibaba有好幾個分布式框架,主要有:進行遠程調用(類似於RMI的這種遠程調用)的(dubbo、hsf),jms消息服務(napoli、notify),KV數據庫(tair)等。這個框架/工具/產品在實現的時候,都考慮到了容災,擴展,負載均衡,於是出現一個配置中心(ConfigServer)的東西來解決這些問題。
基本原理如圖:

 

在我們的系統中,經常會有一些跨系統的調用,如在A系統中要調用B系統的一個服務,我們可能會使用RMI直接來進行,B系統發布一個RMI接口服務,然后A系統就來通過RMI調用這個接口,為了解決容災,擴展,負載均衡的問題,我們可能會想很多辦法,alibaba的這個辦法感覺不錯。
 
本文只說dubbo,原理如下:
  • ConfigServer
配置中心,和每個Server/Client之間會作一個實時的心跳檢測(因為它們都是建立的Socket長連接),比如幾秒鍾檢測一次。收集每個Server提供的服務的信息,每個Client的信息,整理出一個服務列表,如:
serviceName serverAddressList clientAddressList
UserService 192.168.0.1,192.168.0.2,192.168.0.3,192.168.0.4 172.16.0.1,172.16.0.2
ProductService 192.168.0.3,192.168.0.4,192.168.0.5,192.168.0.6 172.16.0.2,172.16.0.3
OrderService 192.168.0.10,192.168.0.12,192.168.0.5,192.168.0.6 172.16.0.3,172.16.0.4
當某個Server不可用,那么就更新受影響的服務對應的 serverAddressList,即把這個Server從 serverAddressList中踢出去(從地址列表中刪除),同時將推送 serverAddressList給這些受影響的服務的clientAddressList里面的所有Client。如:192.168.0.3掛了,那么UserService和ProductService的 serverAddressList都要把192.168.0.3刪除掉,同時把新的列表告訴對應的Client 172.16.0.1,172.16.0.2,172.16.0.3;
當某個Client掛了,那么更新受影響的服務對應的 clientAddressList
ConfigServer根據服務列表,就能提供一個web管理界面,來查看管理服務的提供者和使用者。
新加一個Server時,由於它會主動與ConfigServer取得聯系,而ConfigServer又會將這個信息主動發送給Client,所以新加一個Server時,只需要啟動Server,然后幾秒鍾內,Client就會使用上它提供的服務
  • Client
調用服務的機器,每個Client啟動時,主動與ConfigServer建立Socket長連接,並將自己的IP等相應信息發送給ConfigServer。
Client在使用服務的時候根據服務名稱去ConfigServer中獲取服務提供者信息(這樣ConfigServer就知道某個服務是當前哪幾個Client在使用),Client拿到這些服務提供者信息后,與它們都建立連接,后面就可以直接調用服務了,當有多個服務提供者的時候,Client根據一定的規則來進行負載均衡,如輪詢,隨機,按權重等。
一旦Client使用的服務它對應的服務提供者有變化(服務提供者有新增,刪除的情況),ConfigServer就會把最新的服務提供者列表推送給Client,Client就會依據最新的服務提供者列表重新建立連接,新增的提供者建立連接,刪除的提供者丟棄連接
  • Server
真正提供服務的機器,每個Server啟動時,主動與ConfigServer建立Scoket長連接,並將自己的IP,提供的服務名稱,端口等信息直接發送給ConfigServer,ConfigServer就會收集到每個Server提供的服務的信息。
 
優點:
1,只要在Client和Server啟動的時候,ConfigServer是好的,服務就可調用了,如果后面ConfigServer掛了,那只影響ConfigServer掛了以后服務提供者有變化,而Client還無法感知這一變化。
2,Client每次調用服務是不經過ConfigServer的,Client只是與它建立聯系,從它那里獲取提供服務者列表而已
3,調用服務-負載均衡:Client調用服務時,可以根據規則在多個服務提供者之間輪流調用服務。
4,服務提供者-容災:某一個Server掛了,Client依然是可以正確的調用服務的,當前提是這個服務有至少2個服務提供者,Client能很快的感知到服務提供者的變化,並作出相應反應。
5,服務提供者-擴展:添加一個服務提供者很容易,而且Client會很快的感知到它的存在並使用它。

1.開發軟件、資料

  • jdk1.7.0_79 ,安裝並配置好java開發環境
  • zookeeper-3.4.5   下載地址:http://download.csdn.net/detail/adam_zs/9470314
  • Tomcat 7.0 配置入eclipse或者myeclipse都可以
  • dubbo-admin-2.5.3.war  下載地址:http://download.csdn.net/detail/adam_zs/9470323
  • apache-maven-3.2.5  配置入eclipse或者myeclipse都可以
  • dubbo官方文檔 http://dubbo.io/Home-zh.htm
  • Dubbo安裝 下載地址:https://github.com/alibaba/dubbo/releases  pom.xml:http://files.cnblogs.com/files/belen/pom.xml

2.關鍵步驟

zookeeper安裝部署(

ZooKeeper是一個分布式的,開放源碼的分布式應用程序協調服務,是Google的Chubby一個開源的實現,是Hadoop和Hbase的重要組件。實例中,zookeeper將作為dubbo服務的注冊中心。同時負責集群調度。

為什么要用zookeeper?

Zookeeper可以提供配置管理、命名服務、分布式算法、集群管理功能。具體說明參看如下文章:

http://zhidao.baidu.com/question/919262980452730419.html?fr=iks&word=zookeeper+dubbo+%B9%D8%CF%B5&ie=gbk

Zookeeper部署

1、dubbo依賴於Zookeeper,實現任務的分布式配置及各服務間的交互通信,Zookeeper以TreeNode類型進行存儲,支持Cluster形式部署且保證最終數據一致性,關於ZK的資料網上比較豐富,相關概念不再重復介紹,本文以zookeeper-3.4.6為例,請從官網下載http://zookeeper.apache.org

2、創建ZookeeperLab文件夾目錄,模擬部署3台Zookeeper服務器集群,目錄結構如下。

     

3、解壓從官網下載的zookeeper-3.4.6.tar文件,並分別復制到三台ZkServer的zookeeper-3.4.6文件夾。

     

4、分別在三台ZkServer的data目錄下創建myid文件(注意沒有后綴),用於標識每台Server的ID,在Server1\data\myid文件中保存單個數字1,Server2的myid文件保存2,Server3的myid保存3。

5、創建ZkServer的配置文件,在zookeeper-3.4.6\conf文件夾目錄下創建zoo.cfg,可以從示例的zoo_sample.cfg 復制重命名。因為在同一台機器模擬Cluster部署,端口號不能重復,配置文件中已經有詳細的解釋,修改后的配置如下,其中Server1端口號2181,Server2端口號2182,Server3端口號2183。

復制代碼
# 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=E:/ZookeeperLab/server1/data
dataLogDir=E:/ZookeeperLab/server1/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
server.1=127.0.0.1:2888:3888
server.2=127.0.0.1:2889:3889
server.3=127.0.0.1:2890:3890
復制代碼

6、通過zookeeper-3.4.6\bin文件夾zkServer.bat文件啟動ZKServer,由於Cluster部署需要選舉Leader和Followers,所以在3個ZKServer全部啟動之前會提示一個WARN,屬正常現象。

      

7、Zookeeper啟動成功后可以通過zookeeper-3.4.6\bin文件夾的 zkCli.bat驗證連接是否正常,比如創建節點“create /testnode helloworld”,查看節點“get /testnode”,連接到組群中其它ZkServer,節點數據應該是一致的。更多指令請使用help命令查看。

     

8、對於Linux環境下部署基本一致,zoo.cfg配置文件中data和datalog文件夾路徑改為linux格式路徑,使用“./zkServer.sh start-foreground”命令啟動ZkServer,注意start啟動參數不能輸出異常信息。

      

9、至此Zookeeper的配置完畢。

 

dubbo治理平台部署(

上面內容看起來沒那么直觀。如果有一個控制台來管理和展現就太棒了。不得不說dubbo還是挺貼心的。

下載

官網下載地址:

http://code.alibabatech.com/mvn/releases/com/alibaba/dubbo-admin/2.4.1/dubbo-admin-2.4.1.war

但是該地址最近一直無法下載。

http://pan.baidu.com/share/link?shareid=2205137761&uk=3442370350&fid=707816148751698 可以通過這里下載。

安裝

將war包拷貝到tomcat/webapps目錄下,啟動tomcat。瀏覽器中輸入:

http://localhost:8080/dubbo/

dubbo-admin-2.5.3.war解壓后得到如下文件

刪除D:\ProgramFiles_java\Apache Software Foundation\Tomcat 7.0\webapps\ROOT路徑下所有文件,復制解壓文件到該路徑,效果如下

如需要修改登陸dubbo治理平台密碼,進入D:\ProgramFiles_java\Apache Software Foundation\Tomcat 7.0\webapps\ROOT\WEB-INF路徑,打開dubbo.properties

默認兩個用戶,用戶名密碼分別為 root/root  guest/guest

 

啟動tomcat,瀏覽器輸入地址:http://localhost:8080/ 進入dubbo治理平台

出現上圖說明dubbo治理平台部署完畢

服務提供者打包給服務消費者引用

     

3.源碼

<!-- zookeeper -->
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
            <scope>provided</scope>
        </dependency>
        
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
            <exclusions>
                <exclusion>
                    <artifactId>jmxtools</artifactId>
                    <groupId>com.sun.jdmk</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>jmxri</artifactId>
                    <groupId>com.sun.jmx</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>jms</artifactId>
                    <groupId>javax.jms</groupId>
                </exclusion>
            </exclusions>
        </dependency>

復制代碼
<!-- dubbo-->
    <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.4.9</version>
            <scope>compile</scope>
            <exclusions>
                <exclusion>
                    <artifactId>spring</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>

          

Provider applicationContext.xml:

<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 ">
<!-- 具體的實現bean -->
<bean id="demoService" class="com.unj.dubbotest.provider.impl.DemoServiceImpl"/>
<!-- 提供方應用信息,用於計算依賴關系 -->
<dubbo:application name="xixi_provider"/>
<!--
使用multicast廣播注冊中心暴露服務地址 <dubbo:registry address="multicast://224.5.6.7:1234" />
-->
<!-- 使用zookeeper注冊中心暴露服務地址 -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>
<!-- 用dubbo協議在20880端口暴露服務 -->
<dubbo:protocol name="dubbo" port="2090"/>
<!-- 聲明需要暴露的服務接口 -->
<dubbo:service interface="com.unj.dubbotest.provider.DemoService" ref="demoService"/>
</beans>

服務消費方代碼dubbo-consumer  下載地址:http://download.csdn.net/detail/adam_zs/9470354

Consumer applicationContext.xml:

<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="hi_consumer"/>
<!-- 使用zookeeper注冊中心暴露服務地址 -->
<!--
<dubbo:registry address="multicast://224.5.6.7:1234" />
-->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>
<!-- 生成遠程服務代理,可以像使用本地bean一樣使用demoService -->
<dubbo:reference id="demoService" interface="com.unj.dubbotest.provider.DemoService"/>
</beans>

調用方式 注入spring后,通過ApplicationContext獲取對應服務接口,調用服務方法。

4.啟動順序

  1. 啟動zookeeper 
  2. 啟動tomcat,啟動完畢可以輸入地址http://localhost:8080/可以看到dubbo治理平台
  3. 啟動服務提供者 Provider.java
  4. 啟動消費者 Consumer.java

5.成功啟動截圖


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM