Dubbo是阿里開源的分布式RPC通信框架,目前在國內(nèi)使用范圍比較廣,本文詳細(xì)描述Dubbo的入門級(jí)使用過程,希望能夠給想了解dubbo的人提供一點(diǎn)幫助。
前期準(zhǔn)備(括號(hào)內(nèi)為我使用的版本)
- JDK 1.5+(1.8.0_u161)
- eclipse(4.7.2)
- zookeeper(3.4.11)
- maven(3.5.2)
- 我們?cè)趀clipse中創(chuàng)建maven項(xiàng)目,命名為dubbo-demo。
- 創(chuàng)建三個(gè)子模塊,分別為dubbo-provider,dubbo-consumer,dubbo-api,從命名可以看出這三個(gè)模塊分別為服務(wù)提供者,服務(wù)消費(fèi)者和API接口項(xiàng)目。

項(xiàng)目初始化結(jié)構(gòu)
- 在 dubbo-demo 的 pom.xml中添加公共依賴,主要是添加dubbo和zookeeper客戶端依賴。代碼如下:
<dependencies>
<!-- dubbo當(dāng)前最新版 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.0</version>
</dependency>
<!-- zookeeper客戶端 -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
- 首先我們先處理 api,此項(xiàng)目為其他兩個(gè)項(xiàng)目提供接口,保證生產(chǎn)者和消費(fèi)者所使用的接口統(tǒng)一。我們?cè)?dubbo-api 項(xiàng)目中創(chuàng)建包 com.aotian.demo.dubbo.api,并添加 DemoService 接口。

API項(xiàng)目結(jié)構(gòu)及代碼
- 接口類定義后我們需要在服務(wù)端和客戶端項(xiàng)目中引用,保證兩端接口一致,分別在兩個(gè)項(xiàng)目的 pom.xml 中添加如下代碼:
<dependencies>
<!-- API接口引用 -->
<dependency>
<groupId>com.aotian</groupId>
<artifactId>dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
- 引用接口之后,我們需要在服務(wù)端添加接口實(shí)現(xiàn)類,在 dubbo-provider 項(xiàng)目中創(chuàng)建包 com.aotian.demo.dubbo.provider,并添加 DemoService 接口的實(shí)現(xiàn)類。

服務(wù)端項(xiàng)目結(jié)構(gòu)及代碼實(shí)現(xiàn).png
- 接口實(shí)現(xiàn)類添加后,我們需要將其注冊(cè)到注冊(cè)中心,只有這樣,其他應(yīng)用才有可能請(qǐng)求到,接下來我們?cè)?src / main / resources 下創(chuàng)建 dubbo-provider.xml 文件,用于配置dubbo服務(wù)端注冊(cè),代碼如下:
<?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應(yīng)用程序命名-->
<dubbo:application name="dubbo-demo-provider"/>
<!--dubbo注冊(cè)地址-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!--dubbo協(xié)議地址-->
<dubbo:protocol name="dubbo" port="20880"/>
<!--接口聲明-->
<dubbo:service interface="com.aotian.demo.dubbo.api.DemoService" ref="demoService"/>
<bean id="demoService" class="com.aotian.demo.dubbo.provider.DemoServiceImpl"/>
</beans>
- 接下來我們準(zhǔn)備對(duì)服務(wù)端進(jìn)行注冊(cè),所以需要準(zhǔn)備注冊(cè)中心,這里用的是 zookeeper,官網(wǎng)下載 zookeeper 后解壓,將conf文件夾下的 zoo_sample.cfg 重命名為 zoo.cfg,之后在命令提示符下切換至bin目錄下,執(zhí)行 .\zkServer.cmd,看到如下提示后表示啟動(dòng)成功。
2018-03-13 23:12:59,007 [myid:] - INFO [main:NIOServerCnxnFactory@89] - binding to port 0.0.0.0/0.0.0.0:2181
- 啟動(dòng)服務(wù)端。本 Demo 用于測(cè)試,所以使用 main 方法加載的形式加載服務(wù)端,實(shí)際使用過程中可能會(huì)在 web 環(huán)境中使用。在服務(wù)端下添加 Provider.java 并執(zhí)行 main 方法即可,其他 main 方法實(shí)現(xiàn)如下:
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context
= new ClassPathXmlApplicationContext("classpath:dubbo-provider.xml");
context.start();
// 阻塞當(dāng)前進(jìn)程,否則程序會(huì)直接停止
System.in.read();
}
- 處理好服務(wù)端之后,我們?cè)龠M(jìn)行客戶端的處理,首先添加客戶端配置文件 dubbo-consumer.xml 放于 src / main / resources 下,內(nèi)容與服務(wù)端相似,只不過不需要暴露接口,而是引用接口,代碼如下:
<?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應(yīng)用程序命名-->
<dubbo:application name="dubbo-demo-provider"/>
<!--dubbo注冊(cè)地址-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!--接口引用-->
<dubbo:reference interface="com.aotian.demo.dubbo.api.DemoService" id="demoService"/>
</beans>
- 處理完配置文件,我們可以在客戶端進(jìn)行測(cè)試了,在 src / main / java 下創(chuàng)建包 com.aotian.demo.dubbo.api,并創(chuàng)建文件 consumer.java,main 方法代碼如下:
public static void main(String[] args) {
ClassPathXmlApplicationContext context
= new ClassPathXmlApplicationContext("classpath:dubbo-consumer.xml");
context.start();
String username = "aotian";
DemoService demoService = (DemoService) context.getBean("demoService");
System.out.println(demoService.changeUsername(username));
}
- 執(zhí)行后便可在控制臺(tái)查看結(jié)果,如下圖所示,表示客戶端和服務(wù)端已經(jīng)成功調(diào)用。

執(zhí)行結(jié)果