spring boot集成dubbo,Spring boot +Dubbo,简易的配置方式

匿名 (未验证) 提交于 2019-12-03 00:22:01

dubbo官方Demo,dubbo的基本特性可以看用户指南,dubbo的概念可以看用户指南的第一章入门即可,我就不转述了,想要在dubbo基础上扩展的可以看开发指南。建议可以先尝试自己根据官方去搭一下,出问题了可以再对比一下本文章代码找出问题,成功了就可以省略代码部分阅览了。

文档所属项目是dubbo-spring-boot-parent,由于0.2.x版本还没正式release,所以该demo用的依旧是0.1.0的依赖,但还未发现与Spring boot 2.0的版本继承有问题。

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配置信息,如下图:


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!