微服务框架学习

Protocol Buffer

2018-10-23  本文已影响0人  疾风2018

Protocol Buffer是Google发起的,一个语言无关、平台无关的序列化/反序列化结构化数据的通用机制(框架),可用于通信协议,数据存储等。

Protocol Buffer名字的起源

protocol buffers 诞生之初是为了解决服务器端新旧协议(高低版本)兼容性问题,名字也很体贴,“协议缓冲区”。只不过后期慢慢发展成用于传输数据。 Protocol Buffers 命名由来:

Why the name "Protocol Buffers"? The name originates from the early days of the format, before we had the protocol buffer compiler to generate classes for us. At the time, there was a class called ProtocolBuffer which actually acted as a buffer for an individual method. Users would add tag/value pairs to this buffer individually by calling methods like AddValue(tag, value). The raw bytes were stored in a buffer which could then be written out once the message had been constructed.

Since that time, the "buffers" part of the name has lost its meaning, but it is still the name we use. Today, people usually use the term "protocol message" to refer to a message in an abstract sense, "protocol buffer" to refer to a serialized copy of a message, and "protocol message object" to refer to an in-memory object representing the parsed message.

这个名字起源于 format 早期,在我们有 protocol buffer 编译器为我们生成类之前。当时,有一个名为 ProtocolBuffer 的类,它实际上充当了单个方法的缓冲区。用户可以通过调用像 AddValue(tag,value) 这样的方法分别将标签/值对添加到此缓冲区。原始字节存储在一个缓冲区中,一旦构建消息就可以将其写出。

从那时起,名为“缓冲”的部分已经失去了意义,但它仍然是我们使用的名称。今天,人们通常使用术语“protocol message”来指代抽象意义上的消息,“protocol buffer”指的是消息的序列化副本,而“protocol message object”指的是代表内存中对象解析的消息。

.proto 文件示例

syntax = "proto3"

package perftools.profiles;

option java_package = "com.google.perftools.profiles";

message Profile {

  repeated ValueType sample_type = 1;

  repeated Sample sample = 2;

  int64 keep_frames = 8;  // Index into string table.

}

message ValueType {

  int64 type = 1; // Index into string table.

  int64 unit = 2; // Index into string table.

}

message定义的是数据结构


gRPC

gRPC与Protocol Buffer结合在一起非常合适,都是Google发起并使用的项目。

上一篇下一篇

猜你喜欢

热点阅读