導(dǎo)讀
學(xué)習(xí)gRPC一定繞不過proto文件(不清楚什么是 proto 文件的,請(qǐng)移步【gRPC】5 分鐘入門掃盲)。筆者也是gRPC初學(xué),寫這個(gè)短篇的目的非常純粹,就是幫助團(tuán)隊(duì)新人用最短的時(shí)間掌握proto的基本用法,起碼能看懂。所以本文的內(nèi)容不會(huì)面面俱到,一切以實(shí)用為主。
PS:本文最適合熟悉 ts 的同學(xué)。文末會(huì)附上文本版代碼,方便大家 copy。
正文
Talk is cheap, show me the code.沒有什么比直接上代碼對(duì)比更高效清晰的了。
Proto vs TS
小結(jié)
大概的規(guī)則如下:
ProtoTS
messageinterface
serviceclass
repeatedArray\<T>
stringstring
int32/int64/float etc.number
其他類型比較好理解,就不一一列舉了。還有兩點(diǎn)需要特別說(shuō)明:
proto中的字段名會(huì)在 TS 里被轉(zhuǎn)化成小駝峰(camelCase);
repeated修飾的字段名會(huì)被自動(dòng)加List后綴。
看完以上內(nèi)容,相信應(yīng)付日常開發(fā)肯定沒問題了。高級(jí)應(yīng)用就先不展開了,以目前筆者的程度估計(jì)還 cover 不住。
https://gitee.com/numerical-control-system
https://www.thinksaas.cn/user/space/50002/
https://zhuanlan.zhihu.com/p/430534517
https://zhuanlan.zhihu.com/p/430536898
https://zhuanlan.zhihu.com/p/430537972
結(jié)語(yǔ)
也沒什么好總結(jié)的了,來(lái)段最近看的《誡子書》中的一段作為結(jié)尾,然后祝大家身體健康,萬(wàn)事如意吧。
夫君子之行,靜以修身,儉以養(yǎng)德。非淡泊無(wú)以明志,非寧?kù)o無(wú)以致遠(yuǎn)。夫?qū)W須靜也,才須學(xué)也,非學(xué)無(wú)以廣才,非志無(wú)以成學(xué)。淫慢則不能勵(lì)精,險(xiǎn)躁則不能治性。年與時(shí)馳,意與日去,遂成枯落,多不接世,悲守窮廬,將復(fù)何及!——《誡子書》諸葛亮
文本代碼
Proto
syntax ="proto3";package helloworld;message HelloRequest {stringname =1;}message RepeatHelloRequest {stringname =1;? int32 count =2;}message HelloReply {stringmessage =1;}message ListRequest {? repeated int32 id =1;stringnil =2;}message ListReply {? repeatedstringid =1;stringData =2;}message TsMap {stringfeature =1;stringversion =2;stringseries =3;}message MapI {? TsMap test_map1 =1;? TsMap test_map2 =2;? TsMap validate_map =3;}message MapRequest {? MapI map =1;? repeatedstringdo_safety_cars =2;}message MapReply {? MapI map =1;}service Greeter {// unary callrpc SayHello(HelloRequest) returns (HelloReply);? ? rpc ListTest(ListRequest) returns (ListReply);? rpc MapTest(MapRequest) returns (MapReply);}service Stream {// server streaming callrpc SayRepeatHello(RepeatHelloRequest) returns (stream HelloReply);}復(fù)制代碼
TS
http://www.360doc.com/content/21/1104/07/46403850_1002672849.shtml
http://www.360doc.com/content/21/1107/21/46403850_1003191175.shtml
http://www.360doc.com/content/21/1107/21/46403850_1003190908.shtml
https://blog.csdn.net/chunzhenwangluo/article/details/121209719
https://blog.csdn.net/chunzhenwangluo/article/details/121209107
import*asgrpcWebfrom"grpc-web";interfaceHelloRequest {name:string;}interfaceRepeatHelloRequest {name:string;? count:number;}interfaceHelloReply {message:string;}interfaceListRequest {idList:Array;? nil:string;}interfaceListReply {idList:Array;? data:string;}interfaceTsMap {feature:string;? version:string;? series:string;}interfaceMapI {? testMap1?: TsMap;? testMap2?: TsMap;? validateMap?: TsMap;}interfaceMapRequest {? map?: MapI;? doSafetyCarsList:Array;}interfaceMapReply {? map?: MapI;}exportclassGreeterPromiseClient{constructor(hostname:string,? ? credentials?:null| { [index:string]:string},? ? options?:null| { [index:string]:any});? sayHello(? ? request: HelloRequest,? ? metadata?: grpcWeb.Metadata? ):Promise;? listTest(? ? request: ListRequest,? ? metadata?: grpcWeb.Metadata? ):Promise;? mapTest(request: MapRequest, metadata?: grpcWeb.Metadata):Promise;}exportclassStreamPromiseClient{constructor(hostname:string,? ? credentials?:null| { [index:string]:string},? ? options?:null| { [index:string]:any});? sayRepeatHello(? ? request: RepeatHelloRequest,? ? metadata?: grpcWeb.Metadata? ): grpcWeb.ClientReadableStream;}