1、在Spring中使用Dubbojava
一、Maven依賴git
1 <dependency> 2 <groupId>com.alibaba</groupId> 3 <artifactId>dubbo</artifactId> 4 <version>2.5.3.6</version> 5 <exclusions> 6 <exclusion> 7 <groupId>log4j</groupId> 8 <artifactId>log4j</artifactId> 9 </exclusion> 10 <exclusion> 11 <groupId>commons-logging</groupId> 12 <artifactId>commons-logging</artifactId> 13 </exclusion> 14 <exclusion> 15 <groupId>org.springframework</groupId> 16 <artifactId>spring</artifactId> 17 </exclusion> 18 <exclusion> 19 <groupId>com.alibaba</groupId> 20 <artifactId>fastjson</artifactId> 21 </exclusion> 22 </exclusions> 23 </dependency> 24 <dependency> 25 <groupId>com.github.sgroschupf</groupId> 26 <artifactId>zkclient</artifactId> 27 <version>0.1</version> 28 </dependency>
二、DUBBO生產者註冊到zookeeper的xml配置方式github
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 <!-- 具體的實現bean --> 11 <bean id="demoService" 12 class="com.unj.dubbotest.provider.impl.DemoServiceImpl" /> 13 <!-- 提供方應用信息,用於計算依賴關係 --> 14 <dubbo:application name="xixi_provider" /> 15 <!-- 使用multicast廣播註冊中心暴露服務地址 16 <dubbo:registry address="multicast://224.5.6.7:1234" />--> 17 <!-- 使用zookeeper註冊中心暴露服務地址 --> 18 <dubbo:registry address="zookeeper://127.0.0.1:2181" /> 19 <!-- 用dubbo協議在20880端口暴露服務 --> 20 <dubbo:protocol name="dubbo" port="20880" /> 21 <!-- 聲明須要暴露的服務接口 --> 22 <dubbo:service interface="com.unj.dubbotest.provider.DemoService" version="mys" 23 ref="demoService" /> 24 </beans>
三、DUBBO消費者註冊到zookeeper的xml配置方式web
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="consumer-of-helloworld-app" /> 12 <!-- 註冊地址,用於消費者尋找服務 --> 13 <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181,10.128.3.33:2181" /> 14 <dubbo:consumer timeout="5000" /> 15 <!-- 引用的服務 --> 16 <dubbo:reference id="demoService"interface="com.unj.dubbotest.provider.DemoService" version="mys" /> 17 </beans>
2、在Spring Boot中使用Dubbospring
在Spring Boot中使用Dubbo,不須要使用xml的方式來配置生產者和消費者,須要使用@Bean註解的方式來進行配置。json
一、Maven依賴app
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-web</artifactId> 4 <version>1.2.5.RELEASE</version> 5 </dependency> 6 <dependency> 7 <groupId>org.springframework.boot</groupId> 8 <artifactId>spring-boot-starter</artifactId> 9 <version>1.2.5.RELEASE</version> 10 </dependency> 11 <dependency> 12 <groupId>com.alibaba</groupId> 13 <artifactId>dubbo</artifactId> 14 <version>2.5.3.6</version> 15 <exclusions> 16 <exclusion> 17 <groupId>log4j</groupId> 18 <artifactId>log4j</artifactId> 19 </exclusion> 20 <exclusion> 21 <groupId>commons-logging</groupId> 22 <artifactId>commons-logging</artifactId> 23 </exclusion> 24 <exclusion> 25 <groupId>org.springframework</groupId> 26 <artifactId>spring</artifactId> 27 </exclusion> 28 <exclusion> 29 <groupId>com.alibaba</groupId> 30 <artifactId>fastjson</artifactId> 31 </exclusion> 32 </exclusions> 33 </dependency> 34 <dependency> 35 <groupId>com.github.sgroschupf</groupId> 36 <artifactId>zkclient</artifactId> 37 <version>0.1</version> 38 </dependency>
二、Dubbo基礎配置ide
1 public class DubboBaseConfig { 2 3 @Bean 4 public RegistryConfig registry() { 5 RegistryConfig registryConfig = new RegistryConfig(); 6 registryConfig.setAddress("127.0.0.1:2181"); 7 registryConfig.setProtocol("zookeeper"); 8 return registryConfig; 9 } 10 11 @Bean 12 public ApplicationConfig application() { 13 ApplicationConfig applicationConfig = new ApplicationConfig(); 14 applicationConfig.setName("testApp"); 15 return applicationConfig; 16 } 17 18 @Bean 19 public MonitorConfig monitorConfig() { 20 MonitorConfig mc = new MonitorConfig(); 21 mc.setProtocol("registry"); 22 return mc; 23 } 24 25 @Bean 26 public ReferenceConfig referenceConfig() { 27 ReferenceConfig rc = new ReferenceConfig(); 28 rc.setMonitor(monitorConfig()); 29 return rc; 30 } 31 32 @Bean 33 public ProtocolConfig protocol() { 34 ProtocolConfig protocolConfig = new ProtocolConfig(); 35 protocolConfig.setPort(20880); 36 return protocolConfig; 37 } 38 39 @Bean 40 public ProviderConfig provider() { 41 ProviderConfig providerConfig = new ProviderConfig(); 42 providerConfig.setMonitor(monitorConfig()); 43 return providerConfig; 44 } 45 46 47 }
三、Dubbo生產者配置,須要繼承Dubbo基礎配置spring-boot
1 @Configuration 2 public class ExportServiceConfig extends DubboBaseConfig { 3 4 @Bean 5 public ServiceBean<Person> personServiceExport(Person person) { 6 ServiceBean<Person> serviceBean = new ServiceBean<Person>(); 7 serviceBean.setProxy("javassist"); 8 serviceBean.setVersion("myversion"); 9 serviceBean.setInterface(Person.class.getName()); 10 serviceBean.setRef(person); 11 serviceBean.setTimeout(5000); 12 serviceBean.setRetries(3); 13 return serviceBean; 14 } 15 16 }
四、Dubbo消費者配置,須要繼承Dubbo基礎配置spa
@Configuration public class ReferenceConfig extends DubboBaseConfig { @Bean public ReferenceBean<Person> person() { ReferenceBean<Person> ref = new ReferenceBean<>(); ref.setVersion("myversion"); ref.setInterface(Person.class); ref.setTimeout(5000); ref.setRetries(3); ref.setCheck(false); return ref; } }
五、直接從Spring容器中拿去Person接口便可。