在上一篇中我們提到,dubbo官網上推薦使用ZooKeeper作為註冊中心。那麼今天我們就通過程式碼來實踐一番,看看一個dubbo的服務消費者如果找到通過ZooKeeper暴露自己的dubbo服務提供者,併成功完成互動。

首先建立一個dubbo-test工程,並新增相關的依賴:

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.6</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.10</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.5</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.32.Final</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.8.0</version>
</dependency>

從依賴中也可以看到,dubbo的底層通訊實際上是藉助netty實現的。接下來我們在dubbo-test工程中建立一個名為dubbo-test-provider的module,並編寫需要暴露的服務介面。

首先建立一個介面,ProviderService:

package provider.service;

public interface ProviderService {

String helloService(String name);
}

接下來對這個介面進行實現:

package provider.impl;

import provider.service.ProviderService;

public class ProviderServiceImpl implements ProviderService {

    public String helloService(String name) {
return "Hello " + name + "!";
}
}

開始構建服務者使用的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 = "dubbo_provider" /> <!--註冊中心 -->
<dubbo:registry address = "zookeeper://127.0.0.1:2181" /> <!-- 用dubbo協議在20880埠暴露服務 -->
<dubbo:protocol name = "dubbo" port = "20880" /> <!--服務釋出的配置,需要暴露的服務介面-->
<dubbo:service interface = "provider.service.ProviderService" ref = "providerService" /> <!--Bean-->
<bean id = "providerService" class = "provider.impl.ProviderServiceImpl"/> </beans>

這裡補充說明一下,如果需要對接的是一個ZooKeeper叢集的話,可以採用下方的寫法:

<dubbo:registry protocol = "zookeeper" address = "192.168.11.129:2181,192.168.11.130:2181"/>

啟動類中自然需要載入dubbo的配置檔案:

public class DubboProvider {

    public static void main( String[] args ) throws IOException {

        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
context.start();
System.in.read();
}
}

此時的目錄結構如下:

接下來繼續新增一個module,起名為dubbo-test-consumer。需要注意的是,除非兩個module載入同一份介面類,否則消費者專案中也需要有路徑相同的介面宣告,如下圖所示:

由於我們的消費者僅僅用於呼叫服務提供者暴露的介面,無需暴露自己本身,因此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 = "dubbo_consumer" /> <!--註冊中心 -->
<dubbo:registry address = "zookeeper://127.0.0.1:2181" /> <dubbo:reference id = "providerService" interface = "provider.service.ProviderService"/> </beans>

啟動類中進行服務的呼叫並列印返回結果:

public class DubboConsumer {

    public static void main( String[] args ) throws IOException {

        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("consumer.xml");
context.start();
ProviderService providerService = (ProviderService) context.getBean("providerService");
String s = providerService.helloService("eddy");
System.out.println(s);
System.in.read(); }
}

依次啟動ZooKeeper、服務端以及消費端,效果如下:

參考資料:

https://blog.csdn.net/liyintaoliuyun/article/details/80066269

https://segmentfault.com/a/1190000019896723

https://blog.csdn.net/hjiacheng/article/details/55000570