# Ca1mRpc **Repository Path**: ca1m3/ca1m-rpc ## Basic Information - **Project Name**: Ca1mRpc - **Description**: 基于muduo网络库和protobuf的RPC框架。(施磊老师课程) - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2024-10-11 - **Last Updated**: 2025-10-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # mprpc分布式网络通信框架 ### 集群和分布式 **集群**:每一台服务器独立运行一个工程的所有模块。(几个服务器上部署相同的应用程序来分担客户端的请求。) ![image.png](./res/img1.png) **分布式**:一个工程拆分了很多模块,每一个模块独立部署运行在一个服务器主机上,所有服务器协同工作共同提供服务,每一台服务器称作分布式的一个节点,根据节点的并发要求,对一个节点可以再做节点模块集群部署。 ![image.png](./res/img2.png) ### RPC通信原理 ![image.png](./res/img3.png) 序列化和反序列化使用protobuf而非json的好处: 1. protobuf是二进制存储的;xml和json都是文本存储的(protobuf更快) 2. protobuf无需存储额外的信息 3. protobuf是一种效率和兼容性都很优秀的二进制数据传输格式,可以用于诸如网络传输、配置文件、数据存储等诸多领域。 ### 本地服务发布为RPC服务 ```proto syntax = "proto3"; package fixbug; option cc_generic_services = true; message ResultCode { int32 errcode = 1; bytes errmeg = 2; } message LoginRequest { bytes name = 1; bytes pwd = 2; } message LoginResponse { ResultCode result = 1; bool success = 2; } service UserServiceRpc { rpc Login(LoginRequest) returns(LoginResponse); } ``` 以UserServiceRpc服务为例,protobuf会生成RPC服务提供者UserServiceRpc和RPC服务消费者UserServiceRpc_Stub ### RPC方法调用总体流程 ![image.png](./res/img4.png) ### RPC调用示例 ![image.png](./res/img5.png) ### zookeeper分布式协调服务 zookeeper是在分布式环境中应用非常广泛,其优秀功能很多,比如分布式环境中全局命名服务,服务注册中心,全局分布式锁等。(**实践时最后zookeeper的服务端启动有点问题!**)