Protocol Buffers是Google开源的序列化库,具有平台无关,高性能,兼容性好等有点。它是一种轻便高效的结构化数据存储格式,可以用于结构化数据序列化/反序列化。由于Protocol Buffers具有以上的特点,所以它很适合做数据存储或RPC的数据交换格式,常用作通信协议、数据存储等领域的与语言无关、平台无关、可扩展的序列化结构数据格式。目前支持C++、Java、Python三种语言。在Google内部,几乎所有的RPC协议和文件格式都是采用Protocol Buffers。
Protocol Buffers优点:
1、平台无关、语言无关
2、高性能,解析速度是XML的很多倍
3、体积小
4、使用简单
5、兼容性好
编写Protocol Buffers步骤:
1、定义一个.proto格式的文件
2、使用Google提供的Protocol Buffers编译器生成特定语言(Java、C++、Python)的代码文件
3、使用Protocol Buffers提供的API来编写应用程序。
下面我们来一步一步安装Protocol buffers并创建Protocol buffers应用程序:
下载:
https://developers.google.com/protocol-buffers/docs/downloads
https://code.google.com/p/protobuf/downloads/list
源码:http://sourceforge.net/projects/protobuf/files/protobuf-2.6.0.zip/download
编译后应用文件:http://sourceforge.net/projects/protobuf/files/protoc-2.6.0-win32.zip/download
API :https://developers.google.com/protocol-buffers/docs/reference/java/index
jar : http://mvnrepository.com/artifact/com.google.protobuf/protobuf-java/2.6.0
github : https://github.com/google/protobuf
利用eclipse创建一个工程protobuftest,并添加上面步骤下载的protobuf-java-2.6.0.jar的jar包
定义消息格式文件person.proto
假设我们的文件路径为E:/workspace-indigo/protobuftest/ProtoTest/src/person.proto该文件描述了通讯录中某个人的基本信息,内容如下:
package pro;
option java_package="com.lvcy.pro";
option java_outer_classname="PersonProtos";
message Persion{
required string name=1;
required int32 id=2;
optional string email=3;
message PhoneNumber{
required string number=1;
optional int32 type=2;
}
required PhoneNumber phone=4;
}
使用Google提供的Protocol Buffers编译器生成java语言
终端定位到项目的src目录下,如下所示:
然后再刷新之前创建的项目我们就可以看见生成了一个com.lvcy.pro包,里面定义了一个PersonProtos类,项目结构如下所示:
使用protocol buffers提供的API编写应用程序
该例子创建了一个Person实例,先将对象保存到文件test.txt中,之后从文件中输入再打印出来。
package com.lvcy.pro; import java.io.FileInputStream; import java.io.FileOutputStream; import com.lvcy.pro.PersonProtos.Persion; public class Program { public static void main(String[] args) throws Exception{ Persion persion=Persion.newBuilder().setName("Lvcy").setEmail("123456789@qq.com").setId(10) .setPhone(Persion.PhoneNumber.newBuilder().setNumber("12345678900").setType(1)).build(); FileOutputStream fileOutputStream=new FileOutputStream("test.txt"); persion.writeTo(fileOutputStream); FileInputStream inputStream=new FileInputStream("test.txt"); Persion persion2=Persion.parseFrom(inputStream); System.out.println(persion2); } }
输出:
name: "Lvcy"
id: 10
email: "123456789@qq.com"
phone {
number: "12345678900"
type: 1
}
参考:http://www.lvcy.net/?post=164
相关推荐
Google Protocol Buffers 在 c# 中的应用
Protocol Buffers 2.4.1 jar
Protocol Buffers What is it? Protocol Buffers are a way of encoding structured data in an efficient yet extensible format. Google uses Protocol Buffers for almost all of its internal RPC protocols and...
google protocol buffers 官网中文教程
中文翻译Google Protocol Buffers中文教程中文翻译Google Protocol Buffers中文教程中文翻译Google Protocol Buffers中文教程中文翻译Google Protocol Buffers中文教程
google Protocol Buffers
Google.ProtocolBuffers.dll类库
ProtocolBuffers在即时通讯系统中的应用研究_田源.pdf
Google.ProtocolBuffers动态库反编译生成的源代码,用于学习。
Protocol Buffers Java开发包(protobuf-java-2.3.0.jar)
Protocol Buffers v3.0.0-alpha-1(Java) release 版本和源代码
Protocol Buffers - 谷歌的数据交换格式
This project contains the implementation of Protocol Buffers for Delphi. From the project was implemented limited functionality necessary for a specific project. At that time, I do not see any sense ...
ProtocolBuffers 英文版PDF 不懂别下
在vs2015下编译生成的,最新版本的google的protocol buffers数据交互协议库,可以用于诸如网络传输、配置文件、数据存储等诸多领域。源码里包含了x86和x64编译的库以及一个x86下的可编译运行的测试Demo。
Google Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化。它很适合做数据存储或 RPC 数据交换格式。可用于通讯协议、数据存储等领域的语言无关、平台无关、可扩展的...
Protocol Buffers 2.5.0 bin jar
最新的Protocol Buffers 3.1.0及工具