Protocol Buffer简介

Protocol Buffer是由Google发起的一种语言和平台无关的数据序列化机制,主要用于通信协议和数据存储。其设计初衷为解决服务器端新旧协议兼容问题,后发展为高效传输数据的工具。.proto文件用于定义数据结构,gRPC与Protocol Buffer结合紧密,适用于微服务架构。

(项目地址: https://developers.google.com/protocol-buffers/

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发起并使用的项目。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值