Background of 0.

GRPC is Google’s open source lightweight RPC communication framework, which is often used in microservices architecture.

1. The introduction of gRPC

GRPC is Google’s open source lightweight RPC communication framework, whose communication protocol is based on binary data flow, which makes gRPC have excellent performance.

GRPC supports HTTP 2.0 protocol, uses binary frames for data transmission, and can also establish continuous two-way data flow for communication parties.

Basic concept

In gRPC, client applications can directly invoke methods on server applications on different machines as if it were a local object, making it easy to create distributed applications and services.

Breakdown introduction:

  • (1) Define a service: BASED on the idea of defining a service, gRPC specifies methods that can be called remotely with parameters and return types.
  • (2) Server-side service implementation: The server implements this interface and runs the gRPC server to receive client calls.
  • (3) Client calls via stub: The client has a stub that provides the same methods as the server and executes as if the server’s methods were called.

image.png

GRPC enables clients and servers to run and communicate with each other in different environments, such as geographically, physical environments. And support a variety of development languages for development.

2. GRPC uses protobuf as the communication protocol

The communication between the two microservices is based on HTTP 2.0 binary data frames, using the protobuf protocol built into gRPC, whose DSL syntax can clearly define the data structure of the communication between services

Corresponding to structs and functions in development languages, gRPC describes (defines) the structure of serialized data and service invocation methods through the Protobuf communication protocol.

The following are described respectively:

  • Defining data structures
  • Defining service methods

2.1 Defining data structures

In protobuf, the representation of a transmitted data structure entity object as message. Example:

message Person {
  string name = 1;
  int32 id = 2;
  bool has_ponycopter = 3;
}
Copy the code

It describes the data structure of the message in transit, and Protobuf provides methods for serialization and deserialization. Protobuf provides tools for compiling and generating code, using the Protoc tool to generate source code based on each development language. Refer to my other article on using Protocol Buffers in the Go language.

2.2 Defining service methods

Just like functions and methods in development languages, service definitions can define and describe data access methods, describing method names, calling parameters, and return parameters, for example:

// The greeter service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}
Copy the code

The service methods defined here can also be generated using Protobuf. It then acts as a calling method convention between the server and client.

3. Four types of service methods

GRPC allows you to define four classes of service methods:

3.1 Common RPC Calls

That is, the client sends a request to the server and gets a response from the server, just like a normal function call.

rpc SayHello(HelloRequest) returns (HelloResponse){
}
Copy the code

3.2 Streaming RPC on the server

That is, the client sends a request to the server to obtain a data stream to read a series of messages. The client reads from the returned data stream until there are no more messages.

rpc LotsOfReplies(HelloRequest) returns (stream HelloResponse){
}
Copy the code

3.3 Client Streaming RPC

That is, the client writes and sends a series of messages to the server using a provided data stream. Once the client has finished writing the messages, it waits for the server to read them and return a reply.

rpc LotsOfGreetings(stream HelloRequest) returns (HelloResponse) {
}
Copy the code

3.4 Two-way streaming RPC

That is, each side can send a series of messages through a read/write data stream.

rpc BidiHello(stream HelloRequest) returns (stream HelloResponse){
}
Copy the code

4. Other concepts

4.1 Timeout Period

GRPC allows clients to specify a deadline value before calling a remote method.

4.2 Terminating/Canceling an RPC Call

Either a client or a server can cancel an RPC call at any time

4.2 RPC Life Cycle

A simple RPC form: the client makes a single request and gets a single response.

  • (1) When a client invokes a method via a Stub, the server is notified of the metadata of the client, the name of the method, and the allowed response time (if available).
  • (2) Depending on the application, the server can either send back the original metadata directly before any response or wait for the client to request information.
  • (3) After the server obtains the request information from the client, it processes the message and assembs the response message content. If executed successfully, its response is returned to the client with a status code, optional status information, and optional trace information.
  • (4) If the state is OK, the client will get the answer, then the bundle client call.

Reference 5.

Quickstart www.grpc.io/docs/quicks…

Basic concepts www.grpc.io/docs/guides…

Guide to www.grpc.io/docs/guides…

www.grpc.io/docs/guides…

GRPC Official Documentation Chinese version doc.oschina.net/grpc?t=5796…

END