1、pom.xml依赖
<!--dubbo-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
2、application.properties
#端口配置
server.port=38001
#指定dubbo要提供服务类的所在包
#dubbo.scan.base-packages=com.seven.base.web.dubbo
dubbo.scan.base-packages=com.seven.base.**
#指定订阅那个微服务,默认*代表订阅所有的服务,none代表不订阅
dubbo.cloud.subscribed-services=sever-web
#协议,dubbo
dubbo.protocol.name=dubbo
#协议端口,-1表示自增端口,从20880开始
dubbo.protocol.port=-1
###################################################################################
###############这里dubbo注册中心的配置建议用nacos.discovery的配置方式################
###dubbo注册中心的配置方式会导致nacos控制面板中出现很多带有元数据的服务,看起来会很乱###
#注册中心地址
dubbo.registry.address=seven.nacos.xxx.com
#注册中心用户名
dubbo.registry.username=nacos
#注册中心密码
dubbo.registry.password=nacos
#注册中心namespace
dubbo.registry.parameters.namespace=dee14bb7-eef0-43de-81f9-40912291df3c
#########################################################################################
##建议用nacos.discovery的配置方式,本身微服务工程便会配置注册中心,dubbo注册中心配置便可省去###
#########################################################################################
spring.cloud.nacos.discovery.server-addr=seven.nacos.xxx.com
spring.cloud.nacos.discovery.namespace=dee14bb7-eef0-43de-81f9-40912291df3c
3、编写公共类
User实体对象类
@EqualsAndHashCode(callSuper = false)
@ApiModel(value = "User对象", description = "用户表")
@Data
@ToString
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(type = IdType.ASSIGN_ID)
private Long id;
@ApiModelProperty(value = "用户名")
@TableField("username")
private String username;
@ApiModelProperty(value = "邮箱")
@TableField("email")
private String email;
}
rpc统一返回格式
public class DubboResponseEntity <T> implements Serializable {
private static final long serialVersionUID = -7885008698125769891L;
private T data;
public T getData() {
return data;
}
public DubboResponseEntity() {}
public DubboResponseEntity(T data) {
this.data = data;
}
public static DubboResponseEntity setData(Object data){
return new DubboResponseEntity (data);
}
}
dubbo接口类
public interface IWebService {
DubboResponseEntity<User> selectById(Long userId);
}
4、服务提供者
启动类
@SpringBootApplication
@EnableDiscoveryClient
@EnableDubbo
@MapperScan("com.seven.base.web.business.*.mapper")
public class ServerWebApplication {
public static void main(String[] args) {
SpringApplication.run(ServerWebApplication.class,args);
}
}
暴露服务,实现接口
@DubboService
public class WebServiceProviderImpl implements IWebService {
@Resource
IUserService userService;
@Override
public DubboResponseEntity<User> selectById(Long userId) {
return new DubboResponseEntity<>(userService.selectById(userId));
}
}
IUserService和UserImpl
public interface IUserService {
User selectById(Long userId);
}
@Service
public class UserImpl implements IUserService {
@Resource
UserMapper userMapper;
@Override
public User selectById(Long userId) {
return userMapper.selectById(userId);
}
}
5、服务消费者
启动类
@SpringBootApplication
@EnableDiscoveryClient
@EnableDubbo
public class ServerAuthApplication {
public static void main(String[] args) {
SpringApplication.run(ServerAuthApplication.class, args);
}
}
消费者
@Service
public class WebServiceConsumer {
@DubboReference
public IWebService webService;
public DubboResponseEntity<User> selectById(Long userId){
return webService.selectById(userId);
}
}
controller
@RestController
@RequestMapping("/v1/auth")
@Api(tags = {"登录接口"})
public class LoginController {
@Resource
WebServiceConsumer webServiceConsumer;
@PostMapping("/login")
@ApiOperation(value = "登录接口")
public DubboResponseEntity<User> login(@RequestParam("userId")Long userId) {
return webServiceConsumer.selectById(userId);
}
}