鍒濊瘑Protobuf
Posted Java鎯呮姤灞€
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鍒濊瘑Protobuf相关的知识,希望对你有一定的参考价值。
馃懆鈥嶐煉?灞€闀垮璇伙細鈥?/span>Google Protocol Buffer( 绠€绉?Protobuf) 鏄?Google 鍏徃鍐呴儴鐨勬贩鍚堣瑷€鏁版嵁鏍囧噯 ,鏄竴绉嶈交渚块珮鏁堢殑缁撴瀯鍖栨暟鎹瓨鍌ㄦ牸寮忥紝鍙互鐢ㄤ簬缁撴瀯鍖栨暟鎹覆琛屽寲锛屾垨鑰呰搴忓垪鍖?灏?鏁版嵁缁撴瀯鎴栧璞?杞崲鎴?浜岃繘鍒朵覆 鐨勮繃绋?)銆傚畠寰堥€傚悎鍋?span>鏁版嵁瀛樺偍鎴?RPC 鏁版嵁浜ゆ崲鏍煎紡銆傚彲鐢ㄤ簬閫氳鍗忚銆佹暟鎹瓨鍌ㄧ瓑棰嗗煙鐨勮瑷€鏃犲叧銆佸钩鍙版棤鍏炽€佸彲鎵╁睍鐨勫簭鍒楀寲缁撴瀯鏁版嵁鏍煎紡
鈥?/span>
protocol buffers 璇炵敓涔嬪垵鏄负浜嗚В鍐虫湇鍔″櫒绔柊鏃у崗璁?楂樹綆鐗堟湰)鍏煎鎬ч棶棰橈紝鍚嶅瓧涔熷緢浣撹创锛屸€滃崗璁紦鍐插尯鈥濄€傚彧涓嶈繃鍚庢湡鎱㈡參鍙戝睍鎴愮敤浜庝紶杈撴暟鎹€?/em>
绗旇€呮墍鍦ㄧ殑360骞垮憡鎶曟斁锛?浜挎潯鍟嗗搧淇℃伅鐨勬暟鎹叏閮ㄩ噰鐢≒B鏍煎紡瀛樺偍銆佷紶杈撱€?/p>
Protobuf 鐨勪紭鐐?/span>
-
鏇村皬鈥斺€斿簭鍒楀寲鍚庯紝鏁版嵁澶у皬鍙缉灏忕害3鍊?/p>
-
鏇村揩鈥斺€斿簭鍒楀寲閫熷害鏇村揩锛屾瘮xml鍜孞SON蹇?0-100鍊嶏紝浣撶Н缂╁皬鍚庯紝浼犺緭鏃讹紝甯﹀涔熶細浼樺寲
-
鏇寸畝鍗曗€斺€攑roto缂栬瘧鍣紝鑷姩杩涜搴忓垪鍖栧拰鍙嶅簭鍒楀寲
-
缁存姢鎴愭湰浣庘€斺€旇法骞冲彴銆佽法璇█锛屽骞冲彴浠呴渶瑕佺淮鎶や竴濂楀璞″崗璁紙.proto锛?/p>
-
鍙墿灞曗€斺€斺€滃悜鍚庘€濆吋瀹规€уソ锛屼笉蹇呯牬鍧忓凡閮ㄧ讲鐨勩€佷緷闈犫€滆€佲€濇暟鎹牸寮忕殑绋嬪簭灏卞彲浠ュ鏁版嵁缁撴瀯杩涜鍗囩骇
-
鍔犲瘑鎬уソ鈥斺€擧TTP浼犺緭鍐呭鎶撳寘鍙兘鐪嬪埌瀛楄妭
鍦ㄤ紶杈撴暟鎹噺澶с€佺綉缁滅幆澧冧笉绋冲畾鐨勬暟鎹瓨鍌ㄥ拰RPC鏁版嵁浜ゆ崲鍦烘櫙姣旇緝鍚堥€?/p>
Protobuf 鐨勪笉瓒?/span>
-
鍔熻兘绠€鍗曪紝鏃犳硶鐢ㄦ潵琛ㄧず澶嶆潅鐨勬蹇? -
閫氱敤鎬ц緝宸紝XML鍜孞SON宸叉垚涓哄绉嶈涓氭爣鍑嗙殑缂栧啓宸ュ叿锛宲b鍙槸geogle鍐呴儴浣跨敤 -
鑷В閲婃€у樊锛屼互浜岃繘鍒舵暟鎹祦鏂瑰紡瀛樺偍锛堜笉鍙锛夛紝闇€瑕侀€氳繃.proto鏂囦欢鎵嶅彲浠?
瀹樼綉 Protocol Buffer Basics: Java https://developers.google.com/protocol-buffers/docs/javatutorial
Hello World
1. 瀹氫箟 .proto
鏂囦欢鐨勬秷鎭牸寮忥紙浣犲笇鏈涘瓨鍌ㄧ殑鏁版嵁鏍煎紡鎻忚堪鏂囦欢锛?/h5>
syntax = "proto2";
package tutorial;
option java_package = "com.example.tutorial";
option java_outer_classname = "AddressBookProtos";
//娑堟伅妯″瀷
message Person {
//娑堟伅瀵硅薄鐨勫瓧娈碉細瀛楁淇グ绗?瀛楁绫诲瀷+瀛楁鍚嶇О+鏍囪瘑鍙凤紙閫氳繃浜岃繘鍒舵牸寮忓敮涓€鏍囪瘑姣忎釜瀛楁锛屼笉鍙樺彲锛?br> 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 phones = 4;
}
message AddressBook {
repeated Person people = 1;
}
package tutorial;
option java_package = "com.example.tutorial";
option java_outer_classname = "AddressBookProtos";
//娑堟伅妯″瀷
message Person {
//娑堟伅瀵硅薄鐨勫瓧娈碉細瀛楁淇グ绗?瀛楁绫诲瀷+瀛楁鍚嶇О+鏍囪瘑鍙凤紙閫氳繃浜岃繘鍒舵牸寮忓敮涓€鏍囪瘑姣忎釜瀛楁锛屼笉鍙樺彲锛?br> 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 phones = 4;
}
message AddressBook {
repeated Person people = 1;
}
鈽嗏槅鈽嗘敞锛?/p>
-
syntax = "proto2"锛氭寚鏄庣増鏈? -
package锛歅B鐨勮嚜宸辩殑鍖呭悕锛岄槻姝笉鍚?nbsp; .proto
椤圭洰闂村懡鍚?鍙戠敓鍐茬獊 -
java_package锛氱敓鎴恓ava绫荤殑鍖呭悕,濡備笉鏄惧紡鎸囧畾锛岄粯璁ゅ寘鍚嶄负锛氭寜鐓у簲鐢ㄥ悕绉板€掑簭鏂瑰紡杩涜鎺掑簭 -
java_outer_classname锛氱敓鎴?java绫荤殑绫诲悕,濡備笉鏄惧紡鎸囧畾锛屽垯榛樿涓烘妸.proto鏂囦欢鍚嶈浆鎹负棣栧瓧姣嶅ぇ鍐欐潵鐢熸垚 -
message: 浣犵殑娑堟伅鏍煎紡锛屽悇鏁版嵁绫诲瀷锛? bool
,int32
,float
,double
,string
锛?enum
... 锛夊瓧娈电殑闆嗗悎锛屽湪涓€涓?proto鏂囦欢涓彲浠ュ畾涔夊涓猰essage锛屼竴涓猰essage閲屼篃鍙互瀹氫箟鍙﹀涓€涓猰essage(鐩稿綋浜巎ava鐨勭被锛屽綋鐒朵篃鍙互鏈夊唴閮ㄧ被) -
褰撶劧PB涔熸槸鏀寔鍜宩ava涓€鏍风殑 import
鐨勶紝import "xxx.proto";
-
鍍忔瘡涓瓧娈典篃蹇呴』鏈変慨楗扮锛孭B鎻愪緵鐨勫瓧娈典慨楗扮鏈?绉? -
required锛氬繀濉? -
optional锛氬彲閫? -
repeated 锛氬彲閲嶅瀛楁锛屽彲鏀鹃泦鍚? -
鏍囪瘑鍙凤細閫氳繃浜岃繘鍒舵牸寮忓敮涓€鏍囪瘑姣忎釜瀛楁 锛屼娇鐢ㄥ悗灏变笉鑳藉鍐嶆敼鍙? -
鑼冨洿 [1,15] 鏍囪瘑鍙风殑瀛楁 鍦ㄧ紪鐮佹椂鍗犵敤1涓瓧鑺傦紱 -
鑼冨洿 [16,2047] 鏍囪瘑鍙风殑瀛楁 鍦ㄧ紪鐮佹椂鍗犵敤2涓瓧鑺? -
涓洪绻佸嚭鐜扮殑 娑堟伅瀛楁 淇濈暀 [1,15] 鐨勬爣璇嗗彿 -
鏍囪瘑鍙蜂娇鐢ㄨ寖鍥达細[1,2鐨?9娆℃柟 - 1] -
涓嶅彲浣跨敤 [19000锛?9999] 鏍囪瘑鍙凤紝 鍥犱负 Protobuf
鍗忚瀹炵幇涓杩欎簺鏍囪瘑鍙疯繘琛屼簡棰勭暀銆傚亣鑻ヤ娇鐢紝鍒欎細鎶ラ敊 -
姣忎釜瀛楁鍦ㄨ繘琛岀紪鐮佹椂閮戒細鍗犵敤鍐呭瓨锛岃€?鍗犵敤鍐呭瓨澶у皬 鍙栧喅浜?鏍囪瘑鍙凤細
2. 浣跨敤 protocol buffer 缂栬瘧鍣紙涓嬭浇鍦板潃锛歨ttps://github.com/protocolbuffers/protobuf/releases )
winows鐨勮瘽 cmd鍒扮紪璇戝櫒瀹夎鐩綍鐨刡in鐩綍涓紝鎵ц protoc.exe -h 锛圗:\learning\protoc-3.9.0-win64\bin>protoc.exe -h锛夛紝鍙互鐪嬪埌鍙傛暟璇存槑銆?/h5>
E:\learning\protoc-3.9.0-win64\bin>protoc.exe -I=E:\learning\ --java_out=E:\learning\ E:\learning\addressbook.proto
瀹為檯浣跨敤涓?
protoc.exe -I=E:\learn-workspace\starfish\starfish-learn\src\main\java\priv\starfish\ProtocolBuffers\proto\ --java_out=E:\learn-workspace\starfish\starfish-learn\src\main\java E:\learn-workspace\starfish\starfish-learn\src\main\java\priv\starfish\ProtocolBuffers\proto\addressbook.proto锛?/strong>
3. 閫氳繃 Java protocol buffer API 璇诲啓娑堟伅鏍煎紡
package priv.starfish.ProtocolBuffers;
import com.google.protobuf.InvalidProtocolBufferException;
import priv.starfish.ProtocolBuffers.AddressBookProtos.Person;
import priv.starfish.ProtocolBuffers.AddressBookProtos.AddressBook;
import java.util.Arrays;
/**
* @author: starfish
* @date: 2019/7/24 14:39
* @description:
*/
public class HelloProto {
public static void main(String[] args) {
Person person = Person.newBuilder()
.setId(123)
.setName("starfish")
.setEmail("starfish@126.cn")
.addPhones(AddressBookProtos.Person.PhoneNumber.newBuilder()
.setType(AddressBookProtos.Person.PhoneType.HOME)
.setNumber("13555555555")
.build())
.build();
System.out.println(person.toString());
System.out.println(person.isInitialized());
try {
//搴忓垪鍖栧拰鍙嶅簭鍒楀寲
System.out.println(Arrays.toString(person.toByteArray()));
System.out.println(person.toByteString());
Person newPerson = Person.parseFrom(person.toByteArray());
System.out.println(newPerson);
newPerson = Person.parseFrom(person.toByteString());
System.out.println(newPerson);
} catch (InvalidProtocolBufferException e) {
e.printStackTrace();
}
// 鍚戝湴鍧€绨挎坊鍔犱袱鏉erson淇℃伅
AddressBook.Builder books = AddressBook.newBuilder();
books.addPeople(person);
books.addPeople(Person.newBuilder(person).setEmail("xin@163.com")
.build());
System.out.println("AddressBook瀵硅薄淇℃伅锛?);
System.out.println(books.build());
}
}
缂栬瘧鍚庣敓鎴愮殑java绫绘槸涓嶅彲鍙樼殑锛岀被浼糺ava鐨凷tring锛屼笉鍙慨鏀?/p>
鏋勯€犳秷鎭紝蹇呴』鍏堟瀯閫犱竴涓猙uilder锛岀劧鍚巗et灞炴€э紙鍙互涓€杩炰覆鐨剆et锛夛紝鏈€鍚庤皟鐢╞uild() 鏂规硶銆?/p>
PB甯哥敤鏂规硶
-
isInitialized()
: 妫€鏌ュ繀濉瓧娈碉紙required锛夋槸鍚︽湁set鍊?/p>
-
toString()
: 杩斿洖message鐨勫彲璇诲瓧绗︿覆鏍煎紡
-
mergeFrom(Message other)
: 鍚堝苟message
-
clear()
: 娓呯┖瀛楁鍊?/p>
-
byte[] toByteArray();
: 搴忓垪鍖杕essage,杩斿洖瀛楄妭鏁扮粍
-
MessageType parseFrom(byte[] data);
: 瑙f瀽缁欏畾鐨勫瓧鑺傛暟缁?/p>
-
void writeTo(OutputStream output);
: 搴忓垪鍖杕essage骞跺啓鍏ヨ緭鍑烘祦OutputStream
.
-
MessageType parseFrom(InputStream input);
: 浠庤緭鍏ユ祦 InputStream
璇诲彇骞惰В鏋恗essage
Reference:
https://github.com/halfrost/Halfrost-Field/blob/master/contents/Protocol/Protocol-buffers-encode.md#鍏?protocol-buffer-缂栫爜鍘熺悊
import com.google.protobuf.InvalidProtocolBufferException;
import priv.starfish.ProtocolBuffers.AddressBookProtos.Person;
import priv.starfish.ProtocolBuffers.AddressBookProtos.AddressBook;
import java.util.Arrays;
/**
* @author: starfish
* @date: 2019/7/24 14:39
* @description:
*/
public class HelloProto {
public static void main(String[] args) {
Person person = Person.newBuilder()
.setId(123)
.setName("starfish")
.setEmail("starfish@126.cn")
.addPhones(AddressBookProtos.Person.PhoneNumber.newBuilder()
.setType(AddressBookProtos.Person.PhoneType.HOME)
.setNumber("13555555555")
.build())
.build();
System.out.println(person.toString());
System.out.println(person.isInitialized());
try {
//搴忓垪鍖栧拰鍙嶅簭鍒楀寲
System.out.println(Arrays.toString(person.toByteArray()));
System.out.println(person.toByteString());
Person newPerson = Person.parseFrom(person.toByteArray());
System.out.println(newPerson);
newPerson = Person.parseFrom(person.toByteString());
System.out.println(newPerson);
} catch (InvalidProtocolBufferException e) {
e.printStackTrace();
}
// 鍚戝湴鍧€绨挎坊鍔犱袱鏉erson淇℃伅
AddressBook.Builder books = AddressBook.newBuilder();
books.addPeople(person);
books.addPeople(Person.newBuilder(person).setEmail("xin@163.com")
.build());
System.out.println("AddressBook瀵硅薄淇℃伅锛?);
System.out.println(books.build());
}
}
isInitialized()
: 妫€鏌ュ繀濉瓧娈碉紙required锛夋槸鍚︽湁set鍊?/p>
toString()
: 杩斿洖message鐨勫彲璇诲瓧绗︿覆鏍煎紡
mergeFrom(Message other)
: 鍚堝苟message
clear()
: 娓呯┖瀛楁鍊?/p>
byte[] toByteArray();
: 搴忓垪鍖杕essage,杩斿洖瀛楄妭鏁扮粍
MessageType parseFrom(byte[] data);
: 瑙f瀽缁欏畾鐨勫瓧鑺傛暟缁?/p>
void writeTo(OutputStream output);
: 搴忓垪鍖杕essage骞跺啓鍏ヨ緭鍑烘祦OutputStream
.
MessageType parseFrom(InputStream input);
: 浠庤緭鍏ユ祦 InputStream
璇诲彇骞惰В鏋恗essage
以上是关于鍒濊瘑Protobuf的主要内容,如果未能解决你的问题,请参考以下文章
Photon Server鍒濊瘑(涓? ---ORM鏄犲皠鏀硅繘
銆愯浆銆?springBoot---蹇€熷垱寤洪」鐩紝鍒濊Вjackson
protobuf 下载编译,拷贝,proto文件生成c# c++代码