【gRPC】2 分钟学会 Protocol Buffer 语法

导读

学习gRPC一定绕不过proto文件(不清楚什么是 proto 文件的,请移步【gRPC】5 分钟入门扫盲)。笔者也是gRPC初学,写这个短篇的目的非常纯粹,就是帮助团队新人用最短的时间掌握proto的基本用法,起码能看懂。所以本文的内容不会面面俱到,一切以实用为主。

PS:本文最适合熟悉 ts 的同学。文末会附上文本版代码,方便大家 copy。

正文

Talk is cheap, show me the code.没有什么比直接上代码对比更高效清晰的了。

Proto vs TS

小结

大概的规则如下:

ProtoTS

messageinterface

serviceclass

repeatedArray\<T>

stringstring

int32/int64/float etc.number

其他类型比较好理解,就不一一列举了。还有两点需要特别说明:

proto中的字段名会在 TS 里被转化成小驼峰(camelCase)

repeated修饰的字段名会被自动加List后缀。

看完以上内容,相信应付日常开发肯定没问题了。高级应用就先不展开了,以目前笔者的程度估计还 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

结语

也没什么好总结的了,来段最近看的《诫子书》中的一段作为结尾,然后祝大家身体健康,万事如意吧。

夫君子之行,静以修身,俭以养德。非淡泊无以明志,非宁静无以致远。夫学须静也,才须学也,非学无以广才,非志无以成学。淫慢则不能励精,险躁则不能治性。年与时驰,意与日去,遂成枯落,多不接世,悲守穷庐,将复何及!——《诫子书》诸葛亮

文本代码

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);}复制代码

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;}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容