Posted Java3y

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相关的知识,希望对你有一定的参考价值。

鏈枃鍏紬鍙锋潵婧愶細JavaKeeper    浣滆€咃細Java澶х尶甯?/span>
馃懆鈥嶐煉?灞€闀垮璇伙細鈥?/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;
}

鈽嗏槅鈽嗘敞锛?/p>

  • syntax = "proto2"锛氭寚鏄庣増鏈?
  • package锛歅B鐨勮嚜宸辩殑鍖呭悕锛岄槻姝笉鍚?nbsp; .proto 椤圭洰闂村懡鍚?鍙戠敓鍐茬獊
  • java_package锛氱敓鎴恓ava绫荤殑鍖呭悕,濡備笉鏄惧紡鎸囧畾锛岄粯璁ゅ寘鍚嶄负锛氭寜鐓у簲鐢ㄥ悕绉板€掑簭鏂瑰紡杩涜鎺掑簭
  • java_outer_classname锛氱敓鎴?java绫荤殑绫诲悕,濡備笉鏄惧紡鎸囧畾锛屽垯榛樿涓烘妸.proto鏂囦欢鍚嶈浆鎹负棣栧瓧姣嶅ぇ鍐欐潵鐢熸垚
  • message: 浣犵殑娑堟伅鏍煎紡锛屽悇鏁版嵁绫诲瀷锛? boolint32floatdouble,   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 鍗忚瀹炵幇涓杩欎簺鏍囪瘑鍙疯繘琛屼簡棰勭暀銆傚亣鑻ヤ娇鐢紝鍒欎細鎶ラ敊
    • 姣忎釜瀛楁鍦ㄨ繘琛岀紪鐮佹椂閮戒細鍗犵敤鍐呭瓨锛岃€?鍗犵敤鍐呭瓨澶у皬 鍙栧喅浜?鏍囪瘑鍙凤細
浠€涔堬紵浣犵珶鐒惰繕涓嶇煡閬?Protobuf 锛?> 
 </figure> 
 <h5 data-tool=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>

浠€涔堬紵浣犵珶鐒惰繕涓嶇煡閬?Protobuf 锛?> 
  <figcaption class= pd-idea-screenshot
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-缂栫爜鍘熺悊

鍚勭被鐭ヨ瘑鐐规€荤粨

涓嬮潰鐨勬枃绔犻兘鏈夊搴旂殑鍘熷垱绮剧編PDF锛屽湪鎸佺画鏇存柊涓紝鍙互鏉ユ壘鎴戝偓鏇磣

  • Hibernate
  • AJAX
  • Redis
  • ......

鍘熷垱鐢靛瓙涔?/span>
浠€涔堬紵浣犵珶鐒惰繕涓嶇煡閬?Protobuf 锛?></p><p class=鍘熷垱鎬濈淮瀵煎浘

浠€涔堬紵浣犵珶鐒惰繕涓嶇煡閬?Protobuf 锛?></span></strong></p><p class=

浠€涔堬紵浣犵珶鐒惰繕涓嶇煡閬?Protobuf 锛?></td> 
                <td width=

鎴戞槸涓夋锛屼竴涓兂瑕佸彉寮虹殑鐢蜂汉锛屾劅璋㈠ぇ瀹剁殑鐐硅禐鏀惰棌鍜岃浆鍙戯紝涓嬫湡瑙併€?/strong>

以上是关于的主要内容,如果未能解决你的问题,请参考以下文章

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板

VSCode自定义代码片段6——CSS选择器

VSCode自定义代码片段——声明函数

VSCode自定义代码片段8——声明函数