自己手写的60+工程的RPC框架成功整合了SpringCloud Alibaba

开发 前端
本章,我们更进一步将手写的bhrpc框架整合到SpringCloud Alibaba项目。

大家好,我是冰河~~

目前,我们自己手写的RPC框架已经完成了整体设计、服务提供者的实现、服务消费者的实现、注册中心的实现、负载均衡的实现、SPI扩展序列化机制、SPI扩展动态代理机制、SPI扩展反射机制、SPI扩展负载均衡策略、SPI扩展增强型负载均衡策略、SPI扩展实现注册中心、心跳机制、增强型心跳机制、重试机制、整合Spring、整合SpringBoot和整合Docker等篇章,共计80+篇文章。

本节,我们就基于《SpringCloud Alibaba》专栏的源码整合自己手写的bhrpc框架,替换掉原有项目中使用的Fegin框架。

1、新增shop-service-api工程

(1)新增shop-service-api工程

在父工程shop-springcloud-alibaba下新建shop-service-api子工程,并在shop-service-api子工程的pom.xml文件中添加如下配置。

<dependencies>
<dependency>
<groupId>io.binghe.shop</groupId>
<artifactId>shop-bean</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

shop-service-api子工程的作用就是将shop-user工程中的UserService接口和shop-product工程中的ProductService接口单独分离出来,便于后续整合bhrpc框架。

(2)新增UserService接口

UserService接口的源码详见:shop-service-api工程下的io.binghe.shop.service.UserService,如下所示。

public interface UserService {
/**
* 根据id获取用户信息
*/
User getUserById(Long userId);
}

删除shop-user工程下的io.binghe.shop.user.service.UserService接口,并修改shop-user工程中的报错信息,将报错类中原本依赖io.binghe.shop.user.service.UserService接口修改成依赖io.binghe.shop.service.UserService接口。

(3)新增ProductService接口

ProductService接口的源码详见:shop-service-api工程下的io.binghe.shop.service.ProductService,如下所示。

public interface ProductService {
/**
* 根据商品id获取商品信息
*/
Product getProductById(Long pid);
/**
* 扣减商品库存
*/
int updateProductStockById(Integer count, Long id);
}

删除shop-product工程下的io.binghe.shop.product.service.ProductService接口,并修改shop-product工程中的报错信息,将报错类中原本依赖io.binghe.shop.product.service.ProductService接口修改成依赖io.binghe.shop.service.ProductService接口。

2、改造shop-user工程

shop-user工程对应bhrpc框架的服务提供者角色。

(1)添加pom.xml依赖

shop-user工程作为bhrpc框架的服务提供者,在pom.xml需要添加如下依赖。

<dependency>
<groupId>io.binghe.rpc</groupId>
<artifactId>bhrpc-spring-boot-starter-provider</artifactId>
<version>${bhrpc.version}</version>
</dependency>

<dependency>
<groupId>io.binghe.shop</groupId>
<artifactId>shop-service-api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

(2)修改UserServiceImpl类

UserServiceImpl类的源码详见:shop-user工程下的io.binghe.shop.user.service.impl.UserServiceImpl,需要将UserServiceImpl类上标注的Spring中的@Service注解,替换成bhrpc框架中的@RpcService注解,修改后的源码如下所示。

@RpcService(interfaceClass = UserService.class, version = "1.0.0", group = "binghe")
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;

@Override
public User getUserById(Long userId) {
return userMapper.selectById(userId);
}
}

可以看到,在UserServiceImpl类上标注了bhrpc框架中的@RpcService注解,并且指定了interfaceClass、version和group属性。

(3)修改UserStarter类

UserStarter类的源码详见:shop-user工程下的io.binghe.shop.UserStarter,主要是在UserStarter类上添加@ComponentScan注解,修改后的源码如下所示。

@SpringBootApplication
@ComponentScan(basePackages = {"io.binghe.shop", "io.binghe.rpc"})
@EnableTransactionManagement(proxyTargetClass = true)
@MapperScan(value = { "io.binghe.shop.user.mapper" })
@EnableDiscoveryClient
@EnableAsync
public class UserStarter {
public static void main(String[] args){
SpringApplication.run(UserStarter.class, args);
}
}

可以看到,在UserStarter类上标注了@ComponentScan注解,并指定了扫描的包路径为io.binghe.shop和io.binghe.rpc,使其既能够扫描到微服务项目中包下的类,也能够扫描到bhrpc框架包下的类。

(4)添加配置

由于项目使用了Nacos作为配置中心,所以,需要在Nacos添加shop-user工程作为服务提供者的配置,登录Nacos管理端,找到shop-user工程的配置,如下所示。

图片

shop-user工程的配置

点击编辑按钮,在原有配置的基础上,添加如下配置信息。

bhrpc:
binghe:
provider:
# rpc server
serverAddress: 127.0.0.1:20880
# serverRegistryAddress
serverRegistryAddress: 127.0.0.1:20880
# zookeeper server
registryAddress: 127.0.0.1:2181
# registry center type
registryType: zookeeper
#registry loadbalance type
registryLoadBalanceType: zkconsistenthash
# reflect type
reflectType: cglib
# heartbeatInterval
heartbeatInterval: 30000

可以看到,配置的内容都是bhrpc框架的服务提供者启动时,需要读取的一些参数信息。配置完成后,点击发布按钮进行发布。

至此,shop-user工程改造完成,是不是非常简单呢?我们自己手写的bhrpc框架整合SpringCloud Alibaba项目就是这么简单。

3、改造shop-product工程

shop-product工程对应bhrpc框架的服务提供者角色。改造shop-product工程的步骤与改造shop-user工程的步骤基本相同。

(1)添加pom.xml依赖

shop-product工程同样作为bhrpc框架的服务提供者,在pom.xml需要添加如下依赖。

<dependency>
<groupId>io.binghe.rpc</groupId>
<artifactId>bhrpc-spring-boot-starter-provider</artifactId>
<version>${bhrpc.version}</version>
</dependency>

<dependency>
<groupId>io.binghe.shop</groupId>
<artifactId>shop-service-api</artifactId>
<version>${project.version}</version>
</dependency>

(2)修改ProductServiceImpl类

ProductServiceImpl类的源码详见:shop-product工程下的io.binghe.shop.product.service.impl.ProductServiceImpl,需要将ProductServiceImpl类上标注的Spring中的@Service注解,替换成bhrpc框架中的@RpcService注解,修改后的源码如下所示。

@RpcService(interfaceClass = ProductService.class, version = "1.0.0", group = "binghe")
public class ProductServiceImpl implements ProductService {
@Autowired
private ProductMapper productMapper;
@Override
public Product getProductById(Long pid) {
return productMapper.selectById(pid);
}

@Override
public int updateProductStockById(Integer count, Long id) {
return productMapper.updateProductStockById(count, id);
}
}

可以看到,在ProductServiceImpl类上标注了bhrpc框架中的@RpcService注解,并且指定了interfaceClass、version和group属性。

(3)修改ProductStarter类

ProductStarter类的源码详见:shop-product工程下的io.binghe.shop.ProductStarter,主要是在ProductStarter类上添加@ComponentScan注解,修改后的源码如下所示。

@SpringBootApplication
@ComponentScan(basePackages = {"io.binghe.shop", "io.binghe.rpc"})
@MapperScan(value = { "io.binghe.shop.product.mapper" })
@EnableTransactionManagement(proxyTargetClass = true)
@EnableDiscoveryClient
public class ProductStarter {
public static void main(String[] args){
SpringApplication.run(ProductStarter.class, args);
}
}

可以看到,在ProductStarter类上标注了@ComponentScan注解,并指定了扫描的包路径为io.binghe.shop和io.binghe.rpc,使其既能够扫描到微服务项目中包下的类,也能够扫描到bhrpc框架包下的类。

(4)添加配置

由于项目使用了Nacos作为配置中心,所以,需要在Nacos添加shop-product工程作为服务提供者的配置,登录Nacos管理端,找到shop-product工程的配置,如下所示。

图片

shop-product工程的配置

点击编辑按钮,在原有配置的基础上,添加如下配置信息。

bhrpc:
binghe:
provider:
# rpc server
serverAddress: 127.0.0.1:20881
# serverRegistryAddress
serverRegistryAddress: 127.0.0.1:20881
# zookeeper server
registryAddress: 127.0.0.1:2181
# registry center type
registryType: zookeeper
#registry loadbalance type
registryLoadBalanceType: zkconsistenthash
# reflect type
reflectType: cglib
# heartbeatInterval
heartbeatInterval: 30000

可以看到,配置的内容也都是bhrpc框架的服务提供者启动时,需要读取的一些参数信息。配置完成后,点击发布按钮进行发布。

至此,shop-product工程改造完成,也是非常简单的。

4、改造shop-order工程

shop-order工程对应bhrpc框架的服务消费者角色。

(1)添加pom.xml依赖

shop-order工程作为bhrpc框架的服务消费者,在pom.xml需要添加如下依赖。

<dependency>
<groupId>io.binghe.rpc</groupId>
<artifactId>bhrpc-spring-boot-starter-consumer</artifactId>
<version>${bhrpc.version}</version>
</dependency>

<dependency>
<groupId>io.binghe.shop</groupId>
<artifactId>shop-service-api</artifactId>
<version>${project.version}</version>
</dependency>

(2)新增OrderServiceV9Impl类

为了不影响整体项目原有的逻辑,复制OrderServiceV8Impl类的代码,新增成为OrderServiceV9Impl类,OrderServiceV9Impl类的源码详见:shop-order工程下的io.binghe.shop.order.service.impl.OrderServiceV9Impl,类框架代码如下所示。

@Slf4j
@Service("orderServiceV9")
public class OrderServiceV9Impl implements OrderService {
}

(3)改造OrderServiceV9Impl类

将OrderServiceV9Impl类中,原本userService和productService成员变量上标注的Spring中的@Autowired注解替换成bhrpc框架中的@RpcReference注解,替换后的源码如下所示。

@RpcReference(registryType = "zookeeper", registryAddress = "127.0.0.1:2181", loadBalanceType = "zkconsistenthash", version = "1.0.0", group = "binghe", serializationType = "protostuff", proxy = "cglib", timeout = 30000, async = false)
private UserService userService;

@RpcReference(registryType = "zookeeper", registryAddress = "127.0.0.1:2181", loadBalanceType = "zkconsistenthash", version = "1.0.0", group = "binghe", serializationType = "protostuff", proxy = "cglib", timeout = 30000, async = false)
private ProductService productService;

可以看到,userService和productService成员变量上标注了bhrpc框架中的@RpcReference注解,并且配置了服务消费者启动时需要的一些参数信息。

注意:需要将OrderServiceV9Impl类中的UserService改成引用io.binghe.shop.service.UserService接口,将ProductService改成引用io.binghe.shop.service.ProductService接口,修改OrderServiceV9Impl类中的一些报错信息。

(4)修改OrderStarter类

OrderStarter类的源码详见:shop-order工程下的io.binghe.shop.OrderStarter,主要是在OrderStarter类上添加@ComponentScan注解,修改后的源码如下所示。

@SpringBootApplication
@ComponentScan(basePackages = {"io.binghe.shop", "io.binghe.rpc"})
@EnableTransactionManagement(proxyTargetClass = true)
@MapperScan(value = { "io.binghe.shop.order.mapper" })
@EnableDiscoveryClient
@EnableFeignClients
public class OrderStarter {
public static void main(String[] args){
SpringApplication.run(OrderStarter.class, args);
}
}

可以看到,在OrderStarter类上标注了@ComponentScan注解,并指定了扫描的包路径为io.binghe.shop和io.binghe.rpc,使其既能够扫描到微服务项目中包下的类,也能够扫描到bhrpc框架包下的类。

(5)添加配置

由于项目使用了Nacos作为配置中心,所以,需要在Nacos添加shop-order工程作为服务消费者的配置,登录Nacos管理端,找到shop-order工程的配置,如下所示。

图片

shop-order工程的配置

点击编辑按钮,在原有配置的基础上,添加如下配置信息。

bhrpc:
binghe:
consumer:
# zookeeper server
registryAddress: 127.0.0.1:2181
# registry center type
registryType: zookeeper
# registry loadbalance type
loadBalanceType: zkconsistenthash
# proxy type
proxy: cglib
# version
version: 1.0.0
# group
group: binghe
# zkconsistenthash
serializationType: zkconsistenthash
# timeout
timeout: 30000
# async
async: false
# oneway
oneway: false
# heartbeatInterval
heartbeatInterval: 15000
# retryInterval
retryInterval: 1000
# retryTimes
retryTimes: 3

可以看到,配置的内容都是bhrpc框架的服务消费者启动时,需要读取的一些参数信息。配置完成后,点击发布按钮进行发布。

(6)修改OrderController类

OrderController类的源码详见:shop-order工程下的io.binghe.shop.order.controller.OrderController,主要是将OrderController类中使用@Qualifier注解标识的orderServiceV8修改成orderServiceV9,如下所示。

@Autowired
@Qualifier(value = "orderServiceV9")
private OrderService orderService;

至此,shop-order工程改造完成,也是非常简单的。

目前,在SpringCloud Alibaba项目中整合我们自己手写的RPC框架就完成了,是不是非常简单呢?没错,我们自己手写的bhrpc框架整合SpringCloud Alibaba项目就是这么简单!

5、测试

整合完不测试下怎么行?

(1)、启动服务

分别启动Nacos、RocketMQ、Sentinel、ZipKin、Seata和Zookeeper服务,对应服务的版本在源码的README.md文件中有说明。

(2)、启动工程

按顺序分别启动shop-user工程、shop-product工程、shop-order工程和shop-gateway工程。

  • 启动shop-user工程

输出如下信息,没有报错,说明bhrpc框架监听的是20880端口,表示启动成功。

i.b.r.p.common.server.base.BaseServer    : Server started on port 20880
  • shop-product工程

输出如下信息,没有报错,说明bhrpc框架监听的是20881端口,表示启动成功。

i.b.r.p.common.server.base.BaseServer    : Server started on port 20881
  • shop-order工程

输出如下信息,没有报错,说明启动成功。

o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8081 (http) with context path '/order'
  • shop-gateway工程

输出如下信息,没有报错,说明启动成功。

io.binghe.shop.GatewayStarter            : Started GatewayStarter in 9.604 seconds (JVM running for 10.964)

(3)、查询数据表数据

(1)打开cmd终端,进入MySQL命令行,并进入shop商城数据库,如下所示。

C:\Users\binghe>mysql -uroot -p
Enter password: ****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.7.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use shop;
Database changed

(2)查看商品数据表,如下所示。

mysql> select * from t_product;
+------+------------+-------------+-------------+
| id | t_pro_name | t_pro_price | t_pro_stock |
+------+------------+-------------+-------------+
| 1001 | 华为 | 2399.00 | 100 |
| 1002 | 小米 | 1999.00 | 100 |
| 1003 | iphone | 4999.00 | 100 |
+------+------------+-------------+-------------+
3 rows in set (0.00 sec)

这里,我们以id为1001的商品为例,此时发现商品的库存为100。

(3)查询订单数据表,如下所示。

mysql> select * from t_order;
Empty set (0.00 sec)

可以发现订单数据表为空。

(4)查询订单条目数据表,如下所示。

mysql> select * from t_order_item;
Empty set (0.00 sec)

可以看到,订单条目数据表为空。

(4)、访问项目

打开浏览器访问http://localhost:10002/server-order/order/submit_order?userId=1001&productId=1001&count=1,如下所示。

图片

访问结果

可以看到,项目返回的结果为success,表示项目执行成功。

5、再次查看数据表数据

(1)查看商品数据表,如下所示。

mysql> select * from t_product;
+------+------------+-------------+-------------+
| id | t_pro_name | t_pro_price | t_pro_stock |
+------+------------+-------------+-------------+
| 1001 | 华为 | 2399.00 | 99 |
| 1002 | 小米 | 1999.00 | 100 |
| 1003 | iphone | 4999.00 | 100 |
+------+------------+-------------+-------------+
3 rows in set (0.00 sec)

这里,id为1001的商品库存为99,说明库存已经减少了1。

(2)查询订单数据表,如下所示。

mysql> select * from t_order;
+-------------------+-----------+-------------+-------------+-----------+---------------+
| id | t_user_id | t_user_name | t_phone | t_address | t_total_price |
+-------------------+-----------+-------------+-------------+-----------+---------------+
| 96829539832958976 | 1001 | binghe | 13212345678 | 北京 | 2399.00 |
+-------------------+-----------+-------------+-------------+-----------+---------------+
1 row in set (0.00 sec)

可以看到,在t_order表中新增了一张订单数据表,订单的总金额为2399.00元。

(3)查询订单条目数据表,如下所示。

mysql> select * from t_order_item;
+-------------------+-------------------+----------+------------+-------------+----------+
| id | t_order_id | t_pro_id | t_pro_name | t_pro_price | t_number |
+-------------------+-------------------+----------+------------+-------------+----------+
| 96829541082861568 | 96829539832958976 | 1001 | 华为 | 2399.00 | 1 |
+-------------------+-------------------+----------+------------+-------------+----------+
1 row in set (0.00 sec)

可以看到,订单条目数据表中条了一条订单条目数据,商品的id为1001,商品名称为华为,商品的价格为2399.00,下单的商品数量为1。

根据测试结果可以看出,我们已经正确在SpringCloud Alibaba项目中整合了我们自己手写的bhrpc框架。

6、总结

实现了功能不总结下怎么行?

在完成整合Spring的篇章后,我们又开启了整合SpringBoot的篇章,首先,我们完成了服务提供者整合SpringBoot的功能,并基于SpringBoot接入了服务提供者。同时,实现了服务消费者整合SpringBoot的功能,并且基于SpringBoot接入了服务消费者。

在整合Docker章节,我们实现了基于Docker接入了服务提供者和基于Docker接入了服务消费者。

本章,我们更进一步将手写的bhrpc框架整合到SpringCloud Alibaba项目。

总之,我们写的RPC框架正在一步步实现它该有的功能。

最后,我想说的是:学习《RPC手撸专栏》一定要塌下心来,一步一个脚印,动手实践,认真思考,遇到不懂的问题,可以直接到星球发布主题进行提问。一定要记住:纸上得来终觉浅,绝知此事要躬行的道理。否则,一味的CP,或者光看不练,不仅失去了学习的意义,到头来更是一无所获。

责任编辑:姜华 来源: 冰河技术
相关推荐

2020-11-02 08:19:18

RPC框架Java

2015-04-07 13:48:53

框架编程语言7种理由

2012-10-10 09:14:50

PHPRPCPHP框架

2021-04-21 08:01:31

Googleprotobuf嵌入式系统

2022-05-29 21:38:11

限流熔断流量

2021-02-20 09:45:02

RPC框架Java

2022-09-14 14:41:21

RPC框架RPC协议

2021-03-26 06:01:45

日志MongoDB存储

2023-11-08 07:45:47

Spring微服务

2023-05-06 13:56:02

工具函数库业务

2022-01-07 06:12:08

RPC框架限流

2009-08-06 09:55:09

Mono2.0

2011-04-28 08:59:20

项目框架

2022-04-27 08:23:34

微服务负载均衡

2021-05-31 11:22:24

微服务开发框架

2022-08-29 06:27:15

Nacos微服务

2020-01-09 11:11:35

RPC框架调用远程

2023-06-06 19:18:17

数据工程软件工程

2009-05-08 16:41:47

LinuxUbuntu秘诀

2011-02-17 09:45:40

云计算RPC框架
点赞
收藏

51CTO技术栈公众号