Reprinted from: http://blog.csdn.net/BeautifulGrils/article/details/52775120
1. Overview of Protocol Buffers:
Protocol Buffers is a lightweight and efficient structured data storage format that can be used for structured data serialization, or serialization. It is very suitable for data storage or RPC data exchange format. A language-independent, platform-independent, and extensible serialized structured data format that can be used in communication protocols, data storage, and other fields. Currently, APIs in C++, Java, and Python are provided.
2. Protocol Buffers installation:
1. Download the installation package of Protocol Buffers:
https://github.com/google/protobuf
(Note: The installation package of this article is on github)
(Or you can use the following link to directly download the installation package of version 2.6.1)
https://github.com/google/protobuf/archive/v2.6.1.zip
2. Installation steps: (You can use xftp to upload the downloaded installation package to linux)
[root@localhost ~]# yum -y install autoconf automake libtool curl make g++ unzip
[root@localhost ~]# unzip protobuf-master.zip
[root@localhost ~]# cd protobuf-master
generate configure The script file of the file, if this step is not performed, the following operations will not pass
[root@localhost protobuf-master]# ./autogen.sh
[root@localhost protobuf-master]# ./configure
The installation directory can be modified through ./configure --prefix= command, unified installation under /usr/local/protobuf
[root@localhost protobuf-master]# ./configure --prefix=/usr/local/protobuf
[root@localhost protobuf-master]# make
[root@localhost protobuf-master]# make check
[root@localhost protobuf-master]# ldconfig # refresh shared library cache.
安装成功
[root@localhost protobuf-master]# protoc -I=./ --cpp_out=./ test.proto
回到你的test.proto文件所在目录使用命令protoc -I=./ --cpp_out=./ 生成C++版本的协议文件,在当前目录中看到.h和.cc文件
[root@ilog2 proto]# ll
-rw-r--r-- 1 root root 18745 Oct 10 11:38 test.pb.cc
-rw-r--r-- 1 root root 9021 Oct 10 11:38 test.pb.h
-rw-r--r-- 1 root root 166 Oct 10 11:35 test.proto
生成JAVA的相关协议文件:
[root@localhost protobuf-master]# protoc -I=./ --java_out=./ test.proto
[root@localhost proto]# protoc --java_out=./ msg.proto
在当前目录中就会以package的后面的名字生成目录,并生成相应的java文件
[root@localhost proto]# ll
total 48
drwxr-xr-x 3 root root 4096 Oct 10 11:52 Feinno
drwxr-xr-x 2 root root 4096 Oct 10 11:55 lm
-rw-r--r-- 1 root root 351 Oct 10 11:48 msg.proto
-rw-r--r-- 1 root root 18745 Oct 10 11:38 test.pb.cc
-rw-r--r-- 1 root root 9021 Oct 10 11:38 test.pb.h
-rw-r--r-- 1 root root 166 Oct 10 11:35 test.proto
[root@localhost proto]# ll lm/
total 28
-rw-r--r-- 1 root root 25966 Oct 10 11:55 Test.java
[root@ilog2 proto]# ll Feinno/Practice/Learn/
total 40
-rw-r--r-- 1 root root 38879 Oct 10 11:52 ProtoBufferPractice.java
3、test.proto文件
package lm;
message helloworld
{
required int32 id = 1; // ID
required string str = 2; // str
optional int32 opt = 3; //optional field
}
4、msg .proto文件
package Feinno.Practice.Learn;
option java_package = "Feinno.Practice.Learn";
option java_outer_classname = "ProtoBufferPractice";
message msgInfo {
required int32 ID = 1;
required int64 GoodID = 2;
required string Url = 3;
required string Guid = 4;
required string Type = 5;
required int32 Order = 6;
}
注释:在上例中,package 名字叫做 lm,定义了一个消息 helloworld,该消息有三个成员,类型为 int32 的 id,另一个为类型为 string 的成员 str。opt 是一个可选的成员,即消息中可以不包含该成员。
以上安装都是默认的安装:
即:
/usr/local/bin (可还行的脚本)
/usr/local/include
/usr/local/lib (使用的依赖包)
三、相关好的学习资源:
1、github源码:
https://github.com/google/protobuf/blob/master/src/README.md
2、一片好的学习资源: