為了安全:服務啟動的ip全部使用10.10.10.10
版本:
- dubbo:2.5.5
重要的網址:
- dubbo的github:https://github.com/alibaba/dubbo
- dubbo官網:http://dubbo.io/
- dubbo使用者手冊:https://dubbo.gitbooks.io/dubbo-user-book/
- dubbo管理者手冊:https://dubbo.gitbooks.io/dubbo-admin-book/
- dubbo開發者手冊:https://dubbo.gitbooks.io/dubbo-dev-book/
首先從https://github.com/alibaba/dubbo下載dubbo源碼到本地,我們的第一個dubbo項目就是dubbo源碼中的dubbo-demo子模塊。
代碼結構如下:
其中:
- dubbo-demo-api是提供服務接口的模塊
- 在生產中,該模塊會單獨打成jar包,分別被provider和consumer依賴,provider實現該接口,consumer通過該接口引用provider實現的服務
- dubbo-demo-provider是服務提供者
- dubbo-demo-consumer是服務消費者
一 dubbo-demo-api
1 package com.alibaba.dubbo.demo; 2 3 public interface DemoService { 4 String sayHello(String name); 5 }
只提供了一個接口。
二 dubbo-demo-provider
1 配置文件
src/main/resources/META-INF/spring/dubbo-demo-provider.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 4 xmlns="http://www.springframework.org/schema/beans" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 6 http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> 7 8 <!-- 提供方應用信息,用於計算依賴關系 --> 9 <dubbo:application name="demo-provider"/> 10 11 <!-- 使用zookeeper注冊中心,並使用curator客戶端 --> 12 <dubbo:registry protocol="zookeeper" address="10.211.55.5:2181" client="curator"/> 13 14 <!-- 使用dubbo協議在20880端口暴露服務 --> 15 <dubbo:protocol name="dubbo" port="20880"/> 16 17 <!-- 和本地bean一樣實現服務 --> 18 <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/> 19 20 <!-- 聲明需要暴露的服務接口 --> 21 <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/> 22 </beans>
配置項:
- dubbo:application:指定應用名字,在生產中通常與artifactId相同
- dubbo:registry:指定注冊中心
- protocol:使用的注冊協議
- address:注冊中心地址
- client:默認情況下,操作zookeeper的java客戶端使用的是zkClient,這里使用curator
- dubbo:protocol:指定服務暴露的協議和端口
- dubbo:service:生命暴露的服務接口及其實現類
2 provider提供服務接口實現
1 package com.alibaba.dubbo.demo.provider; 2 3 import com.alibaba.dubbo.demo.DemoService; 4 import com.alibaba.dubbo.rpc.RpcContext; 5 6 import java.text.SimpleDateFormat; 7 import java.util.Date; 8 9 public class DemoServiceImpl implements DemoService { 10 public String sayHello(String name) { 11 System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress()); 12 return "Hello " + name + ", response form provider: " + RpcContext.getContext().getLocalAddress(); 13 } 14 }
3 provider啟動類
1 package com.alibaba.dubbo.demo.provider; 2 3 import org.springframework.context.support.ClassPathXmlApplicationContext; 4 5 public class Provider { 6 public static void main(String[] args) throws Exception { 7 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-provider.xml"}); 8 context.start(); 9 10 System.in.read(); // 按任意鍵退出 11 } 12 }
三 dubbo-demo-consumer
1 配置文件
src/main/resources/META-INF/spring/dubbo-demo-consumer.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 4 xmlns="http://www.springframework.org/schema/beans" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 6 http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> 7 8 <!-- 消費方應用名,用於計算依賴關系,不是匹配條件,不要與提供方一樣 --> 9 <dubbo:application name="demo-consumer"/> 10 11 <!-- 使用zookeeper注冊中心,並使用curator客戶端 --> 12 <dubbo:registry protocol="zookeeper" address="10.211.55.5:2181" client="curator"/> 13 14 <!-- 生成遠程服務代理,可以和本地bean一樣使用demoService --> 15 <dubbo:reference id="demoService" check="false" interface="com.alibaba.dubbo.demo.DemoService"/> 16 </beans>
配置項:
- dubbo:reference:指定引用的服務
- check:檢查注冊中心是否有可用的 provider
2 consumer啟動類
1 package com.alibaba.dubbo.demo.consumer; 2 3 import com.alibaba.dubbo.demo.DemoService; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 public class Consumer { 7 public static void main(String[] args) { 8 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-consumer.xml"}); 9 context.start(); 10 11 DemoService demoService = (DemoService) context.getBean("demoService"); // 獲取遠程服務代理 12 String hello = demoService.sayHello("world"); // 執行遠程方法 13 14 System.out.println(hello); // 顯示調用結果 15 } 16 }
啟動服務並且調用遠程服務。
四 啟動服務
1 啟動provider
啟動成功后,會發現在zookeeper上創建了節點:
/dubbo
--/com.alibaba.dubbo.demo.DemoService
----/providers
------/dubbo://10.10.10.10:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=1393&side=provider×tamp=1506831679007
注意:
- 所有dubbo相關的都會注冊在“/dubbo”根節點下
- 與當前暴露的接口服務相關的,都注冊在“/dubbo/接口”下
2 啟動consumer
啟動成功后,會發現在zookeeper上創建了節點:
/dubbo
--/com.alibaba.dubbo.demo.DemoService
----/consumers
------/consumer://10.10.10.10/com.alibaba.dubbo.demo.DemoService?application=demo-consumer&category=consumers&check=false&dubbo=2.0.0&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=1434&side=consumer×tamp=1506832498078
之后,看到provider和consumer雙方的互動輸出,則表示rpc成功!
第一個dubbo項目就結束了,dubbo-demo項目也可以查看http://dubbo.io/的例子。