RPC 框架分层设计| 青训营

23 阅读2分钟

需要解决的问题

  1. 函数映射
  2. 数据传输
  3. 网络传输

RPC模型

Remote Procedure Call

image-20230801114159180

IDL 文件

对于不同的语言编写的程序可以相互通信

生成代码

IDL生成的静态库

编解码

将内存中表示到字节序列的的转换称为编码,反之为解码,也称为序列化和反序列化

通信协议

数据在网络中传输内容和格式

网络传输

TCP/UDP

Pros & Cons

  1. 单一职责
  2. 扩容方便
  3. 故障隔离

但故障处理比较麻烦

分层设计

编解码层

数据格式:编程语言特定格式/文本格式/二进制编码

TLV编码

  • Tag 标签类型
  • Length
  • Value 值

协议层

网络通信层

微服务架构

基本概念

服务

相同逻辑的运行实体

实例

运行实体即为实例

集群

服务内部的逻辑划分,包含多个实例

实例与进程

一个实例对应多个进程

有状态/无状态服务

有无存储可持久化的服务

服务间通信

image-20230801142143250

服务注册和发现

  • Hardcode:

    指定目标服务的地址不行,因为:不固定ip地址

  • dns

    存在缓存会导致延时,负载均衡问题

  • 服务注册中心

    存储服务名到实例的映射

image-20230801142604509

核心服务治理功能

服务发布

让服务升级运行新的代码的过程

蓝绿部署

image-20230801143937265

灰度发布canary

image-20230801144044260

流量治理

基于地区、集群、实例来分流

负载均衡

Round robin

Ring hash

稳定性治理

限流

image-20230801150958281

过载保护

image-20230801151017929

熔断

image-20230801151040331

降级

image-20230801151054015

Takeaway

RPC is a client-server communication model that enables one program to request a service or function from another program on a different machine over a network. It has several pros that makes it stands out from other model:

  • Procedural Abstraction, allowing developers to call remote procedures or functions in a manner similar to local function calls.
  • Language/platform independence.
  • Transparency

Developers should pay attention to performance, error handling, security, versioning, and testing considerations to ensure effective development and deployment of RPC-based systems.