springboot整合dubbo
本地测试环境:
dubbo 2.6.0
springboot 2.1.7
JDK 1.8
Zookeeper 3.5.5
pom关键依耐:
<!-- dubbo依赖 --> <dependency> <groupId>com.alibaba.spring.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <!-- zookeeper依赖 --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.5.5</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency> <!-- zkclient依赖 --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.11</version> </dependency>
1.provider(提供者)
1.1 启动类注解
@SpringBootApplication @EnableDubboConfiguration
1.2 服务实现类
#注意@Service为com.alibaba.dubbo.config.annotation.Service注解 @Service(version = "1.0.0", timeout = 10000, interfaceClass = EmpService.class) @Component
1.3 配置文件(yml)
server: port: 10088 #应用服务端口 spring: dubbo: application: #注册在注册中心的唯一标识名称 name: dubbo-provider-001 registry: #单机zookeeper中心 address: zookeeper://192.168.7.11:2181 protocol: #默认名称,勿修改 name: dubbo #暴露服务端口 (默认是20880,修改端口,不同的服务提供者端口不能重复) port: 20889 #服务暴露与发现消费所在的package scan: com.cchengyyj.emp.service
2.consumer(消费者)
2.1 启动类注解
@SpringBootApplication @EnableDubboConfiguration
2.2 控制器类主要注解
#Controller类 @Controller #注入属性,com.alibaba.dubbo.config.annotation.Reference注解,注意要指定版本 @Reference(version = "1.0.0") #控制器方法上 @RequestMapping(value = "/showAllEmps", method = RequestMethod.GET) @ResponseBody
2.3 配置文件
#server setting server: port: 10010 servlet: context-path: #dubbo spring: application: name: dubbo-consumer-001 dubbo: registry: zookeeper://192.168.7.11:2181
测试如果发现报:ERROR 411116 --- [ main] com.alibaba.dubbo.qos.server.Server : [DUBBO] qos-server can not bind localhost:22222, dubbo version: 2.6.0, current host: 127.0.0.1
由于在单机上启动多个springboot应用实例,解决方法在启动类中关闭QOS服务:
public class ApplicationConsumer { public static void main(String[] args) { //配置dubbo.qos.port端口 System.setProperty(Constants.QOS_PORT,"33333"); //配置dubbo.qos.accept.foreign.ip是否关闭远程连接 System.setProperty(Constants.ACCEPT_FOREIGN_IP,"false"); SpringApplication.run(ApplicationConsumer.class, args); //关闭QOS服务 Server.getInstance().stop(); } }
测试:
1.提供者
服务名: com.cchengyyj.emp.EmpService:1.0.0
服务地址: dubbo://192.168.7.1:20889/com.cchengyyj.emp.EmpService?anyhost=true&application=dubbo-provider-001&dubbo=2.6.0&generic=false&interface=com.cchengyyj.emp.EmpService&methods=findAll&pid=404380&revision=1.0.0&side=provider&timeout=10000×tamp=1567691986101&version=1.0.0
动态配置:
主机名: 192.168.7.1:20889
所属端: provider
应用名: dubbo-provider-001
方法列表: findAll
Dubbo版本: 2.6.0
进程号: 404380
接口名: com.cchengyyj.emp.EmpService
版本: 1.0.0
泛化: false
超时: 10000
绑定所有IP: true
修订版本: 1.0.0
2.消费者
服务名: com.cchengyyj.emp.EmpService:1.0.0
消费者地址: consumer://192.168.7.1/com.cchengyyj.emp.EmpService:1.0.0?application=dubbo-consumer-001&category=consumers&check=false&dubbo=2.6.0&interface=com.cchengyyj.emp.EmpService&methods=findAll&pid=411320&revision=1.0.0&side=consumer×tamp=1567692274835&version=1.0.0
动态配置:
主机名: 192.168.7.1
所属端: consumer
应用名: dubbo-consumer-001
方法列表: findAll
Dubbo版本: 2.6.0
进程号: 411320
检查: false
数据类型: consumers
接口名: com.cchengyyj.emp.EmpService
版本: 1.0.0
修订版本: 1.0.0