1. protobuf简介
1.1 什么是protobuf
protobuf(Protocol Buffers),是Google推出的一个的结构化数据交换协议,用于传递自定义的消息格式,
可用于同一台机器上的进程间、不同设备进程间的数据传递。是一种语言无关、平台无关、扩展性良好的,提供了一种将结构化数据进行序列化和反序列化的方法。
Protocol Buffers是一个灵活、高效、自动化的方式提供了结构化数据的序列化方法,你可以将它看到是XML,但它更小、更快、更简单。我们可以按自己的需要定义结构化数据,然后使用protobuf读/写这些结构数据,生成数据流可以用于不同平台、不同语言间的数据传递。
1.2 protobuf格式的优劣
protobuf相对于XML等结构化数据传输方式,它一种二进制的数据格式,具有更高的传输、打包、解包效率。
相对于XML来说,protobuf具有以下优势:
- 更加简单
- 小3〜10倍
- 快20〜100倍
- 歧义更少
- 生成数据访问类更加简单
而相对于JSON来说,protobuf也有比较明显的优势:
- 更好的前后数据版本兼容性
- 提供了验证机制,更容易被扩展
- 不同语言间互操作性更好
protobuf有一定的优势,最明显就是其数据量更小,这带来的速度提升同样是十分非常明显的。但相比XML和JSON格式来说,也会有一些缺点:
- 二进制传编码与传输,可读性差
- 编码与解码依赖于额外的库,不能在浏览器、JS中直接使用
- 缺乏自描述
2. 怎么样使用protobuf
2.1 编译器安装
protobuf编译器会将消息文件(.proto文件)编译为语言环境所需的文件。
使用protobuf首先要安装protobuf编译器。可以通过以下网址下载预编译二进制包或源文件:
https://github.com/google/protobuf/releases
本例中我们下载的源文件,下载后解压,并按以下步骤安装:
$ ./configure $ make $ make install
安装后,系统就有了protoc命令,通过这个命令可对.proto文件进行编译。protoc命令格式如下:
protoc [OPTION] PROTO_FILES
2.2 运行时环境安装
根据所使用语言,下载自己所需要的protobuf运行时环境。Google提供了对C++、Java、Python、 Objective-C、C#、JavaNano、JS(Node.js)、Ruby、 Go、PHP几种语言的支持,可以点击通过以下网址下载protobuf运行时环境:
https://github.com/google/protobuf#protobuf-runtime-installation
注意:protobuf使用C++编写,使用源码安装时C++运行时环境也会自动被安装。
2.3 使用
安装相应支持模块后,一般还需要以下几个步骤:
- 编写
.proto文件,该文件用于消息格式的定义 - 使用
protobuf编译器编译.proto文件 - 使用
protobuf运行时模块提供的API进行消息的读写
3. protobuf使用示例
接下来通过一个示例来介绍protobuf的使用,在这个示例中将参考官方文档中C++语言。如果需要其它语言示例请参考官方的Tutorials。
3.1 定义.proto文件(消息格式)
.proto文件是在程序中使用的结构化数据,首先我们需要编写一个.proto文件来定义我们程序中需要处理的结构化数据。在protobuf术语中结构化数据被称为消息(Message)。
定义一个.proto文件如下,文件保存为itbilu.proto:
package itbilu;
message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
required string number = 1;
optional PhoneType type = 2 [default = HOME];
}
repeated PhoneNumber phone = 4;
}
message AddressBook {
repeated Person person = 1;
}
如上所示,.proto文件的定义语法类似于C++或Java。在这个文件中,包含以下部分的内容:
包定义-.proto文件以package包声明关键字开始,包定义有助于防止在不同项目中命名冲突。在上面示例中,包名被定义为tutorial。
消息定义-定义完包名后,就开始了消息的定义,消息以message关键字声明。消息可以理解为一个对象,在上面文件中,定义了Person和AddressBook两个消息,而Person中又定义了一个PhoneNumber子消息。
消息成员定义-消息是一个包含一组各类型字段的的集合。在消息中,定义字段有很多数据类型可以使用,如:bool、int32、float、double、string等。定义字段时,也可以在消息中定义一个子消息结构做为数据类型,如:上面示例中的PhoneNumber。定义消息类型时,也可以使用enum枚举做为数据类型。
定义字段时,我们使用了required、optional、repeated三个关键字。这些关键字表示对字段的约束,分别表示:
required-非空约束。如果字段值为空,会被认为是uninitialized,并抛出异常。optional-可选。表示字段可以赋值,也可以不赋值。不赋值时,将会使用默认值。repeated-可重复次数。表示字段可以重复使用的次数,重复顺序会被保存在protobuf中,可以将其理解为一个数组。
3.2 编译proto文件
定义好.proto文件后,就可以使用protobuf编译器将其编译成目标文件。C++编译命令格式如下:
$ protoc -I=$SRC_DIR --cpp_out=$DST_DIR $SRC_DIR/addressbook.proto
-I参数用于指定输源文件目录;--cpp_out用于指定以C++语言格式输出,并指定到了输出目录及编译文件。
本例中,将itbilu.proto保存在了protobuf/src目录下,C++运行时环境也在这个目录中。我们可以像下面这样编译:
$ protoc --cpp_out=./ ./itbilu.proto
编译完成会输出以下两个文件
itbilu.pb.h-C++头文件itbilu.pb.cc-C++实现文件
输出的文件就是在C++运行时环境中,对指定消息itbilu的操作类。在本例中,包含以下方法:
// name inline bool has_name() const; inline void clear_name(); inline const ::std::string& name() const; inline void set_name(const ::std::string& value); inline void set_name(const char* value); inline ::std::string* mutable_name(); // id inline bool has_id() const; inline void clear_id(); inline int32_t id() const; inline void set_id(int32_t value); // email inline bool has_email() const; inline void clear_email(); inline const ::std::string& email() const; inline void set_email(const ::std::string& value); inline void set_email(const char* value); inline ::std::string* mutable_email(); // phone inline int phone_size() const; inline void clear_phone(); inline const ::google::protobuf::RepeatedPtrField< ::tutorial::Person_PhoneNumber >& phone() const; inline ::google::protobuf::RepeatedPtrField< ::tutorial::Person_PhoneNumber >* mutable_phone(); inline const ::tutorial::Person_PhoneNumber& phone(int index) const; inline ::tutorial::Person_PhoneNumber* mutable_phone(int index); inline ::tutorial::Person_PhoneNumber* add_phone();
在不同语言环境中的API,请参考: API Reference。
注意:protoc编译器,实际上是根据.proto消息源文件,编译并生成指定语言的操作类。结合不同的运行时环境,可以实现消息跨语言环境的互操作。
3.3 消息读写
现在我们可以使用生成的消息操作类进行进行消息的读写。
Writing写消息
使用消息添加用户资料,并写入到了输出流中:
#include <iostream>
#include <fstream>
#include <string>
#include "itbilu.pb.h"
using namespace std;
// This function fills in a Person message based on user input.
void PromptForAddress(tutorial::Person* person) {
cout << "Enter person ID number: ";
int id;
cin >> id;
person->set_id(id);
cin.ignore(256, '\n');
cout << "Enter name: ";
getline(cin, *person->mutable_name());
cout << "Enter email address (blank for none): ";
string email;
getline(cin, email);
if (!email.empty()) {
person->set_email(email);
}
while (true) {
cout << "Enter a phone number (or leave blank to finish): ";
string number;
getline(cin, number);
if (number.empty()) {
break;
}
tutorial::Person::PhoneNumber* phone_number = person->add_phone();
phone_number->set_number(number);
cout << "Is this a mobile, home, or work phone? ";
string type;
getline(cin, type);
if (type == "mobile") {
phone_number->set_type(tutorial::Person::MOBILE);
} else if (type == "home") {
phone_number->set_type(tutorial::Person::HOME);
} else if (type == "work") {
phone_number->set_type(tutorial::Person::WORK);
} else {
cout << "Unknown phone type. Using default." << endl;
}
}
}
// Main function: Reads the entire address book from a file,
// adds one person based on user input, then writes it back out to the same
// file.
int main(int argc, char* argv[]) {
// Verify that the version of the library that we linked against is
// compatible with the version of the headers we compiled against.
GOOGLE_PROTOBUF_VERIFY_VERSION;
if (argc != 2) {
cerr << "Usage: " << argv[0] << " ADDRESS_BOOK_FILE" << endl;
return -1;
}
tutorial::AddressBook address_book;
{
// Read the existing address book.
fstream input(argv[1], ios::in | ios::binary);
if (!input) {
cout << argv[1] << ": File not found. Creating a new file." << endl;
} else if (!address_book.ParseFromIstream(&input)) {
cerr << "Failed to parse address book." << endl;
return -1;
}
}
// Add an address.
PromptForAddress(address_book.add_person());
{
// Write the new address book back to disk.
fstream output(argv[1], ios::out | ios::trunc | ios::binary);
if (!address_book.SerializeToOstream(&output)) {
cerr << "Failed to write address book." << endl;
return -1;
}
}
// Optional: Delete all global objects allocated by libprotobuf.
google::protobuf::ShutdownProtobufLibrary();
return 0;
}
Reading读消息
相应的,我们还需要创建一个消息读取示例,以读取上例中创建的消息文件:
#include <iostream>
#include <fstream>
#include <string>
#include "addressbook.pb.h"
using namespace std;
// Iterates though all people in the AddressBook and prints info about them.
void ListPeople(const tutorial::AddressBook& address_book) {
for (int i = 0; i < address_book.person_size(); i++) {
const tutorial::Person& person = address_book.person(i);
cout << "Person ID: " << person.id() << endl;
cout << " Name: " << person.name() << endl;
if (person.has_email()) {
cout << " E-mail address: " << person.email() << endl;
}
for (int j = 0; j < person.phone_size(); j++) {
const tutorial::Person::PhoneNumber& phone_number = person.phone(j);
switch (phone_number.type()) {
case tutorial::Person::MOBILE:
cout << " Mobile phone #: ";
break;
case tutorial::Person::HOME:
cout << " Home phone #: ";
break;
case tutorial::Person::WORK:
cout << " Work phone #: ";
break;
}
cout << phone_number.number() << endl;
}
}
}
// Main function: Reads the entire address book from a file and prints all
// the information inside.
int main(int argc, char* argv[]) {
// Verify that the version of the library that we linked against is
// compatible with the version of the headers we compiled against.
GOOGLE_PROTOBUF_VERIFY_VERSION;
if (argc != 2) {
cerr << "Usage: " << argv[0] << " ADDRESS_BOOK_FILE" << endl;
return -1;
}
tutorial::AddressBook address_book;
{
// Read the existing address book.
fstream input(argv[1], ios::in | ios::binary);
if (!address_book.ParseFromIstream(&input)) {
cerr << "Failed to parse address book." << endl;
return -1;
}
}
ListPeople(address_book);
// Optional: Delete all global objects allocated by libprotobuf.
google::protobuf::ShutdownProtobufLibrary();
return 0;
}
以上就是protobuf的使用过程。保存上面两个文件,并使用g++或VS等工具编译。可以使用Writing创建一个消息文件,再使用Reading读取消息文件。
