gRPC 简要

只涉及很基础的 general idea. gRPC is an RPC implementation using Protocol Buffers by Google.

Remote Procedure Call (RPC)

一种基于 client-server 模型的范式, 另外还有 Representational State Transfer (REST).

In distributed computing, a remote procedure call (RPC) is when a computer program causes a procedure (subroutine) to execute in a different address space (commonly on another computer on a shared network), which is coded as if it were a normal (local) procedure call, without the programmer explicitly coding the details for the remote interaction.

RPCs are a form of inter-process communication (IPC), in that different processes have different address spaces.

To let different clients access servers, a number of standardized RPC systems have been created. Most of these use an interface description language (IDL) to let various platforms call the RPC.

gRPC

By default, gRPC uses Protocol Buffers, Google’s mature open source mechanism for serializing structured data (like JSON).

The first step when working with protocol buffers is to define the structure for the data you want to serialize in a proto file: this is an ordinary text file with a .proto extension. Protocol buffer data is structured as messages, where each message is a small logical record of information containing a series of name-value pairs called fields. Here’s a simple example:

message Person {
  string name = 1;
  int32 id = 2;
  bool has_ponycopter = 3;
}

You define gRPC services in ordinary proto files, with RPC method parameters and return types specified as protocol buffer messages. 输入输出都是 proto 格式. TensorFlow 用的 .pb 也是这个格式.

Protocol buffers

只摘录个别要点

It’s like JSON, except it’s smaller and faster, and it generates native language bindings. Protocol buffers are the most commonly-used data format at Google.

二进制, 更小, 但 not human readable

劣势

  • For data that exceeds a few megabytes, consider a different solution.
  • Messages are not compressed.
  • Protocol buffer messages are less than maximally efficient in both size and speed for many scientific and engineering uses that involve large, multi-dimensional arrays of floating point numbers.
  • Protocol buffers are not a formal standard of any organization.

适合的场景

  • Microservices: gRPC is designed for low latency and high throughput communication. gRPC is great for lightweight microservices where efficiency is critical.
  • Point-to-point real-time communication: gRPC has excellent support for bi-directional streaming. gRPC services can push messages in real-time without polling.

为什么叫这个名字? 历史原因, 以前有个类叫 ProtocolBuffer, 调用这个类的方法 AddValue(tag, value) 存到 buffer, 再一次性写出. 现在这个玩意儿和 buffer 无关了. 参考 这里.

参考

其他

  • RPC 的其他实现还有百度的 brpc, Facebook 的 Thrift 等.
  • Will Zhang. (2017). 如何评价百度开源的 RPC 框架 brpc?
  • rpc benchmark 里直言 “gRPC 几乎在所有参与的测试中垫底, 可能它的定位是给 google cloud platform 的用户提供一个多语言, 对网络友好的实现, 性能还不是要务”.
  • 一个简单例子: 鸢尾花分类
  • Designing data-intensive applications 第 4 章 Encoding and Evolution 介绍了 protobuf 和其他格式的编解码.