项目结构: |-- spring-boot-dubbo-demo (父级工程) |-- spring-boot-dubbo-base (基础工程) |-- spring-boot-dubbo-consumer (消费者) |-- spring-boot-dubbo-provider (生产者) 复制代码
SpringBoot版本:2.2.0
Dubbo版本:2.7.0
Nacos版本:1.1.4
Nacos官方文档: nacos.io/zh-cn/docs/…
默认账号密码是nacos
Consumer和Provider的Maven依赖如下:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Base依赖 --> <dependency> <groupId>com.sans</groupId> <artifactId>spring-boot-dubbo-base</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!-- Dubbo依赖 --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>2.7.0</version> </dependency> <!-- Nacos依赖 --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-registry-nacos</artifactId> <version>2.7.1</version> </dependency> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> <version>1.0.0</version> </dependency> </dependencies> 复制代码
Consumer配置如下:
# 配置端口 server: port: 8862 dubbo: # 配置服务信息 application: name: dubbo-consumer # 禁用QOS同一台机器可能会有端口冲突现象 qos-enable: false qos-accept-foreign-ip: false # 配置注册中心 registry: address: nacos://127.0.0.1:8848 # 设置超时时间 consumer: timeout: 4000 spring: main: # 解决Bean重复定义问题 allow-bean-definition-overriding: true 复制代码
Provider配置如下:
# 配置端口 server: port: 8861 dubbo: # 配置服务信息 application: name: dubbo-provider # 禁用QOS同一台机器可能会有端口冲突现象 qos-enable: false qos-accept-foreign-ip: false # 配置注册中心 registry: address: nacos://127.0.0.1:8848 # 设置协议-协议由提供方指定消费方被动接受 protocol: name: dubbo port: 20880 spring: main: # 解决Bean重复定义问题 allow-bean-definition-overriding: true 复制代码
编写DTO
/** * RPC接口DTO * 注意这里要实现序列化接口 * @Author Sans * @CreateTime 2019/11/6 23:04 */ @Data public class ProviderTestDTO implements Serializable { // ID private int id; // 名字 private String name; // 序号 private Integer number; } 复制代码
编写Serivce
/** * RPC接口 * @Author Sans * @CreateTime 2019/11/6 23:03 */ public interface IProviderService { List<ProviderTestDTO> queryList(); } 复制代码
编写返回结果类
/** * 返回结果类 * 这里采用构建者模式构建 * 优点:1.私有化构造器访问范围小 2.参数可灵活设置便于管理 * @Author Sans * @CreateTime 2019/11/7 18:59 */ @Getter public class ResultVO<T> implements Serializable { /** * 返回码 */ private int code; /** * 返回信息 */ private String message; /** * 返回数据 */ private T data; /** 私有化构造器 **/ private ResultVO() {} private ResultVO(ResultVO<T> resultVO) { this.code = resultVO.code; this.message = resultVO.message; this.data = resultVO.data; } /** * Build */ public static class Builder<T>{ private ResultVO<T> resultVO; public Builder() { resultVO = new ResultVO<>(); } public Builder code(int code){ resultVO.code = code; return this; } public Builder message(String message){ resultVO.message = message; return this; } public Builder data(T data){ resultVO.data = data; return this; } public ResultVO<T> build(){ return new ResultVO<>(resultVO); } } } 复制代码
在启动类上面不要忘记加上@EnableDubbo注解
@EnableDubbo //开启Dubbo的注解支持 @SpringBootApplication public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } } 复制代码
实现IProviderService接口,注意这里的Serivce引用的是dubbo的包
/** * 生产者Dubbo接口实现 * @Author Sans * @CreateTime 2019/11/6 23:01 */ @Service public class ProviderServiceImpl implements IProviderService { @Override public List<ProviderTestDTO> queryList() { // 初始化数据 ProviderTestDTO testDTO1 = new ProviderTestDTO(); testDTO1.setId(1); testDTO1.setName("学生"); testDTO1.setNumber(100); ProviderTestDTO testDTO2 = new ProviderTestDTO(); testDTO2.setId(2); testDTO2.setName("教师"); testDTO2.setNumber(101); // 组装数据 List<ProviderTestDTO> list = new ArrayList<>(); list.add(testDTO1); list.add(testDTO2); return list; } } 复制代码
和Provider工程的启动类一样,加上@EnableDubbo注解
@EnableDubbo //开启dubbo的注解支持 @SpringBootApplication public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } } 复制代码
编写测试接口
/** * 消费测试接口 * @Author Sans * @CreateTime 2019/11/6 23:09 */ @RestController @RequestMapping("/consumer") public class ConsumerController { // Dubbo远程调用注解 @Reference private IProviderService providerService; @RequestMapping(value = "/list",method = RequestMethod.GET) public ResultVO getList(){ // 远程调用 List<ProviderTestDTO> providerTestDTOList = providerService.queryList(); return new ResultVO.Builder<>().code(200).message("success").data(providerTestDTOList).build(); } } 复制代码
使用Postman访问Consumer测试接口