13.1 简介

Google Protocol Buffer (简称 Protobuf)是google旗下的一款轻便高效的结构化数据存储格式,平台无关、语言无关、可扩展,可用于通讯协议和数据存储等领域。所以很适合用做数据存储和作为不同应用,不同语言之间相互通信的数据交换格式,只要实现相同的协议格式即同一 proto文件被编译成不同的语言版本,加入到各自的工程中去。这样不同语言就可以解析其他语言通过 protobuf序列化的数据。目前官网提供了 C++,Python,JAVA,GO等语言的支持。google在2008年7月7号将其作为开源项目对外公布。

tips:

  1. 啥叫平台无关?Linux、mac和Windows都可以用,32位系统,64位系统通吃
  2. 啥叫语言无关?C++、Java、Python、Golang语言编写的程序都可以用,而且可以相互通信
  3. 那啥叫可扩展呢?就是这个数据格式可以方便的增删一部分字段啦~
  4. 最后,啥叫序列化啊?解释得通俗点儿就是把复杂的结构体数据按照一定的规则编码成一个字节切片

13.2 数据交换格式

A)常用的数据交换格式有三种:

  1. json: 一般的web项目中,最流行的主要还是 json。因为浏览器对于json 数据支持非常好,有很多内建的函数支持。
  2. xml: 在 webservice 中应用最为广泛,但是相比于 json,它的数据更加冗余,因为需要成对的闭合标签。json 使用了键值对的方式,不仅压缩了一定的数据空间,同时也具有可读性。
  3. protobuf: 是后起之秀,是谷歌开源的一种数据格式,适合高性能,对响应速度有要求的数据传输场景。因为 profobuf 是二进制数据格式,需要编码和解码。数据本身不具有可读性。因此只能反序列化之后得到真正可读的数据。

B)protobuf的优势与劣势

优势:

1:序列化后体积相比Json和XML很小,适合网络传输

2:支持跨平台多语言

3:消息格式升级和兼容性还不错

4:序列化反序列化速度很快,快于Json的处理速速

劣势:

1:应用不够广(相比xml和json)

2:二进制格式导致可读性差

3:缺乏自描述


13.3 protobuf环境安装

A) protobuf 编译工具安装

1、下载 protoBuf:

  1. cd $GOPATH/src/
  2. git clone https://github.com/protocolbuffers/protobuf.git

2、或者直接将压缩包拖入后解压

  1. unzip protobuf.zip

3、安装依赖库

  1. sudo apt-get install autoconf automake libtool curl make g++ unzip libffi-dev -y

4、进入目录

  1. cd protobuf/

5、自动生成configure配置文件:

  1. ./autogen.sh

6、配置环境:

  1. ./configure

7、编译源代码(时间比较长):

  1. make

8、安装

  1. sudo make install

9、刷新共享库 (很重要的一步啊)

  1. sudo ldconfig

10、成功后需要使用命令测试

  1. protoc -h

13.4 protobuf与C++编程

  1. 首先创建一个msg.proto文件,里面定义协议的格式

protobuf_test/msg.proto

  1. syntax="proto3";
  2. package protobuf_test;
  3. message helloworld
  4. {
  5. int32 id = 1; // ID
  6. string str = 2; // str
  7. int32 opt = 3; //optional field
  8. }
  1. 写好 proto 文件之后就可以用 Protobuf 编译器将该文件编译成目标语言了。本例中我们将使用 C++。
  2. 使用如下命令可以生成我们所需要的.cc与.h
  1. protoc --cpp_out=. ./*.proto
  1. 执行上述命令将生成msg.pb.ccmsg.pb.h,执行时需要在文件msg.proto所在的目录下执行。

为了方便,我们可以写一个脚本,方便下次执行。

build.sh

  1. #!/bin/bash
  2. protoc --cpp_out=. ./*.proto

protobuf_test/write.cpp

  1. #include "msg.pb.h"
  2. #include <fstream>
  3. #include <iostream>
  4. using namespace std;
  5. int main(void)
  6. {
  7. protobuf_test::helloworld msg1;
  8. msg1.set_id(101);
  9. msg1.set_str("hello");
  10. fstream output("./log", ios::out | ios::trunc | ios::binary);
  11. if (!msg1.SerializeToOstream(&output)) {
  12. cerr << "Failed to write msg." << endl;
  13. return -1;
  14. }
  15. return 0;
  16. }
  1. Msg1 是一个 helloworld 类的对象,set_id() 用来设置 id 的.SerializeToOstream 将对象序列化后写入一个 fstream 流。当担序列化的接口还有很多比如SerializeToString, SerializeToArray等。

protobuf_test/read.cpp

  1. #include "msg.pb.h"
  2. #include <fstream>
  3. #include <iostream>
  4. using namespace std;
  5. void ListMsg(const protobuf_test::helloworld & msg) {
  6. cout << msg.id() << endl;
  7. cout << msg.str() << endl;
  8. }
  9. int main(int argc, char* argv[]) {
  10. protobuf_test::helloworld msg1;
  11. fstream input("./log", ios::in | ios::binary);
  12. if (!msg1.ParseFromIstream(&input)) {
  13. cerr << "Failed to parse address book." << endl;
  14. return -1;
  15. }
  16. ListMsg(msg1);
  17. }
  1. 现在进行编译:
  1. $ g++ msg.pb.cc write.cpp -o write -lprotobuf
  2. $ g++ msg.pb.cc read.cpp -o read -lprotobuf
  1. 执行,先执行write,再执行read write会将数据写进log文件中,read可以通过pb协议解析出数据到内存变量中.