文章目录
一、Spring Cloud整合Dubbo
父pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-dubbo-demo</artifactId>
<groupId>com.jihu.dubbo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-cloud-dubbo-provider-user</artifactId>
<dependencies>
<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>
<dependency>
<groupId>com.jihu.dubbo</groupId>
<artifactId>spring-cloud-dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>5.1.49</version>
</dependency>
<!-- mybatis自动配置依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1.1 spring-cloud-dubbo-api
pom依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-dubbo-demo</artifactId>
<groupId>com.jihu.dubbo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-cloud-dubbo-provider-user</artifactId>
<dependencies>
<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>
<dependency>
<groupId>com.jihu.dubbo</groupId>
<artifactId>spring-cloud-dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>5.1.49</version>
</dependency>
<!-- mybatis自动配置依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
User:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {
private Integer id;
private String name;
}
UserService:
public interface UserService {
List<User> list();
User getById(Integer id);
}
1.2 spring-cloud-dubbo-provider-user
引入pom依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-dubbo-demo</artifactId>
<groupId>com.jihu.dubbo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-cloud-dubbo-provider-user</artifactId>
<dependencies>
<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>
<dependency>
<groupId>com.jihu.dubbo</groupId>
<artifactId>spring-cloud-dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>5.1.49</version>
</dependency>
<!-- mybatis自动配置依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
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<User> 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<User> 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);
}
}
1.3 spring-cloud-dubbo-consumer-user
引入pom依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-dubbo-demo</artifactId>
<groupId>com.jihu.dubbo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-cloud-dubbo-consumer-user</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
<dependency>
<groupId>com.jihu.dubbo</groupId>
<artifactId>spring-cloud-dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
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<User> 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注解即可引入一个第三方接口!
二、从Open Feign迁移到Dubbo
Dubbo Spring Cloud 提供了方案,即 @DubboTransported 注解,支持在类,方法,属性上使用。能够帮助服务消费端的 Spring Cloud Open Feign 接口以及 @LoadBalanced RestTemplate Bean 底层走 Dubbo 调用(可切换 Dubbo 支持的协议),而服务提供方则只需在原有 @RestController 类上追加 Dubbo @Servce 注解(需要抽取接口)即可,换言之,在不调整 Feign 接口以及 RestTemplate URL 的前提下,实现无缝迁移。
2.1 修改服务提供者
@Slf4j
@DubboService
@RestController
@RequestMapping("/user")
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
@RequestMapping("/list")
public List<User> list() {
log.info("查询user列表");
return userMapper.list();
}
@Override
@RequestMapping("/getById/{id}")
public User getById(Integer id) {
return userMapper.getById(id);
}
}
2.2 服务消费端引入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.jihu.dubbo</groupId>
<artifactId>spring-cloud-dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
2.3 feign的实现,消费端启动类上添加@EnableFeignClients
@SpringBootApplication
@EnableFeignClients
public class SpringCloudDubboConsumerUserApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudDubboConsumerUserApplication.class, args);
}
}
2.4 feign接口添加 @DubboTransported 注解
@FeignClient(value = "spring-cloud-dubbo-provider-user-feign",path = "/user")
@DubboTransported(protocol = "dubbo")
public interface UserDubboFeignService {
@RequestMapping("/list")
public List<User> list();
@RequestMapping("/getById/{id}")
public User getById(@PathVariable("id") Integer id);
}
2.5 调用对象添加 @DubboTransported 注解
// 添加@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<User> list(){
return userService.list();
}
}