spring-cloud-dubbo-demo
com.jihu.dubbo
1.0-SNAPSHOT
4.0.0
spring-cloud-dubbo-provider-user
com.alibaba.cloud
spring-cloud-starter-dubbo
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
com.jihu.dubbo
spring-cloud-dubbo-api
1.0-SNAPSHOT
compile
mysql
mysql-connector-java
runtime
5.1.49
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.0.0
org.springframework.boot
spring-boot-maven-plugin
pom依赖:
spring-cloud-dubbo-demo
com.jihu.dubbo
1.0-SNAPSHOT
4.0.0
spring-cloud-dubbo-provider-user
com.alibaba.cloud
spring-cloud-starter-dubbo
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
com.jihu.dubbo
spring-cloud-dubbo-api
1.0-SNAPSHOT
compile
mysql
mysql-connector-java
runtime
5.1.49
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.0.0
org.springframework.boot
spring-boot-maven-plugin
User:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {
private Integer id;
private String name;
}
UserService:
public interface UserService {
List list();
User getById(Integer id);
}
引入pom依赖:
spring-cloud-dubbo-demo
com.jihu.dubbo
1.0-SNAPSHOT
4.0.0
spring-cloud-dubbo-provider-user
com.alibaba.cloud
spring-cloud-starter-dubbo
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
com.jihu.dubbo
spring-cloud-dubbo-api
1.0-SNAPSHOT
compile
mysql
mysql-connector-java
runtime
5.1.49
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.0.0
org.springframework.boot
spring-boot-maven-plugin
application.yml
spring:
application:
name: spring-cloud-dubbo-provider-user
main:
# Spring Boot2.1及更高的版本需要设定
allow-bean-definition-overriding: true
cloud:
nacos:
discovery:
server-addr: 192.168.131.172:8848
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/spring-cloud-alibaba-test?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
username: root
password: mysqladmin
dubbo:
scan:
# 指定 Dubbo 服务实现类的扫描基准包
base-packages: com.jihu.mall.user.service
# 应用名不用在dubbo这里配置了,因为spring上已经配置了
protocol:
name: dubbo
# dubbo 协议端口( -1 表示自增端口,从 20880 开始)
port: -1
# 也不需要dubbo自己在注册服务了,直接使用springCloud中的nacos注册
UserMapper:
@Mapper
public interface UserMapper {
@Select("select * from user")
List list();
@Select("select * from user where id=#{id}")
User getById(Integer id);
}
UserServiceImpl:
@DubboService
@Slf4j
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List list() {
log.info("查询user列表");
return userMapper.list();
}
@Override
public User getById(Integer id) {
return userMapper.getById(id);
}
}
SpringCloudDubboProviderUserApplication:
@SpringBootApplication
public class SpringCloudDubboProviderUserApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudDubboProviderUserApplication.class, args);
}
}
引入pom依赖:
spring-cloud-dubbo-demo
com.jihu.dubbo
1.0-SNAPSHOT
4.0.0
spring-cloud-dubbo-consumer-user
org.springframework.boot
spring-boot-starter-web
com.alibaba.cloud
spring-cloud-starter-dubbo
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
com.jihu.dubbo
spring-cloud-dubbo-api
1.0-SNAPSHOT
compile
org.springframework.boot
spring-boot-maven-plugin
application.yml:
spring:
application:
name: spring-cloud-dubbo-consumer-user
main:
# Spring Boot2.1及更高的版本需要设定
allow-bean-definition-overriding: true
cloud:
nacos:
# Nacos 服务发现与注册配置
discovery:
server-addr: 192.168.131.172:8848
dubbo:
cloud:
# 指定需要订阅的服务提供方,默认值*,会订阅所有服务,不建议使用
subscribed-services: spring-cloud-dubbo-provider-user
# 服务名称也不需要指定了,因为spring中已经指定了
# application:
# name: ${spring.application.name}
protocol:
# dubbo 协议
name: dubbo
# dubbo 协议端口( -1 表示自增端口,从 20880 开始)
port: -1
# 不需要再单独注册dubbo服务了,此时直接使用nacos注册
# registry:
# #挂载到 Spring Cloud 注册中心 高版本可选
# address: spring-cloud://127.0.0.1:8848
当应用使用属性dubbo.cloud.subscribed-services为默认值时,日志中将会输出警告:
UserConstroller:
@RestController
@RequestMapping("/user")
public class UserConstroller {
@DubboReference
private UserService userService;
@RequestMapping("/info/{id}")
public User info(@PathVariable("id") Integer id){
return userService.getById(id);
}
@RequestMapping("/list")
public List list(){
return userService.list();
}
}
SpringCloudDubboConsumerUserApplication:
@SpringBootApplication
public class SpringCloudDubboConsumerUserApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudDubboConsumerUserApplication.class, args);
}
}
启动服务之后,去nacos检查服务注册:
可以看到,此时两个服务都已经注册到nacos上了。我们不必再像单独使用dubbo的那样,去配置注册。
测试:http://localhost:8080/user/list
到此,dubbo和springCloud的整合已经完成。
我们可以发现,感觉dubbo的rpc调用好像要比open feign更加的简单。我们甚至都不需要创建feign接口,直接使用@DubboReference注解即可引入一个第三方接口!
Dubbo Spring Cloud 提供了方案,即 @DubboTransported 注解,支持在类,方法,属性上使用。能够帮助服务消费端的 Spring Cloud Open Feign 接口以及 @LoadBalanced RestTemplate Bean 底层走 Dubbo 调用(可切换 Dubbo 支持的协议),而服务提供方则只需在原有 @RestController 类上追加 Dubbo @Servce 注解(需要抽取接口)即可,换言之,在不调整 Feign 接口以及 RestTemplate URL 的前提下,实现无缝迁移。
@Slf4j
@DubboService
@RestController
@RequestMapping("/user")
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
@RequestMapping("/list")
public List list() {
log.info("查询user列表");
return userMapper.list();
}
@Override
@RequestMapping("/getById/{id}")
public User getById(Integer id) {
return userMapper.getById(id);
}
}
org.springframework.boot
spring-boot-starter-web
com.alibaba.cloud
spring-cloud-starter-dubbo
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
org.springframework.cloud
spring-cloud-starter-openfeign
com.jihu.dubbo
spring-cloud-dubbo-api
1.0-SNAPSHOT
compile
@SpringBootApplication
@EnableFeignClients
public class SpringCloudDubboConsumerUserApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudDubboConsumerUserApplication.class, args);
}
}
@FeignClient(value = "spring-cloud-dubbo-provider-user-feign",path = "/user")
@DubboTransported(protocol = "dubbo")
public interface UserDubboFeignService {
@RequestMapping("/list")
public List list();
@RequestMapping("/getById/{id}")
public User getById(@PathVariable("id") Integer id);
}
// 添加@DubboService注解,将其同时注册成一个dubbo服务
@DubboService
@RestController
@RequestMapping("/user")
public class UserConstroller {
@DubboReference
private UserService userService;
@RequestMapping("/info/{id}")
public User info(@PathVariable("id") Integer id){
return userService.getById(id);
}
@RequestMapping("/list")
public List list(){
return userService.list();
}
}