简介
protobuf 是什么?
Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages.
Google公司开发的一种数据描述语言- 一种与语言无关,平台无关的可扩展机制,用于序列化结构化数据
- 通过
proto文件定义结构化数据,其他功能类似的比如XML、JSON等; - 自带代码生成器
现在有许多框架等在使用Protocol Buffers。gRPC也是基于Protocol Buffers。 Protocol Buffers 目前有2和3两个版本号。
在gRPC中推荐使用proto3版本。
为什么叫"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.
优点
- 支持多种编程语言
- 序列化数据体积小
- 反序列化速度快
- 序列化和反序列化代码自动生成
缺点
- 可读性差,缺乏自描述性
"变态的"性能表现
有位网友曾经做过各种通用序列化协议技术的对比,我这里直接拿来给大家感受一下:
具体的参数:
protobuf | jackson | xstream | Serializable | hessian2 | hessian2压缩 | hessian1 | |
|---|---|---|---|---|---|---|---|
序列化(单位ns) | 1154 | 5421 | 92406 | 10189 | 26794 | 100766 | 29027 |
反序列化(单位ns) | 1334 | 8743 | 117329 | 64027 | 37871 | 188432 | 37596 |
| bytes | 97 | 311 | 664 | 824 | 374 | 283 | 495 |
Protobuf安装
- 选择对应的安装文件下载,解压(
win10)(tips:文件解压的路径最好是英文,无任何特殊字符,包括空格)
- 设置环境变量
Path中增加bin目录(win10)
- 查看是否成功安装(
win10)