概念介紹
Protocol buffers 是google公司的與語言無關、與平台無關的、可擴張的為序列化話結構數據,就像xml一樣,辦事更加的小巧、快速、簡單。Protocol buffers 目前支持語言有Java,Python和C++。
為什么不用XML
Protocol buffers在序列化結構數據方便比XML有很多的有點。Protocolbuffers
- 更加簡單
- 比xml小3-10倍
- 比xml快20-100倍
- 比xml更少的歧義
- 以編程方式生成數據訪問類更加容易
前期准備(下載安裝protocol buffers)
官網地址:https://developers.google.com/protocol-buffers/
由於google是被牆的,大家可以自行翻牆,這里推薦一個網站可以直接通過添加hosts方式實現翻牆(https://laod.cn/hosts/2016-google-hosts.html)
下載地址:https://github.com/google/protobuf/releases/tag/v3.0.0
下載 protobuf-java-3.0.0.zip和protoc-3.0.0-win32.zip
下載完成之后將兩個壓縮包分別解壓。
Eclipse安裝protocol buffers dt插件
help->install New software...
install Xtext 地址:http://download.eclipse.org/modeling/tmf/xtext/updates/composite/releases/
isntall protobuf-dt 地址:http://junit.github.io/protobuf-dt/git/update-site/site.xml
安裝成功之后會出現如下圖的一個菜單:

配置說明如下圖:


編譯protobuf的jar包
由於protobuf是通過maven管理的,並且只提供了源代碼,需要自行執行打包。
將protoc.exe文件拷貝到解壓之后的protobuf-3.0.0\src目錄下
切換到protobuf-3.0.0\java目錄下,執行mvn install

安裝成功之后如下圖所示:

現在就可以開發protobuf項目了
1、添加protobuf依賴
<dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>3.0.0</version> </dependency>
2、編寫person.proto文件
package proto; option java_package="com.wjg.base.protobuf.bean"; option java_outer_classname = "PersonProto"; message Person{ required string name = 1; required int32 age =2; required string birthday = 3; required bool man = 4; enum PhoneType{ MOBILE=0; HOME=1; WORK=2; } message PhoneNumber{ required string number=1; optional PhoneType type =2[default = HOME]; } repeated PhoneNumber phone =5; }
保存文件之后就會在com.wjg.base.protobuf.bean包下生成PersonProto類
現在就可以開始測試了新建junit測試類
/** * */ package com.wjg.base.protobuf; import org.junit.Test; import com.google.protobuf.InvalidProtocolBufferException; import com.wjg.base.protobuf.bean.PersonProto; import com.wjg.base.protobuf.bean.PersonProto.Person; /** * @author ghost * @version 創建時間:2016年11月23日 下午5:23:59 類說明 */ public class PersonProtoTest { /** * 測試序列化 */ @Test public void testSerialize() { PersonProto.Person.Builder builder = PersonProto.Person.newBuilder(); builder.setAge(23); builder.setBirthday("2016-11-23"); builder.setName("ghost"); builder.setMan(true); builder.addPhone(Person.PhoneNumber.newBuilder() .setNumber("010-34783871").setType(Person.PhoneType.MOBILE)); Person person = builder.build(); System.out.println(person.toString()); } /** * 測試反序列化 * * @throws InvalidProtocolBufferException */ @Test public void testDeserialize() throws InvalidProtocolBufferException { PersonProto.Person.Builder builder = PersonProto.Person.newBuilder(); builder.setAge(23); builder.setBirthday("2016-12-23"); builder.setName("ghostman"); builder.setMan(true); Person person = builder.build(); Person newPerson = Person.parseFrom(person.toByteArray()); System.out.println(newPerson.toString()); } }
項目結構如下圖所示:

protobuf java初體驗已經結束。
參考資料如下:
https://developers.google.com/protocol-buffers/docs/javatutorial
https://github.com/google/protobuf/
http://www.cnblogs.com/ungshow/archive/2011/12/27/2303257.html
http://blog.csdn.net/caisini_vc/article/details/5599468
注:
發現一個封裝了protobuf文件操作的步驟,可以直接使用java注解定義字段類型即可。github地址如下,有興趣的可以自行研究:
https://github.com/jhunters/jprotobuf
