0.原理

- ConfigServer
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 |
- Client
- Server
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可以提供配置管理、命名服務、分布式算法、集群管理功能。具體說明參看如下文章:
)
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。瀏覽器中輸入:
)
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>
<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:
服務消費方代碼dubbo-consumer 下載地址:http://download.csdn.net/detail/adam_zs/9470354
Consumer applicationContext.xml:
調用方式 注入spring后,通過ApplicationContext獲取對應服務接口,調用服務方法。
4.啟動順序
- 啟動zookeeper
- 啟動tomcat,啟動完畢可以輸入地址http://localhost:8080/可以看到dubbo治理平台
- 啟動服務提供者 Provider.java
- 啟動消費者 Consumer.java