这个项目是一个C++11 标准开发的小型异步 RPC 框架(Remote Procedure Call,远程过程调用)框架,采用了以下技术:
- 基于c++11线程池
- 异步高并发主从 Reactor 架构
- 底层采用 epoll 实现 IO 多路复用
- 应用层采用 protobuf 自定义 rpc 通信协议
- 异步日志,支持同步和异步模式,可以写入文件或直接输出
- 定时器,用于防止程序崩坏超时,防止阻塞,从而避免长时间等待。
这个项目大多数都加上注释,因为我也是从零开始学起来的,采用技术真的挺多。这个rpc框架项目grpc和其他rpc还是小巧太多,但是基本原理都差不多,这个mircorpc毕竟小巧,用于交流学习,我觉得还是很友好。 里面难免有些bug,欢迎大家提供。
Protobuf 3.21.12 TinyXML CMake
-
在
build
目录下执行以下命令进行项目编译:$ cmake .. $ make
-
执行
install.sh
安装RPC所需的依赖:$ ./install.sh
- 用户提供自定义服务的
.proto
文件。
syntax="proto3";
option cc_generic_services=true;
message addStudentRequest{
int32 price =1;
string goods=2;
}
message addStudentResponse{
int32 ret_code=1;
string res_info=2;
string order_id=3;
}
message queryStudentRequest{
string order_id=1;
}
message queryStudentResponse{
int32 ret_code=1;
string res_info=2;
string order_id=3;
int32 price=4;
string goods=5;
}
service StudentService{
rpc add_student(addStudentRequest)returns(addStudentResponse);
rpc query_student(queryStudentRequest)returns(queryStudentResponse);
}
-
使用
makeService
Python脚本构建服务的执行环境:$ python3 RPC/makeService/makeService.py -i {servicefile}.proto -o [installpath]
-
继续执行服务中的CMake和编译:
$ cd [installpath] $ cmake . $ make
-
执行
bin
目录下的start.sh
开启服务:$ ./start.sh
在 test_client
目录下提供了客户端测试程序,可以使用该程序测试RPC服务的功能。