1、pom.xml依赖

  1. <!--dubbo-->
  2. <dependency>
  3. <groupId>com.alibaba.cloud</groupId>
  4. <artifactId>spring-cloud-starter-dubbo</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.alibaba.cloud</groupId>
  8. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  9. </dependency>

2、application.properties

  1. #端口配置
  2. server.port=38001
  3. #指定dubbo要提供服务类的所在包
  4. #dubbo.scan.base-packages=com.seven.base.web.dubbo
  5. dubbo.scan.base-packages=com.seven.base.**
  6. #指定订阅那个微服务,默认*代表订阅所有的服务,none代表不订阅
  7. dubbo.cloud.subscribed-services=sever-web
  8. #协议,dubbo
  9. dubbo.protocol.name=dubbo
  10. #协议端口,-1表示自增端口,从20880开始
  11. dubbo.protocol.port=-1
  12. ###################################################################################
  13. ###############这里dubbo注册中心的配置建议用nacos.discovery的配置方式################
  14. ###dubbo注册中心的配置方式会导致nacos控制面板中出现很多带有元数据的服务,看起来会很乱###
  15. #注册中心地址
  16. dubbo.registry.address=seven.nacos.xxx.com
  17. #注册中心用户名
  18. dubbo.registry.username=nacos
  19. #注册中心密码
  20. dubbo.registry.password=nacos
  21. #注册中心namespace
  22. dubbo.registry.parameters.namespace=dee14bb7-eef0-43de-81f9-40912291df3c
  23. #########################################################################################
  24. ##建议用nacos.discovery的配置方式,本身微服务工程便会配置注册中心,dubbo注册中心配置便可省去###
  25. #########################################################################################
  26. spring.cloud.nacos.discovery.server-addr=seven.nacos.xxx.com
  27. spring.cloud.nacos.discovery.namespace=dee14bb7-eef0-43de-81f9-40912291df3c

3、编写公共类

User实体对象类

  1. @EqualsAndHashCode(callSuper = false)
  2. @ApiModel(value = "User对象", description = "用户表")
  3. @Data
  4. @ToString
  5. public class User implements Serializable {
  6. private static final long serialVersionUID = 1L;
  7. @TableId(type = IdType.ASSIGN_ID)
  8. private Long id;
  9. @ApiModelProperty(value = "用户名")
  10. @TableField("username")
  11. private String username;
  12. @ApiModelProperty(value = "邮箱")
  13. @TableField("email")
  14. private String email;
  15. }

rpc统一返回格式

  1. public class DubboResponseEntity <T> implements Serializable {
  2. private static final long serialVersionUID = -7885008698125769891L;
  3. private T data;
  4. public T getData() {
  5. return data;
  6. }
  7. public DubboResponseEntity() {}
  8. public DubboResponseEntity(T data) {
  9. this.data = data;
  10. }
  11. public static DubboResponseEntity setData(Object data){
  12. return new DubboResponseEntity (data);
  13. }
  14. }

dubbo接口类

  1. public interface IWebService {
  2. DubboResponseEntity<User> selectById(Long userId);
  3. }

4、服务提供者

启动类

  1. @SpringBootApplication
  2. @EnableDiscoveryClient
  3. @EnableDubbo
  4. @MapperScan("com.seven.base.web.business.*.mapper")
  5. public class ServerWebApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(ServerWebApplication.class,args);
  8. }
  9. }

暴露服务,实现接口

  1. @DubboService
  2. public class WebServiceProviderImpl implements IWebService {
  3. @Resource
  4. IUserService userService;
  5. @Override
  6. public DubboResponseEntity<User> selectById(Long userId) {
  7. return new DubboResponseEntity<>(userService.selectById(userId));
  8. }
  9. }

IUserService和UserImpl

  1. public interface IUserService {
  2. User selectById(Long userId);
  3. }
  4. @Service
  5. public class UserImpl implements IUserService {
  6. @Resource
  7. UserMapper userMapper;
  8. @Override
  9. public User selectById(Long userId) {
  10. return userMapper.selectById(userId);
  11. }
  12. }

5、服务消费者

启动类

  1. @SpringBootApplication
  2. @EnableDiscoveryClient
  3. @EnableDubbo
  4. public class ServerAuthApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(ServerAuthApplication.class, args);
  7. }
  8. }

消费者

  1. @Service
  2. public class WebServiceConsumer {
  3. @DubboReference
  4. public IWebService webService;
  5. public DubboResponseEntity<User> selectById(Long userId){
  6. return webService.selectById(userId);
  7. }
  8. }

controller

  1. @RestController
  2. @RequestMapping("/v1/auth")
  3. @Api(tags = {"登录接口"})
  4. public class LoginController {
  5. @Resource
  6. WebServiceConsumer webServiceConsumer;
  7. @PostMapping("/login")
  8. @ApiOperation(value = "登录接口")
  9. public DubboResponseEntity<User> login(@RequestParam("userId")Long userId) {
  10. return webServiceConsumer.selectById(userId);
  11. }
  12. }