[gRPC] Part 2 A comprehensive understanding of gRPC principles, characteristics, and interaction processes

Table of contents

1. What is gRPC 

 2. Features of gRPC

3. gRPC interaction process

4、Protocol Buffers

5. Design based on HTTP 2.0 standard

6. Language support


1. What is gRPC 

In gRPC, a client application can directly call methods on a server application on a different computer as if it were a local object, making it easier for you to create distributed applications and services. Like many RPC systems, gRPC is based on the idea of ​​defining a service, specifying that it can be called remotely with its parameters and return types.

On the server side, the server implements this interface and runs a gRPC server to handle client calls. On the client side, the client has a stub (called a client language in some) that provides the same methods as the server.

gRPC clients and servers can run in a variety of environments - from Google's internal servers to your own desktop - and can be written in any gRPC-supported language. So, for example, you can easily create a gRPC server in Java and use a client in Go, Python or Ruby. Also the latest Google APIs will have gRPC versions of their interfaces, allowing you to easily build Google functionality into your applications. 

 gRPC is a high-performance, general-purpose open source RPC framework. It was designed by Google in 2015 for mobile application development and based on the HTTP/2 protocol standard. It is developed based on the ProtoBuf serialization protocol and supports many development languages.

Because it is an open-source framework, both sides of the communication can carry out secondary development, so the communication between the client and the server will focus more on the content of the business level, reducing the attention to the underlying communication implemented by the gRPC framework.

As shown in the figure below, the DATA part is the content at the business level, and all the information below is encapsulated by gRPC.

 2. Features of gRPC

Cross-language use, support C++, Java, Go, Python, Ruby , C#, Node.js, Android Java, Objective-C, PHP and other programming languages;

Define the service based on the IDL file, and generate the data structure, server interface, and client Stub of the specified language through the proto3 tool;

The communication protocol is based on the standard HTTP/2 design, and supports bidirectional streaming, message header compression, single TCP multiplexing, server push and other features, which make gRPC more power-saving and network traffic-saving on mobile devices;

Serialization supports PB (Protocol Buffer) and JSON. PB is a language-independent high-performance serialization framework based on HTTP/2 + PB, which guarantees the high performance of RPC calls;

Easy to install and easy to expand (using this framework can reach millions of RPCs per second).

3. gRPC interaction process

After enabling the gRPC function, the switch acts as the gRPC client, and the collection server acts as the gRPC server;

The switch will construct the corresponding data format (GPB/JSON) according to the subscribed events, write the proto file through Protocol Buffers, the switch will establish a gRPC channel with the server, and send a request message to the server through the gRPC protocol;

After the server receives the request message, the server will interpret the proto file through Protocol Buffers, restore the data structure with the first defined format, and perform business processing;

After the data is processed, the server needs to use Protocol Buffers to recompile the response data, and send a response message to the switch through the gRPC protocol;

After the switch receives the response message, it ends this gRPC interaction.

Simply put, gRPC is to establish a connection after the gRPC function is enabled on the client and server, and push the subscription data configured on the device to the server.

We can see that the whole process needs to use Protocol Buffers to define the structured data of the data to be processed in the proto file

4、Protocol Buffers

It can be understood  that ProtoBuf is a more flexible and efficient data format , similar to XML and JSON, and is very suitable for some data transmission scenarios that require high performance and response speed.

ProtoBuf has three main functions in the framework of gRPC: define data structure, define service interface, and improve transmission efficiency through serialization and deserialization.

Why does ProtoBuf improve transmission efficiency?

We know that when XML and JSON are used for data compilation, the data text format is easier to read, but when data exchange is performed, the device needs to consume a lot of CPU on I/O actions, which will naturally affect the entire transmission rate.

Protocol Buffers is not like the former, it will serialize the string before transmission, that is, binary data .

It can be seen that the content of the two is not much different, and the content is very intuitive, but the content encoded by Protocol Buffers is only provided for the operator to read. In fact, the transmission will not be in this text form, but the serialized binary Data, the number of bytes will be much less than that of JSON and XML, and the speed will be faster.

How does gPRC support cross-platform and multi-language?

It is also an advantage that Protocol Buffers comes with a compiler. The proto file mentioned above is compiled through the compiler. The proto file needs to be compiled to generate a similar library file. Only based on the library file can the real data application be developed.

What programming language is used to compile and generate this library file? Since the operation and maintenance personnel responsible for network equipment and server equipment in the current network are often not the same group of people, the operation and maintenance personnel may be accustomed to using different programming languages ​​for operation and maintenance development, so one of the advantages of Protocol Buffers can be brought into play - cross-language .

From the above introduction, we can conclude the advantages of Protocol Buffers over JSON and XML in terms of encoding:

  • Standard IDL and IDL compiler, which makes it very friendly to engineers;
  • Serialized data is very concise and compact. Compared with XML, the serialized data volume is about 1/3 to 1/10;
  • The parsing speed is very fast, about 20-100 times faster than the corresponding XML;

Protobuf applicable scenarios:

  • Protobuf has a wide user base, small space overhead and high resolution performance are its highlights, which is very suitable for RPC calls with high performance requirements within the company ;

  • Since Protobuf provides standard IDL and corresponding compiler, its IDL file is a very strong business constraint for all parties involved;

  • Protobuf has nothing to do with the transport layer. Using HTTP has good access properties across firewalls, so Protobuf is also suitable for scenarios with high performance requirements between companies;

  • Due to its high parsing performance and relatively small data volume after serialization, it is very suitable for the persistence scenario of application layer objects;

  • The main problem is that it supports relatively few languages . In addition, since there is no standard underlying transport layer protocol bound, it is relatively troublesome to debug the transport layer protocol between companies.

5. Design based on HTTP 2.0 standard

In addition to Protocol Buffers, as can be seen from the interaction diagram and layered framework, gRPC has another advantage—it is based on the HTTP 2.0 protocol.

Since gRPC is designed based on the HTTP 2.0 standard, it brings more powerful functions, such as multiplexing, binary frames, header compression, and push mechanisms.

These functions bring significant benefits to the device, such as saving bandwidth, reducing the number of TCP connections, saving CPU usage, etc. gRPC can be applied both on the client side and on the server side, thereby realizing and simplifying communication at both ends in a transparent manner system construction.

HTTP 1.X defines four ways to interact with the server, namely GET, POST, PUT, and DELETE, which are reserved in HTTP 2.0. Let's look at the new features of HTTP 2.0: bidirectional stream, multiplexing , binary Frame, header compression.

  • XML serialization (Xstream) is inferior both in performance and simplicity.

  • Compared with Protobuf, Thrift has certain disadvantages in terms of time and space overhead.

  • Protobuf and Avro perform very well in both areas.

6. Language support

The following are officially supported gRPC languages, platforms, and OS versions:

Guess you like

Origin blog.csdn.net/fanjufei123456/article/details/130024609