spring boot集成dubbo,Spring boot +Dubbo,简易的配置方式
刚做完一个基于motan的项目不久,便去看看dubbo的新特性了,dubbo自上年9月恢复更新到现在大概半年多,发现已经有和spring boot集成的配置了。个人喜欢的配置方式优先级一般都是资源文件>Bean>xml,因感觉而言Bean的配置方式更适合让人了解源码,而资源文件则是最简便,看了下还好之前写dubbo的demo时没有写文章,因为我一般都比较喜欢写可以通过最简易的方式达到目的的demo文章,而且也不会也重复的文章,如果之前写了就只有通过Bean配置而没有application资源文件配置dubbo的文章了。
该文章主要参考自 dubbo官方Demo,dubbo的基本特性可以看用户指南,dubbo的概念可以看用户指南的第一章入门即可,我就不转述了,想要在dubbo基础上扩展的可以看开发指南。建议可以先尝试自己根据官方去搭一下,出问题了可以再对比一下本文章代码找出问题,成功了就可以省略代码部分阅览了。
现在可以通过添加dubbo-spring-boot-starter依赖实现dubbo与spring boot的整合,简化dubbo的配置,具体文档如下图:
文档所属项目是dubbo-spring-boot-parent,由于0.2.x版本还没正式release,所以该demo用的依旧是0.1.0的依赖,但还未发现与Spring boot 2.0的版本继承有问题。
个人demo分了3个模块-consumer、provider、service,这3个模块的父模块则是dubbo-motan。service里存放服务接口和POJO(按正常划分POJO该新建一个entity模块存放,但demo随便了一下),provider存放DAO、service接口实现暴露service,consumer存放controller(controller从暴露服务的位置获取所需服务,如consule、zookeeper为分布式服务进行管理的注册中心,也可以是特定url)。dubbo-motan的maven依赖如下:
dubbo-service-demo模块存服务接口与POJO:
package per.dubbo.demo.postgres.service; import per.dubbo.demo.postgres.model.Student; import com.baomidou.mybatisplus.service.IService; /** * @author Wilson * @since 2018-05-25 */ public interface StudentService extends IService<Student> { }
dubbo-provider-demo存放服务、DAO与dubbo配置资源文件properties.yml(@Service需用dubbo的):
StudentServiceImpl:
package per.dubbo.demo.postgres.service.impl; import com.alibaba.dubbo.config.annotation.Service; import com.baomidou.mybatisplus.service.impl.ServiceImpl; import per.dubbo.demo.postgres.dao.StudentDAO; import per.dubbo.demo.postgres.model.Student; import per.dubbo.demo.postgres.service.StudentService; import javax.annotation.Resource; /** * @author Wilson * @since 2018-05-25 */ @Service public class StudentServiceImpl extends ServiceImpl<StudentDAO, Student> implements StudentService { @Resource private StudentDAO studentDAO; }
dubbo-provider-demo模块下的application.xml:
server: port: 8081 spring: application: name: provider-demo dubbo: scan: base-packages: per.dubbo.demo.postgres.service.impl application: name: dubbo-provider-demo id: dubbo-provider-demo protocol: id: dubbo name: dubbo port: 33333 registry: address: multicast://224.5.6.7:1234 check: false
启动进程ProviderApplicationDemo(@EnableDubbo同@DubboComponentScan与@EnableDubboConfig,虽然application.yml已配置了扫描包但实际启动却没有起效,大概是我使用的spring boot版本是2.0但dubbo-spring-boot是0.1.x的原因,所以要加@EnableDubbo才能起效):
package per.dubbo.demo.postgres; import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; import io.swagger.annotations.Api; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * ProviderApplicationDemo * * @author Wilson * @date 18-4-12 */ @Api("ProviderApplicationDemo") @EnableDubbo @SpringBootApplication public class ProviderApplicationDemo { public static void main(String[] args) { SpringApplication.run(ProviderApplicationDemo.class); } }
dubbo-consumer-demo存放controller:
package per.dubbo.demo.controller.impl; import com.alibaba.dubbo.config.annotation.Reference; import org.springframework.web.bind.annotation.RestController; import per.dubbo.demo.common.ServerResponse; import per.dubbo.demo.controller.UserBaseController; import per.dubbo.demo.postgres.service.StudentService; import reactor.core.publisher.Mono; /** * UserBaseControllerImpl * * @author Wilson * @date 18-5-25 */ @RestController public class UserBaseControllerImpl implements UserBaseController { @Reference private StudentService studentService; @Override public Mono<ServerResponse> login(String username, String password) { return Mono.just(ServerResponse.ok()); } @Override public Mono<ServerResponse> list() { return Mono.just(ServerResponse.ok(studentService.selectList(null))); } }
启动程序ConsumerDemoApplication(@PostConstruct在这只是用来判断有没有成功发现服务):
package per.dubbo.demo; import com.alibaba.dubbo.config.annotation.Reference; import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import per.dubbo.demo.postgres.service.StudentService; import javax.annotation.PostConstruct; /** * ConsumerDemoApplication * * @author Wilson * @date 18-5-15 */ @SpringBootApplication @DubboComponentScan public class ConsumerDemoApplication { @Reference private StudentService studentService; @PostConstruct public void init() { System.err.println("studentService:" + studentService); } public static void main(String[] args) { SpringApplication.run(ConsumerDemoApplication.class); } }
consumer配置文件application.yml:
spring: application: name: dubbo-consumer-demo server: port: 7979 dubbo: consumer: check: false application: id: dubbo-consumer name: dubbo-consumer protocol: id: consumer name: consumer port: 33333 registry: address: multicast://224.5.6.7:1234 check: false scan: base-packages: per.dubbo.demo.controller
项目起跑时都可以看到控制台输出application.yml的dubbo配置信息,如下图:
这一切都部署好后就把每一个Consumer模块都当作一个系统开发就好了,其它无关dubbo配置的代码就不贴浪费位置了,毕竟最重要的还是思想。分布式就相当于把一个大系统划分为一个个小系统进行开发,而dubbo就是划分的代码实现。如电商就可分为用户Consumer、用户Provider、商品Consumer、商品Provider、订单Consumer、订单Provider等等,不同的consumer可以放到一个consumer大模块进行管理,provider丢到一个provider大模块进行管理,按照该文章里的例子则dubbo-demo则为所有模块的祖宗,根据功能划分存放common、provider-demo、consumer-demo、service-demo、entity等子模块,consumer-demo里放user-consumer、order-consumer、store-consumer等子模块,provider-demo放user-provider、order-provider、store-provider等子模块