Alibaba Cloud Service Mesh (ASM) allows you to manage applications that use the gRPC protocol. For example, you can develop applications and add applications to containers and ASM instances. This topic describes the design principle of the gRPC practice that ASM provides.

Communication models of gRPC

Design principle

  • The practice involves the four communication models of gRPC.
  • The method names and parameter names that are used in the practice do not indicate any business features. This way, you can focus on the technology.

Communication models and implementation methods

Communication model Implementation method
Unary RPC talk
Server streaming RPC talkOneAnswerMore
Client streaming RPC talkMoreAnswerOne
Bidirectional streaming RPC talkBidirectional

Protocol Buffers definition

service LandingService {
  //Unary RPC
  rpc talk (TalkRequest) returns (TalkResponse) {
  }
  //Server streaming RPC
  rpc talkOneAnswerMore (TalkRequest) returns (stream TalkResponse) {
  }
  //Client streaming RPC with random & sleep
  rpc talkMoreAnswerOne (stream TalkRequest) returns (TalkResponse) {
  }
  //Bidirectional streaming RPC
  rpc talkBidirectional (stream TalkRequest) returns (stream TalkResponse) {
  }

Methods

  • The mainline logic of the practice is simple. For example, the data parameter in the following figure indicates a subscript of the hello array. When the gRPC server receives a request that contains the data parameter, the gRPC server returns the corresponding value in the hello array to the gRPC client.
  • The practice simplifies requests and responses. The practice uses only one method to encapsulate each request and response, no matter whether the request or response contains one or more messages.
    • All requests are strings. If a request contains multiple messages, separate the messages with commas (,).
    • All responses are arrays. If a response contains only one message, the returned array contains only one value.
  • The gRPC client and server communicate with each other by using the programming language. The traffic shaping configuration is displayed in the language that is specified by the lang variable.
Communication by using the programming language

Protocols

Design principle

  • The practice uses simple request parameters to facilitate debugging. At the same time, the request parameters contain sufficient information.
  • The response parameters in the practice support all the data types that are required for demonstration.

Request protocol

The request parameters in the practice include data and meta. All the request parameters are strings. The data parameter specifies the value of the subscript that you want to add to the hello array. The meta parameter specifies the programming language.

message TalkRequest {
  //language index
  string data = 1;
  //clientside language
  string meta = 2;
}

Response protocol

  • The response parameters in the practice contain only the status parameter and the TalkResult parameter. The value of the status parameter is an integer, which indicates a status code. The value of the TalkResult parameter is an array.
  • The array of the TalkResult parameter supports values of multiple data types, including the big integer, enumeration, and key-value pair types. The generic type of key-value pairs is string.
message TalkResponse {
  int32 status = 1;
  repeated TalkResult results = 2;
}

message TalkResult {
  //timestamp
  int64 id = 1;
  //enum
  ResultType type = 2;
  // result uuid
  // language index
  // data hello
  // meta serverside language (It's not good here, 
  //      but ok since I feel like to keep the response)
  map<string, string> kv = 3;
}

enum ResultType {
  OK = 0;
  FAIL = 1;
}

Functions

Function Description
Environment variable The practice provides the GRC_SERVER variable for the gRPC client. In local development and debugging, the value of this variable is localhost. You must specify domain names of the gRPC services of the pod that you want to access. When the pod of the gRPC client starts, the value of the GRC_SERVER variable changes to the domain name of an active gRPC service. This way, the gRPC client can call the gRPC service.
Random number For the client streaming remote procedure call (RPC) and bidrectional streaming RPC models, the gRPC client needs to call the random number function to generate a random integer value, which must be one of the subscripts of the hello array.
Timestamp TalkResult.id is a unique identifier of the int64 type. The value is a timestamp that is generated by the timestamp function.
UUID TalkResult.kv[id] is a unique identifier of the string type. The value is a UUID that is generated by the UUID function.
Sleep For the models excluding unary RPC, you can call the sleep function to set the interval between requests. This way, you can better observe the sequence of messages between the gRPC client and server.