Dubbo在Spring和Spring Boot中的使用

一、在Spring中使用Dubbo

1、Maven依赖

 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>

2、DUBBO生产者注册到zookeeper的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  <!-- 具体的实现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>

3、DUBBO消费者注册到zookeeper的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="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>

 

二、在Spring Boot中使用Dubbo

在Spring Boot中使用Dubbo,不需要使用xml的方式来配置生产者和消费者,需要使用@Bean注解的方式来进行配置。

1、Maven依赖

 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>

2、Dubbo基础配置

 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 }

3、Dubbo生产者配置,需要继承Dubbo基础配置

 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 }

4、Dubbo消费者配置,需要继承Dubbo基础配置

@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;
    }
}

5、直接从Spring容器中拿去Person接口即可。

posted @ 2016-11-01 14:43  自在流云  阅读(39628)  评论(0编辑  收藏  举报