takeaway
Learning gRPC must be around the PROto file (not clear what is proto file, please go to [gRPC] 5 minutes introduction to literacy). The author is also a beginner of gRPC. The purpose of writing this short story is very pure, which is to help the new team members master the basic usage of Proto in the shortest time, or at least understand it. Therefore, the content of this article will not be exhaustive, everything is mainly practical.
PS: This article is most suitable for students who are familiar with TS. At the end of the article will be attached to this version of the code, easy to copy.
The body of the
Talk is cheap, show me the code. Nothing is more efficient and clear than a direct code comparison.
Proto vs TS
summary
The general rules are as follows:
Proto | TS |
---|---|
message |
interface |
service |
class |
repeated |
Array\<T> |
string |
string |
int32/int64/float etc. |
number |
Other types are easier to understand, so I won’t list them all. Two more points need to be noted:
proto
The field name in TS is converted toSmall camelCase;repeated
The modified field name is automatically addedList
The suffix.
After reading the above content, I believe that there is no problem with daily development. Advanced applications will not be launched, with the current extent of the author is estimated to cover not.
conclusion
There’s nothing left to conclude, so I’ll end with a passage from the Commandments that I read recently, and then I wish you all good health and good luck in everything.
The gentleman’s trip, quiet to cultivate morality, thrift to raise virtue. Not indifferent without understanding, not quiet beyond reach. Husband learning must be static also, only to learn also, not learn beyond wide only, not beyond into learning. Slow sex can not stimulate the essence, risk impetuous can not cure sex. Year and time chi, meaning and day to go, then into withered, do not meet the world, sad shou poor lu, will be what and! — Zhuge Liang
Text code
Proto
syntax = "proto3";
package helloworld;
message HelloRequest {
string name = 1;
}
message RepeatHelloRequest {
string name = 1;
int32 count = 2;
}
message HelloReply {
string message = 1;
}
message ListRequest {
repeated int32 id = 1;
string nil = 2;
}
message ListReply {
repeated string id = 1;
string Data = 2;
}
message TsMap {
string feature = 1;
string version = 2;
string series = 3;
}
message MapI {
TsMap test_map1 = 1;
TsMap test_map2 = 2;
TsMap validate_map = 3;
}
message MapRequest {
MapI map = 1;
repeated string do_safety_cars = 2;
}
message MapReply {
MapI map = 1;
}
service Greeter {
// unary call
rpc SayHello(HelloRequest) returns (HelloReply);
rpc ListTest(ListRequest) returns (ListReply);
rpc MapTest(MapRequest) returns (MapReply);
}
service Stream {
// server streaming call
rpc SayRepeatHello(RepeatHelloRequest) returns (stream HelloReply);
}
Copy the code
TS
import * as grpcWeb from "grpc-web";
interface HelloRequest {
name: string;
}
interface RepeatHelloRequest {
name: string;
count: number;
}
interface HelloReply {
message: string;
}
interface ListRequest {
idList: Array<number>;
nil: string;
}
interface ListReply {
idList: Array<string>;
data: string;
}
interface TsMap {
feature: string;
version: string;
series: string;
}
interfaceMapI { testMap1? : TsMap; testMap2? : TsMap; validateMap? : TsMap; }interfaceMapRequest { map? : MapI; doSafetyCarsList:Array<string>;
}
interfaceMapReply { map? : MapI; }export class GreeterPromiseClient {
constructor(
hostname: string, credentials? :null | { [index: string] :string}, options? :null | { [index: string] :any }
); sayHello( request: HelloRequest, metadata? : grpcWeb.Metadata ):Promise<HelloReply>; listTest( request: ListRequest, metadata? : grpcWeb.Metadata ):Promise<ListReply>; mapTest(request: MapRequest, metadata? : grpcWeb.Metadata):Promise<MapReply>;
}
export class StreamPromiseClient {
constructor(
hostname: string, credentials? :null | { [index: string] :string}, options? :null | { [index: string] :any }
); sayRepeatHello( request: RepeatHelloRequest, metadata? : grpcWeb.Metadata ): grpcWeb.ClientReadableStream<HelloReply>; }Copy the code