仓库源文站点原文


title: "gRPC 简要" categories: Tech updated: comments: true

mathjax: false

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

<!-- more -->

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

劣势

适合的场景

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

参考

其他