1、Maven依赖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version> 2.5 . 3.6 </version> <exclusions> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> <exclusion> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version> 0.1 </version> </dependency> |
2、DUBBO生产者注册到zookeeper的xml配置方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<?xml version= "1.0" encoding= "UTF-8" ?> <beans xmlns= "http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo= "http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans.xsd http: //code.alibabatech.com/schema/dubbo http: //code.alibabatech.com/schema/dubbo/dubbo.xsd "> <!-- 具体的实现bean --> <bean id= "demoService" class = "com.unj.dubbotest.provider.impl.DemoServiceImpl" /> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name= "xixi_provider" /> <!-- 使用multicast广播注册中心暴露服务地址 <dubbo:registry address= "multicast://224.5.6.7:1234" />--> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address= "zookeeper://127.0.0.1:2181" /> <!-- 用dubbo协议在 20880 端口暴露服务 --> <dubbo:protocol name= "dubbo" port= "20880" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface = "com.unj.dubbotest.provider.DemoService" version= "mys" ref= "demoService" /> </beans> |
3、DUBBO消费者注册到zookeeper的xml配置方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?xml version= "1.0" encoding= "UTF-8" ?> <beans xmlns= "http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo= "http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans.xsd http: //code.alibabatech.com/schema/dubbo http: //code.alibabatech.com/schema/dubbo/dubbo.xsd "> <!-- 消费者应用信息,用于提供依赖关系 --> <dubbo:application name= "consumer-of-helloworld-app" /> <!-- 注册地址,用于消费者寻找服务 --> <dubbo:registry protocol= "zookeeper" address= "127.0.0.1:2181,10.128.3.33:2181" /> <dubbo:consumer timeout= "5000" /> <!-- 引用的服务 --> <dubbo:reference id= "demoService" interface = "com.unj.dubbotest.provider.DemoService" version= "mys" /> </beans> |
二、在Spring Boot中使用Dubbo
在Spring Boot中使用Dubbo,不需要使用xml的方式来配置生产者和消费者,需要使用@Bean注解的方式来进行配置。
1、Maven依赖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version> 1.2 . 5 .RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version> 1.2 . 5 .RELEASE</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version> 2.5 . 3.6 </version> <exclusions> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> <exclusion> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version> 0.1 </version> </dependency> |
2、Dubbo基础配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
public class DubboBaseConfig { @Bean public RegistryConfig registry() { RegistryConfig registryConfig = new RegistryConfig(); registryConfig.setAddress( "127.0.0.1:2181" ); registryConfig.setProtocol( "zookeeper" ); return registryConfig; } @Bean public ApplicationConfig application() { ApplicationConfig applicationConfig = new ApplicationConfig(); applicationConfig.setName( "testApp" ); return applicationConfig; } @Bean public MonitorConfig monitorConfig() { MonitorConfig mc = new MonitorConfig(); mc.setProtocol( "registry" ); return mc; } @Bean public ReferenceConfig referenceConfig() { ReferenceConfig rc = new ReferenceConfig(); rc.setMonitor(monitorConfig()); return rc; } @Bean public ProtocolConfig protocol() { ProtocolConfig protocolConfig = new ProtocolConfig(); protocolConfig.setPort( 20880 ); return protocolConfig; } @Bean public ProviderConfig provider() { ProviderConfig providerConfig = new ProviderConfig(); providerConfig.setMonitor(monitorConfig()); return providerConfig; } } |
3、Dubbo生产者配置,需要继承Dubbo基础配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@Configuration public class ExportServiceConfig extends DubboBaseConfig { @Bean public ServiceBean<Person> personServiceExport(Person person) { ServiceBean<Person> serviceBean = new ServiceBean<Person>(); serviceBean.setProxy( "javassist" ); serviceBean.setVersion( "myversion" ); serviceBean.setInterface(Person. class .getName()); serviceBean.setRef(person); serviceBean.setTimeout( 5000 ); serviceBean.setRetries( 3 ); return serviceBean; } } |
4、Dubbo消费者配置,需要继承Dubbo基础配置
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@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接口即可。
总结
以上所述是小编给大家介绍的Dubbo在Spring和Spring Boot中的使用详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:http://www.cnblogs.com/cksvsaaa/p/6019393.html