dubbo入門-helloworld
在dubbo中,服務被注冊在注冊中心中,我們把提供服務的server成為服務提供方,調用服務的server稱為服務調用方,兩者通過RPC進行調用,並使用了dubbo協議(使用的協議可以經過配置進行修改)協調工作。為了demo的方便,這裏把服務接口以及相關的一些依賴類複製放在了兩個不同的工程中,在實際的開發中,我們需要提取一個API的工程,發布到MAVEN倉庫中,由服務調用方和服務提供方引用依賴。
準備工作
這裏使用zookeeper作為注冊中心,所以需要準備zookeeper的可用環境。dubbo官網中推薦zookeeper作為注冊中心,但據官網所述,阿裏內部沒有使用zookeeper,而是使用自己實現的一套注冊中心方案。zookeeper可以參考https://my.oschina.net/thinwonton/blog?catalog=5607452
1. 服務提供方
一般是服務的實現。往往在架構層麵抽象成服務層。
1.1 工程結構
在這裏,dubbo-parent是其它實例工程的父工程,為子工程提供統一的版本號管理。
DemoService.java
public interface DemoService {
/**
* 對兩個數求和
*/
public int sum(Integer x, Integer y);
/**
* 對兩個數做乘法運算
*
* @param x
* @param y
* @return
*/
public int multi(Integer x, Integer y);
}
DemoServiceImpl.java 實現類
public class DemoServiceImpl implements DemoService {
public int sum(Integer x, Integer y) {
return x + y;
}
public int multi(Integer x, Integer y) {
return x * y;
}
}
applicationContext.xml
spring的配置文件,dubbo框架運行在spring 容器中。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="https://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
https://code.alibabatech.com/schema/dubbo
https://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 引入服務提供者配置文件 -->
<import resource="classpath:dubbo.xml" />
</beans>
dubbo.xml
服務的配置,dubbo通過xml文件掃描,在spring的啟動階段把服務注冊到注冊中心。這裏的demoService是服務的實現者。當服務提供方獲取到消費方的rpc請求後,將會調用該實現類的對應的方法運行,並把結果通過PRC返回給調用方。 是指定協議類型,我們的提供者可以選擇dubbo或者RMI作為底層通信協議,或者兩者都選擇,由調用方選擇其中一種和服務方進行通信。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="https://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
https://code.alibabatech.com/schema/dubbo
https://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 配置Bean -->
<bean
/>
<!-- 指定web服務名字 -->
<dubbo:application name="helloworld-provider" />
<!-- 聲明服務注冊中心 -->
<!-- 使用zookeeper作為注冊中心 -->
<dubbo:registry protocol="zookeeper" address="192.168.88.15:2181" />
<!-- 指定傳輸層通信協議 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 暴露服務地址,該服務的實現類是demoService的引用 -->
<dubbo:service ref="demoService"
interface="com.github.thinwonton.dubbo.sample.helloworld.service.DemoService"
protocol="dubbo" />
</beans>
主程序 ProviderMain.java
public class ProviderMain {
public static void main(String[] args) throws IOException {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
System.out.println("服務啟動...");
System.in.read();
System.out.println("服務結束...");
}
}
當啟動提供方應用成功後,通過日誌可以看到服務已經在服務中心進行注冊,並獲取到如下信息:
- 暴露的服務IP和端口是192.168.42.2:20880
- 服務所在的應用名是helloworld-provider
- 所暴露的服務的接口和方法是com.github.thinwonton.dubbo.sample.helloworld.service.DemoService,multi & sum
INFO 13:42:53,738 com.alibaba.dubbo.registry.support.AbstractRegistry: [DUBBO] Register: dubbo://192.168.42.2:20880/com.github.thinwonton.dubbo.sample.helloworld.service.DemoService?anyhost=true&application=helloworld-provider&dubbo=2.5.3&interface=com.github.thinwonton.dubbo.sample.helloworld.service.DemoService&methods=multi,sum&pid=8764&side=provider×tamp=1488087764234, dubbo version: 2.5.3, current host: 127.0.0.1
INFO 13:42:53,777 com.alibaba.dubbo.registry.support.AbstractRegistry: [DUBBO] Subscribe: provider://192.168.42.2:20880/com.github.thinwonton.dubbo.sample.helloworld.service.DemoService?anyhost=true&application=helloworld-provider&category=configurators&check=false&dubbo=2.5.3&interface=com.github.thinwonton.dubbo.sample.helloworld.service.DemoService&methods=multi,sum&pid=8764&side=provider×tamp=1488087764234, dubbo version: 2.5.3, current host: 127.0.0.1
INFO 13:42:53,804 com.alibaba.dubbo.registry.support.AbstractRegistry: [DUBBO] Notify urls for subscribe url provider://192.168.42.2:20880/com.github.thinwonton.dubbo.sample.helloworld.service.DemoService?anyhost=true&application=helloworld-provider&category=configurators&check=false&dubbo=2.5.3&interface=com.github.thinwonton.dubbo.sample.helloworld.service.DemoService&methods=multi,sum&pid=8764&side=provider×tamp=1488087764234, urls: [empty://192.168.42.2:20880/com.github.thinwonton.dubbo.sample.helloworld.service.DemoService?anyhost=true&application=helloworld-provider&category=configurators&check=false&dubbo=2.5.3&interface=com.github.thinwonton.dubbo.sample.helloworld.service.DemoService&methods=multi,sum&pid=8764&side=provider×tamp=1488087764234], dubbo version: 2.5.3, current host: 127.0.0.1
通過zookeeper的客戶端連接ZK的服務,查看樹的ZNode情況
[root@www zookeeper-3.4.9]# bin/zkCli.sh -server 192.168.88.15:2181
[zk: 192.168.88.15:2181(CONNECTED) 2] ls /
[dubbo, zookeeper]
[zk: 192.168.88.15:2181(CONNECTED) 3] ls /dubbo
[com.github.thinwonton.dubbo.sample.helloworld.service.DemoService]
[zk: 192.168.88.15:2181(CONNECTED) 4] ls /dubbo/com.github.thinwonton.dubbo.sample.helloworld.service.DemoService
[consumers, routers, providers, configurators]
[zk: 192.168.88.15:2181(CONNECTED) 5] ls /dubbo/com.github.thinwonton.dubbo.sample.helloworld.service.DemoService/providers
[dubbo%3A%2F%2F192.168.42.2%3A20880%2Fcom.github.thinwonton.dubbo.sample.helloworld.service.DemoService%3Fanyhost%3Dtrue%26application%3Dhelloworld-provider%26dubbo%3D2.5.3%26interface%3Dcom.github.thinwonton.dubbo.sample.helloworld.service.DemoService%26methods%3Dmulti%2Csum%26pid%3D7968%26side%3Dprovider%26timestamp%3D1488095682955]
zk樹中,為dubbo的服務com.github.thinwonton.dubbo.sample.helloworld.service.DemoService建立了一個節點,並分別建立consumers, routers, providers, configurators四個節點。這四個節點分別記錄了消費方,服務路由信息,提供方,配置的一些信息。我們查看providers可以看到192.168.42.2:20880這台機器再為我們服務。
2. 服務調用方(消費方)
一般集成在WEB工程,即表現層工程。
2.1 工程結構
消費方 dubbo.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="https://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
https://code.alibabatech.com/schema/dubbo
https://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 指定web應用名字 -->
<dubbo:application name="helloworld-consumer" />
<!-- 聲明服務注冊中心 -->
<dubbo:registry protocol="zookeeper" address="192.168.88.15:2181" />
<!-- 引用服務,demoService僅用於根據接口生成動態代理,默認使用javassist生成代理對象 -->
<dubbo:reference
interface="com.github.thinwonton.dubbo.sample.helloworld.service.DemoService"
protocol="dubbo" />
</beans>
ConsumerMain.java
public class ConsumerMain {
public static void main(String[] args) throws IOException {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
DemoService demoService = (DemoService) applicationContext.getBean("demoService");
int sumResult = demoService.sum(1, 2);
System.out.println("加法服務的結果是:" + sumResult);
int multiResult = demoService.multi(3, 4);
System.out.println("減法服務的結果是:" + multiResult);
}
}
運行主程序後,即可看到相應的結果
文章轉載自 開源中國社區 [https://www.oschina.net]
最後更新:2017-06-30 15:31:52