💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
## 13) 数据传输协议protocol buffer ### 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: ``` cd $GOPATH/src/ git clone https://github.com/protocolbuffers/protobuf.git ``` 2、或者直接将压缩包拖入后解压 ``` unzip protobuf.zip ``` 3、安装依赖库 ``` sudo apt-get install autoconf automake libtool curl make g++ unzip libffi-dev -y ``` 4、进入目录 ``` cd protobuf/ ``` 5、自动生成configure配置文件: ``` ./autogen.sh ``` 6、配置环境: ``` ./configure ``` 7、编译源代码(时间比较长): ``` make ``` 8、安装 ``` sudo make install ``` 9、刷新共享库 (很重要的一步啊) ``` sudo ldconfig ``` 10、成功后需要使用命令测试 ``` protoc -h ``` ##### ### 13.4 protobuf与C++编程 ​ 首先创建一个msg.proto文件,里面定义协议的格式 > protobuf_test/msg.proto ```protobuf syntax="proto3"; package protobuf_test; message helloworld { int32 id = 1; // ID string str = 2; // str int32 opt = 3; //optional field } ``` ​ 写好 proto 文件之后就可以用 Protobuf 编译器将该文件编译成目标语言了。本例中我们将使用 C++。 ​ 使用如下命令可以生成我们所需要的.cc与.h ```bash protoc --cpp_out=. ./*.proto ``` ​ 执行上述命令将生成msg.pb.cc与msg.pb.h,执行时需要在文件msg.proto所在的目录下执行。 为了方便,我们可以写一个脚本,方便下次执行。 > build.sh ```bash #!/bin/bash protoc --cpp_out=. ./*.proto ``` > protobuf_test/write.cpp ```c #include "msg.pb.h" #include <fstream> #include <iostream> using namespace std; int main(void) { protobuf_test::helloworld msg1; msg1.set_id(101); msg1.set_str("hello"); fstream output("./log", ios::out | ios::trunc | ios::binary); if (!msg1.SerializeToOstream(&output)) { cerr << "Failed to write msg." << endl; return -1; } return 0; } ``` ​ Msg1 是一个 helloworld 类的对象,set_id() 用来设置 id 的.SerializeToOstream 将对象序列化后写入一个 fstream 流。当担序列化的接口还有很多比如SerializeToString, SerializeToArray等。 > protobuf_test/read.cpp ```c #include "msg.pb.h" #include <fstream> #include <iostream> using namespace std; void ListMsg(const protobuf_test::helloworld & msg) { cout << msg.id() << endl; cout << msg.str() << endl; } int main(int argc, char* argv[]) { protobuf_test::helloworld msg1; fstream input("./log", ios::in | ios::binary); if (!msg1.ParseFromIstream(&input)) { cerr << "Failed to parse address book." << endl; return -1; } ListMsg(msg1); } ``` ​ 现在进行编译: ```bash $ g++ msg.pb.cc write.cpp -o write -lprotobuf $ g++ msg.pb.cc read.cpp -o read -lprotobuf ``` ​ 执行,先执行write,再执行read。 write会将数据写进log文件中,read可以通过pb协议解析出数据到内存变量中. --- ### 关于作者: 作者:`Aceld(刘丹冰)` mail: [danbing.at@gmail.com](mailto:danbing.at@gmail.com) github: [https://github.com/aceld](https://github.com/aceld) 原创书籍: [https://www.kancloud.cn/@aceld](https://www.kancloud.cn/@aceld) ![](https://img.kancloud.cn/b0/d1/b0d11a21ba62e96aef1c11d5bfff2cf8_227x227.jpg) >**原创声明:未经作者允许请勿转载, 如果转载请注明出处**