本文采用Dubbo與Zookeeper、Spring框架的整合。
Dubbo是什么?
Dubbo 不單單只是高性能的 RPC 調(diào)用框架,更是 SOA 服務(wù)治理的一種方案。
核心:
1. 遠(yuǎn)程通訊(RPC): 提供對多種基于長連接的NIO框架抽象封裝,包括多種線程模型,序列化,以及“請求-響應(yīng)”模式的信息交換方式。
2. 集群容錯: 提供基于接口方法的透明遠(yuǎn)程過程調(diào)用,包括多協(xié)議支持,以及軟負(fù)載均衡,失敗容錯,地址路由,動態(tài)配置等集群支持。
3. 自動發(fā)現(xiàn): 基于注冊中心目錄服務(wù),使服務(wù)消費(fèi)方能動態(tài)的查找服務(wù)提供方,使地址透明,使服務(wù)提供方可以平滑增加或減少機(jī)器。
Dubbo 架構(gòu)流程圖

節(jié)點(diǎn)角色說明:
Provider: 暴露服務(wù)的服務(wù)提供方。
Consumer: 調(diào)用遠(yuǎn)程服務(wù)的服務(wù)消費(fèi)方。
Registry: 服務(wù)注冊與發(fā)現(xiàn)的注冊中心。
Monitor: 統(tǒng)計服務(wù)的調(diào)用次調(diào)和調(diào)用時間的監(jiān)控中心。
Container: 服務(wù)運(yùn)行容器。
調(diào)用流程
0.啟動服務(wù)提供者。
1.服務(wù)提供者在啟動時,向注冊中心注冊自己提供的服務(wù)。
2.服務(wù)消費(fèi)者在啟動時,向注冊中心訂閱自己所需的服務(wù)。
3.注冊中心返回服務(wù)提供者地址列表給消費(fèi)者,如果有變更,注冊中心將基于長連接推送變更數(shù)據(jù)給消費(fèi)者。
4.服務(wù)消費(fèi)者,從提供者地址列表中,基于軟負(fù)載均衡算法,選一臺提供者進(jìn)行調(diào)用,如果調(diào)用失敗,再選另一臺調(diào)用。
5.服務(wù)消費(fèi)者和提供者,在內(nèi)存中累計調(diào)用次數(shù)和調(diào)用時間,定時每分鐘發(fā)送一次統(tǒng)計數(shù)據(jù)到監(jiān)控中心。
dubbo支持多種類型的注冊中心:
Multicast注冊中心
Zookeeper注冊中心
Redis注冊中心
Simple注冊中心
ZooKeeper 服務(wù)注冊中心
ZooKeeper 是一個分布式的,開放源碼的分布式應(yīng)用程序協(xié)調(diào)服務(wù)。它是一個為分布式應(yīng)用提供一致性服務(wù)的軟件,提供的功能包括:配置維護(hù)、域名服務(wù)、分布式同步、組服務(wù)等。
Step1:下載最新版本且穩(wěn)定的zookeeper,在http://apache.fayea.com/zookeeper/里就可以下載,也可以在我的資源管理器中下載Dubbo與Zookeeper所需的工具
step2:解壓下載的文件,zookeeper不需要安裝的,解壓即可。 然后把解壓后文件放到你要的位置,如:E:\zookeeper\zookeeper-3.3.6
Step3:進(jìn)入E:\zookeeper\zookeeper-3.3.6\conf目錄,將里面的zoo_sample.cfg文件,做一個備份,然后改名為zoo.cfg,因為zookeeper啟動后,只認(rèn)識zoo.cfg中的所有設(shè)置和配置
Step4:打開zoo.cfg,輸入以下內(nèi)容:
# zk之間(當(dāng)然是配置了zk集群模式)、zk與客戶端之間每隔2秒進(jìn)行一次心跳檢測
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit和leader之間最長心跳時間,設(shè)置的是10那么就是tickTime的10陪,即2000毫秒*10=20000毫秒=20秒
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
#leader和follower之間發(fā)送消息, 請求和應(yīng)答的最大時間長度. 此時該參數(shù)設(shè)置為5, 說明時間限制為5倍tickTime, 即2000毫秒*5=10
syncLimit=5
# the directory where the snapshot is stored.
# 數(shù)據(jù)目錄. 可以是任意目錄.
dataDir=E:\\zookeeper\\zookeeper-3.3.6\\data
dataLogDir=E:\\zookeeper\\zookeeper-3.3.6\\logs
# the port at which the clients will connect
# 監(jiān)聽client連接的端口號.
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
# 這個操作將限制連接到 ZooKeeper 的客戶端的數(shù)量,限制并發(fā)連接的數(shù)量,它通過 IP 來區(qū)分不同的客戶端。此配置選項可以用來阻止某些類別的 Dos 攻擊。將它設(shè)置為 0 或者忽略而不進(jìn)行設(shè)置將會取消對并發(fā)連接的限制。
#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
# 客戶端在與zookeeper交互過程中會產(chǎn)生非常多的日志,而且zookeeper也會將內(nèi)存中的數(shù)據(jù)作為snapshot保存下來,這些數(shù)據(jù)是不會被自動刪除的,這樣磁盤中這樣的數(shù)據(jù)就會越來越多。不過可以通過這兩個參數(shù)來設(shè)置,讓zookeeper自動刪除數(shù)據(jù)。autopurge.purgeInterval就是設(shè)置多少小時清理一次。而autopurge.snapRetainCount是設(shè)置保留多少個snapshot,之前的則刪除。
# 不過如果你的集群是一個非常繁忙的集群,然后又碰上這個刪除操作,可能會影響zookeeper集群的性能,所以一般會讓這個過程在訪問低谷的時候進(jìn)行,但是遺憾的是zookeeper并沒有設(shè)置在哪個時間點(diǎn)運(yùn)行的設(shè)置,所以有的時候我們會禁用這個自動刪除的功能,而在服務(wù)器上配置一個cron,然后在凌晨來干這件事。
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
Step6: 使用cmd,進(jìn)入E:\zookeeper\zookeeper-3.3.6\bin,運(yùn)行zkServer.cmd,啟動服務(wù),如下圖(port:2181是zookeeper的專用監(jiān)聽端口)。

創(chuàng)建MAVEN項目
項目結(jié)構(gòu):
DubboDemo 工程,主工程,主要導(dǎo)入公共jar包等
dubbo-api : 工程 存放公共接口;
dubbo-consumer : 工程 調(diào)用遠(yuǎn)程服務(wù)(消費(fèi)者);
dubbo-provider : 工程 提供遠(yuǎn)程服務(wù)(服務(wù)者)。
(注:每個工程都是獨(dú)立的,通過maven 引入父子關(guān)系)

1、新建MAVEN項目 DubboDemo
導(dǎo)入所需要的jar包依賴。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>DubboDemo</groupId>
<artifactId>DubboDemo</artifactId>
<packaging>pom</packaging>
<version>0.0.1-SNAPSHOT</version>
<modules>
<module>dubbo-api</module>
<module>dubbo-consumer</module>
<module>dubbo-provider</module>
</modules>
<name>DubboDemo Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- spring版本號 -->
<spring.version>4.2.5.RELEASE</spring.version>
<!-- mybatis版本號 -->
<mybatis.version>3.2.8</mybatis.version>
<!-- mysql驅(qū)動版本號 -->
<mysql-driver.version>5.1.29</mysql-driver.version>
<!-- log4j日志包版本號 -->
<slf4j.version>1.7.18</slf4j.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- 添加jstl依賴 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<!-- 添加junit4依賴 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<!-- 指定范圍,在測試時才會加載 -->
<scope>test</scope>
</dependency>
<!-- 添加spring核心依賴 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 添加mybatis依賴 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- 添加mybatis/spring整合包依賴 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- 添加mysql驅(qū)動依賴 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-driver.version}</version>
</dependency>
<!-- 添加數(shù)據(jù)庫連接池依賴 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<!-- 添加fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.22</version>
</dependency>
<!-- 添加日志相關(guān)jar包 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- log end -->
<!-- 映入JSON -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.9</version>
</dependency>
<!-- dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<dependency>
<groupId>DubboDemo</groupId>
<artifactId>dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<finalName>DubboDemo</finalName>
</build>
</project>
2、在此項目下新建MAVEN項目:dubbo-api
創(chuàng)建接口DemoService
package com.dubbo.demo;
import java.util.List;
public interface DemoService {
List<String> getPermissions(Long id);
}
3、創(chuàng)建dubbo-provider的MAVEN項目
加入公共接口所在的依賴
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>DubboDemo</artifactId>
<groupId>DubboDemo</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-provider</artifactId>
</project>
實(shí)現(xiàn)接口實(shí)現(xiàn)
package com.dubbo.demo.impl;
import java.util.ArrayList;
import java.util.List;
import com.dubbo.demo.DemoService;
public class DemoServiceImpl implements DemoService{
public List<String> getPermissions(Long id) {
// TODO Auto-generated method stub
List<String> demo = new ArrayList<String>();
demo.add(String.format("Permission_%d", id - 1));
demo.add(String.format("Permission_%d", id));
demo.add(String.format("Permission_%d", id + 1));
return demo;
}
}
添加Spring配置聲明文件 Provider.xml 此文件放在target/classes文件夾下
<?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">
<!--定義了提供方應(yīng)用信息,用于計算依賴關(guān)系;在 dubbo-admin 或 dubbo-monitor 會顯示這個名字,方便辨識-->
<dubbo:application name="demotest-provider" owner="programmer" organization="dubbox"/>
<!--使用 zookeeper 注冊中心暴露服務(wù),注意要先開啟 zookeeper-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!-- 用dubbo協(xié)議在20880端口暴露服務(wù) -->
<dubbo:protocol name="dubbo" port="20880" />
<!--使用 dubbo 協(xié)議實(shí)現(xiàn)定義好的 api.PermissionService 接口-->
<dubbo:service interface="com.dubbo.demo.DemoService" ref="demoService" protocol="dubbo" />
<!--具體實(shí)現(xiàn)該接口的 bean-->
<bean id="demoService" class="com.dubbo.demo.impl.DemoServiceImpl"/>
</beans>
添加啟動遠(yuǎn)程服務(wù)類Provider:
package com.dubbo.demo.impl;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
System.out.println(context.getDisplayName() + ": here");
context.start();
System.out.println("服務(wù)已經(jīng)啟動...");
System.in.read();
}
}
4、創(chuàng)建dubbo-consumer的MAVEN項目
通過Spring引用服務(wù)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>DubboDemo</artifactId>
<groupId>DubboDemo</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-consumer</artifactId>
</project>
添加Spring配置聲明文件 consumer.xml 此文件放在target/classes文件夾下
<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="demotest-consumer" owner="programmer" organization="dubbox"/>
<!--向 zookeeper 訂閱 provider 的地址,由 zookeeper 定時推送-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!--使用 dubbo 協(xié)議調(diào)用定義好的 api.PermissionService 接口-->
<dubbo:reference id="permissionService" interface="com.dubbo.demo.DemoService"/>
</beans>
創(chuàng)建Consumer類, 啟動Consumer,調(diào)用遠(yuǎn)程服務(wù)
package com.dubbo.consumer;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.dubbo.demo.DemoService;
public class Consumer {
public static void main(String[] args) {
//測試常規(guī)服務(wù)
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("consumer.xml");
context.start();
System.out.println("consumer start");
DemoService demoService = context.getBean(DemoService.class);
System.out.println("consumer");
System.out.println(demoService.getPermissions(1L));
}
}
5、運(yùn)行項目,先確保provider已被運(yùn)行后再啟動consumer模塊
順序:zookeeper –> dubbo-provider –> dubbo-consumer
運(yùn)行結(jié)果如下:

dubbo-admin 搭建
提供界面化管理dubbo 服務(wù)
下載地址 :https://github.com/apache/incubator-dubbo
下載之后的目錄如下圖:

找到dubbo-admin,將其進(jìn)行打war包
進(jìn)入cmd,dubbo-admin 根目錄,執(zhí)行 mvn package -Dmaven.skip.test=true


(如果打不成功,可以下載我的dubbo-admin.war包)
將 dubbo-admin-2.5.4.war拷出放到tomcat的webapps里,因為zookeeper默認(rèn)使用的是8080,為避免沖突,tomcat的 conf\server.xml 文件,把啟動端口改成8090(不會起沖突的端口都可以)。
緊接著運(yùn)行tomcat,war包將自動解壓,為避免覆蓋。解壓完畢時,停掉tomcat,把war包刪掉,此時進(jìn)入WEB-INF目錄,dubbo.properties文件里配置信息:
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest
回到zookeeper文件夾,找到bin目錄下的zkServer,雙擊將它啟動。啟動完畢,再啟動剛剛配置的tomcat。瀏覽器輸入http://localhost:8090/dubbo-admin-2.5.4/,輸入賬號root,密碼root。進(jìn)入頁面如下:

