【Protocol Buffer】简介和安装

 转载:https://blog.51cto.com/u_15352776/3768328

01. Protocol Buffer简介

protobuf也叫protocol buffer是google 的一种数据交换的格式,它独立于语言,独立于平台。google 提供了多种语言的实现:java、c#、c++、go 和 python,每一种实现都包含了相应语言的编译器以及库文件。

由于它是一种二进制的格式,比使用 xml 、json进行数据交换快许多。可以把它用于分布式应用之间的数据通信或者异构环境下的数据交换。作为一种效率和兼容性都很优秀的二进制数据传输格式,可以用于诸如网络传输、配置文件、数据存储等诸多领域。

02. Protocol Buffer优缺点

优点

  • 性能好/效率高
  • 代码生成机制
  • 支持“向后兼容”和“向前兼容”
  • 支持多种编程语言

缺点

  • 应用不够广(相比xml和json)
  • 二进制格式导致可读性差
  • 缺乏自描述

03. Protocol Buffer安装

源码包下载:Protocol Buffer源码包下载
源码包下载:protobuf-master(20191007).zip
源码包下载:protobuf-cpp-3.9.2.tar.gz

安装环境:Ubuntu 18.04
3.0 安装所需工具

deng@itcast:~/protobuf-master$ sudo apt-get install autoconf automake libtool curl make g++ unzip

3.1 解压源代码

deng@itcast:~$ unzip protobuf-master.zip

3.2 进入protobuf-master目录

deng@itcast:~$ cd protobuf-master/
deng@itcast:~/protobuf-master$ 

3.3 自动生成configure配置文件

deng@itcast:~/protobuf-master$ pwd
/home/deng/protobuf-master
deng@itcast:~/protobuf-master$ ./autogen.sh 

3.4 生成Makefile

deng@itcast:~/protobuf-master$ ./configure 

3.5 编译源代码

deng@itcast:~/protobuf-master$ make -j4

3.6 安装

deng@itcast:~/protobuf-master$ sudo make install 

3.7 刷新动态库

deng@itcast:~/protobuf-master$ sudo ldconfig
deng@itcast:~/protobuf-master$ 

温馨提示:

源码包中的src/README.md, 有详细的安装说明。

04. Protocol Buffer测试

4.1 测试examples中程序
在源代码包中,有个examples文件夹,把里面的addressbook.proto、add_person.cc、list_people.cc拷贝出来。
img

deng@itcast:~/protobuf-master/examples$ mkdir ~/test01 deng@itcast:~/protobuf-master/examples$ cp add_person.cc list_people.cc addressbook.proto ~/test01/ deng@itcast:~/protobuf-master/examples$

4.2 编译 .proto文件

deng@itcast:~/test01$ ls
add_person.cc  addressbook.proto  list_people.cc
deng@itcast:~/test01$ protoc addressbook.proto --cpp_out=./
deng@itcast:~/test01$ ls
add_person.cc      addressbook.pb.h   list_people.cc
addressbook.pb.cc  addressbook.proto
deng@itcast:~/test01$ 

说明:

  • protoc:protobuf自带的编译工具,将.proto文件生成指定的类

  • –cpp_out:指定输出特定的语言和路径

    通过protoc工具编译.proto文件时,编译器将生成所选择语言的代码,这些代码可以操作在.proto文件中定义的消息类型,包括获取、设置字段值,将消息序列化到一个输出流中,以及从一个输入流中解析消息。
    对C++来说,编译器会为每个.proto文件生成一个.h文件和一个.cc文件,.proto文件中的每一个消息有一个对应的类。

4.3 写文件(序列化)测试
如果没有pkg-config工具,先安装pkg-config工具

deng@itcast:~/test01$ sudo apt install pkg-config

编译代码

deng@itcast:~/test01$ g++ add_person.cc addressbook.pb.cc `pkg-config --cflags --libs protobuf`
deng@itcast:~/test01$ 

反单引号:反引号的作用就是将反引号内的linux命令执行
pkg-config 是通过库提供的一个.pc文件获得库的各种必要信息的,包括版本信息、编译和连接需要的参数等。
pkg-config –cflags protobuf:列出指定共享库的预处理和编译flags

deng@itcast:~/test01$ pkg-config --cflags protobuf
-pthread -I/usr/local/include
deng@itcast:~/test01$ pkg-config --libs protobuf
-L/usr/local/lib -lprotobuf
deng@itcast:~/test01$ pkg-config --cflags --libs protobuf
-pthread -I/usr/local/include -L/usr/local/lib -lprotobuf
deng@itcast:~/test01$ 

运行可执行文件

deng@itcast:~/test01$ ./a.out pb.test
pb.test: File not found.  Creating a new file.
Enter person ID number: 1
Enter name: deng
Enter email address (blank for none): deng@itcast.cn
Enter a phone number (or leave blank to finish): 110
Is this a mobile, home, or work phone? mobile
Enter a phone number (or leave blank to finish): 120
Is this a mobile, home, or work phone? home
Enter a phone number (or leave blank to finish): 
deng@itcast:~/test01$ ls
add_person.cc      addressbook.pb.h   a.out           pb.test
addressbook.pb.cc  addressbook.proto  list_people.cc
deng@itcast:~/test01$ 

img
pb.test文件内容如下

deng@itcast:~/test01$ od -x pb.test 
0000000 300a 040a 6564 676e 0110 0e1a 6564 676e
0000020 6940 6374 7361 2e74 6e63 0522 030a 3131
0000040 2230 0a07 3103 3032 0110 062a a508 bd9f
0000060 05ec
0000062
deng@itcast:~/test01$ 

4.4 读文件(反序列化)测试

deng@itcast:~/test01$ g++ list_people.cc addressbook.pb.cc `pkg-config --cflags --libs protobuf`
deng@itcast:~/test01$ ./a.out pb.test
Person ID: 1
  Name: deng
  E-mail address: deng@itcast.cn
  Mobile phone #: 110
  Home phone #: 120
  Updated: 2019-09-28T12:18:45Z
deng@itcast:~/test01$ 

05. 参考

5.1 protobuf源码下载: https://github.com/protocolbuffers/protobuf
5.2 protobuf官方参考文档:https://developers.google.cn/protocol-buffers/
5.3 protobuf快速入门文档:https://developers.google.cn/protocol-buffers/docs/tutorials
5.4 protobuf不同语言源码下载:https://github.com/protocolbuffers/protobuf/releases
5.5 百度百科:[https://baike.baidu.com/item/protocol+buffer/1664400?fr=aladdin](https://baike.baidu.com/item/protocol buffer/1664400?fr=aladdin)
5.6 简书博客:https://www.jianshu.com/p/746bb9d81380
5.7 IBM技术文章:https://www.ibm.com/developerworks/cn/linux/l-cn-gpb/
5.8 中文参考手册(翻译):https://www.cntofu.com/book/116/guide/index.md

Comments

Popular posts from this blog

微服务科普

ESXi添加USB硬盘作为数据存储

油焖大虾菜谱