現在在搞dubbo調研,過程記錄下來,備忘。
Dubbo[
]是一個分布式服務框架,致力於提供高性能和透明化的RPC遠程服務調用方案,以及SOA服務治理方案。這是一個開源項目,項目地址:https://github.com/alibaba/dubbo,中文文檔地址目前是:http://alibaba.github.io/dubbo-doc-static/Home-zh.htm
現在搭建一個基於zookeeper為注冊中心的例子,需要提前安裝zookeeper。
1. 新建一個maven項目dubboTest,修改pom文件,增加以下依賴
1 <dependencies> 2 <dependency> 3 <groupId>org.springframework</groupId> 4 <artifactId>spring-context</artifactId> 5 <version>3.2.5.RELEASE</version> 6 </dependency> 7 8 <dependency> 9 <groupId>com.alibaba</groupId> 10 <artifactId>dubbo</artifactId> 11 <version>2.4.9</version> 12 </dependency> 13 14 <dependency> 15 <groupId>org.apache.zookeeper</groupId> 16 <artifactId>zookeeper</artifactId> 17 <version>3.3.3</version> 18 <exclusions> 19 <exclusion> 20 <groupId>com.sun.jmx</groupId> 21 <artifactId>jmxri</artifactId> 22 </exclusion> 23 <exclusion> 24 <groupId>com.sun.jdmk</groupId> 25 <artifactId>jmxtools</artifactId> 26 </exclusion> 27 <exclusion> 28 <groupId>javax.jms</groupId> 29 <artifactId>jms</artifactId> 30 </exclusion> 31 </exclusions> 32 </dependency> 33 34 <dependency> 35 <groupId>com.github.sgroschupf</groupId> 36 <artifactId>zkclient</artifactId> 37 <version>0.1</version> 38 </dependency> 39 40 <dependency> 41 <groupId>com.netflix.curator</groupId> 42 <artifactId>curator-framework</artifactId> 43 <version>1.1.16</version> 44 </dependency> 45 </dependencies>
2. xml配置
在resources下創建provider.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://code.alibabatech.com/schema/dubbo 8 http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> 9 10 <!-- 提供方應用信息,用於計算依賴關系 --> 11 <dubbo:application name="dubbo-test-service" /> 12 13 <!-- 使用multicast廣播注冊中心暴露服務地址 --> 14 <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> --> 15 <dubbo:registry address="zookeeper://127.0.0.1:2181" client="zkclient" /> 16 17 <!-- 用dubbo協議在20880端口暴露服務 --> 18 <dubbo:protocol name="dubbo" port="20880" /> 19 20 <!-- 聲明需要暴露的服務接口 --> 21 <dubbo:service interface="com.sogou.dubbo.demo.DemoService" ref="demoService" version="1.0" /> 22 23 <!-- 和本地bean一樣實現服務 --> 24 <bean id="demoService" class="com.sogou.dubbo.demo.provider.DemoServiceImpl" /> 25 26 </beans>
在resources下創建consumer.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://code.alibabatech.com/schema/dubbo 8 http://code.alibabatech.com/schema/dubbo/dubbo.xsd 9 "> 10 11 <!-- 消費方應用名,用於計算依賴關系,不是匹配條件,不要與提供方一樣 --> 12 <dubbo:application name="dubbo-test-consumer" /> 13 14 <!-- 使用multicast廣播注冊中心暴露發現服務地址 --> 15 <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> --> 16 <dubbo:registry address="zookeeper://127.0.0.1:2181" /> 17 18 <!-- 生成遠程服務代理,可以和本地bean一樣使用demoService --> 19 <dubbo:reference id="demoService" interface="com.sogou.dubbo.demo.DemoService" version="1.0" /> 20 21 </beans>
每個接口都應定義版本號version,為后續不兼容升級提供可能
3. 創建相關服務接口
新建一個DemoService接口
1 package com.sogou.dubbo.demo; 2 3 public interface DemoService { 4 5 String sayHello(String name); 6 }
4. 實現相關服務
實現DemoService接口
1 package com.sogou.dubbo.demo.provider; 2 3 import com.sogou.dubbo.demo.DemoService; 4 5 public class DemoServiceImpl implements DemoService { 6 7 @Override 8 public String sayHello(String name) { 9 return "Hello " + name; 10 } 11 }
5. 創建測試服務提供者和消費者
創建服務提供者:
1 package com.sogou.dubbo.demo; 2 3 import org.springframework.context.support.ClassPathXmlApplicationContext; 4 5 import java.io.IOException; 6 7 public class Provider { 8 9 public static void main(String[] args) throws IOException { 10 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"provider.xml"}); 11 context.start(); 12 System.out.println("回車鍵結束..."); 13 System.in.read(); 14 } 15 16 }
創建消費者
1 package com.sogou.dubbo.demo; 2 3 import org.springframework.context.support.ClassPathXmlApplicationContext; 4 5 public class Consumer { 6 7 public static void main(String[] args) { 8 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"consumer.xml"}); 9 context.start(); 10 11 DemoService demoService = (DemoService) context.getBean("demoService"); 12 String res = demoService.sayHello("world"); 13 14 System.out.println(res); 15 } 16 17 }
6.測試
先啟動zookeeper,然后運行provider.java啟動服務,最后運行consumer.java來測試。
