原文地址:
博客地址: 欢迎转载,转载请注明作者及出处,谢谢!
SpringBoot整合Dubbo2.5.10,使用官方最新spring-boot-starter
开始
Dubbo已经进入了Apache孵化器,并且发布了官方的spring-boot-starter0.1.0,用于简化dubbo应用的配置,主要包括了autoconfigure(自动装配),externalized-configuration(外部化配置),actuator(生产准备)等,可参考官方github .
准备工作
需要提前安装好JDK1.8,Maven,Zookeeper。
初始化Maven项目
为了整个项目结构清晰,使用模块化的maven项目。pom文件如下:
复制代码 org.springframework.boot spring-boot-starter-parent 1.5.8.RELEASE 4.0.0 org.boot.dubbo boot-dubbo 1.0.0-SNAPSHOT pom boot-dubbo Dubbo project for Spring Boot dubbo-provider dubbo-consumer dubbo-api UTF-8 UTF-8 1.8 0.1.0 1.5.8.RELEASE 1.2.31 0.2 1.4.3 com.alibaba.boot dubbo-spring-boot-starter ${dubbo-spring-boot-starter.version} org.springframework.boot spring-boot-starter-logging ${springboot.version} com.101tec zkclient ${zk-client.version}
主要分为三个模块,api,provider和consumer
创建生产者
有了spring-boot-starter,dubbo的配置变得非常简单,再也不用像以前一样配置一大堆xml文件,只需要几个简单的配置,就可以做到开箱即用。
- 先配置生产者的pom.xml
复制代码 4.0.0 org.boot.dubbo dubbo-provider ${parent.version} jar boot-dubbo org.boot.dubbo 1.0.0-SNAPSHOT ../pom.xml dubbo-provider Dubbo project for Spring Boot:Provider org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-actuator org.springframework.boot spring-boot-starter-logging org.springframework.boot spring-boot-starter-test com.alibaba.boot dubbo-spring-boot-starter zookeeper org.apache.zookeeper com.101tec zkclient org.projectlombok lombok org.boot.dubbo dubbo-api ${parent.version} org.springframework.boot spring-boot-maven-plugin
- 接着使用properties进行SpringBoot和Dubbo的配置,配置如下:
spring.application.name=springboot-dubbo-provider server.port=9090 #dubbo配置 dubbo.application.id=springboot-dubbo-provider dubbo.application.name=springboot-dubbo-provider dubbo.application.owner=luoliang #协议配置 dubbo.protocol.id=dubbo dubbo.protocol.name=dubbo #把默认的20880端口换成12345 dubbo.protocol.port=12345 #服务注册配置 dubbo.registry.id=my-registry dubbo.registry.address=zookeeper://localhost:2181 #配置dubbo的包扫描,针对dubbo的@Service, @Reference注解 dubbo.scan.base-packages=org.boot.dubbo.provider.service #dubbo健康监控 endpoints.dubbo.enabled=true management.health.dubbo.status.defaults=memory management.health.dubbo.status.extras=load,threadpool management.port=9091复制代码
- 进行了上面两步之后,Dubbo已经集成好了,接下来就可以直接开始撸服务代码了,可以直接使用注解来暴露服务接口
- 先在api中写一个interface
public interface HelloService { String sayHello(String name);}复制代码
- 实现接口,加上自己的业务逻辑
@Service(version = "1.0.0", application = "${dubbo.application.id}", protocol = "${dubbo.protocol.id}", registry = "${dubbo.registry.id}")public class HelloServiceImpl implements HelloService { @Override public String sayHello(String name) { return "Hello, " + name + " (from Spring Boot)"; }}复制代码
注意,这里的service注解是com.alibaba.dubbo.config.annotation.Service
创建消费者
- 配置消费者者的pom.xml
复制代码 4.0.0 org.boot.dubbo dubbo-consumer ${parent.version} jar boot-dubbo org.boot.dubbo 1.0.0-SNAPSHOT ../pom.xml dubbo-consumer Dubbo project for Spring Boot:Consumer org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-actuator org.springframework.boot spring-boot-starter-test org.springframework.boot spring-boot-starter-logging com.alibaba.boot dubbo-spring-boot-starter com.101tec zkclient org.projectlombok lombok org.boot.dubbo dubbo-api ${parent.version} org.springframework.boot spring-boot-maven-plugin
- application.properties配置如下:
spring.application.name=springboot-dubbo-consumerserver.port=8081#dubbo配置dubbo.application.id=springboot-dubbo-consumerdubbo.application.name=springboot-dubbo-consumerdubbo.application.owner=luoliang#服务注册配置dubbo.registry.id=my-registrydubbo.registry.address=zookeeper://localhost:2181management.port=8082复制代码
- 编写service来消费dubbo的服务,主要代码如下:
@Servicepublic class ConsumerServiceImpl implements ConsumerService { @Reference(version = "1.0.0", application = "${dubbo.application.id}") private HelloService helloService; @Override public String sayHello(String name) { return helloService.sayHello(name); }}复制代码
在mvc的controller中注入此服务复制代码
@RestController@RequestMapping("/user")public class DefaultController { @Resource private ConsumerService consumerService; @RequestMapping("/sayHello") public String register(String name) { return consumerService.sayHello(name); }}复制代码
到这里,整个项目基本结构已经搭建完成,consumer已经能够消费provider提供的服务。
现在来测试一下,分别启动provider和consumer,打开浏览器,输入http://localhost:8081/user/sayHello?name=dubbo
可以看到,返回的结果和预期一样,说明项目已经成功集成
需要源码请移步本人github,如果能顺手star就更好啦! ,博客接这篇博客,学习使用Docker容器化SpringBoot+Dubbo应用。
第一次在掘金发文,各位大佬轻喷~
参考
- https://github.com/apache/incubator-dubbo-spring-boot-project