RPC framework GPRC Analysis

RPC transmission frame data are usually two: binary transmission, and text-based transmission.
The advantages of binary transmission are: good transmission performance, because write-protocol file, so more rigorous.
The disadvantages are: a binary language is difficult to cross,

Text transmission is the class advantage: you can cross-language, and because no written agreement document, the use of more flexible.
Disadvantages are: transmission performance to be worse slightly.

GRPC is characterized not only uses binary transmission, to ensure that the transmission performance, but also to meet the cross-language, to ensure flexibility.

Serialization

GPRC binary serialization protocol is Protocol Buffers.
Protocol Bufers is a highly efficient compression serialization protocol, there are many design sophisticated serialization methods.
For a 32-bit int type, generally require four stores Byte. In Protocol Bufers, the use of variable length is in the form of an integer. For the 8 highest bits per Byte has a special meaning.

If this bit is 1, this figure represents poured subsequent Byte also belong to this number; if the bit is 0, then this number ends. Other Bit 7 is used to represent the digital content. Thus, a number less than 128 may be represented by a Byte; number greater than 128, such as 130, will be represented by two bytes.

For each field, using a TLV (Tag, Length, Value) storage approach.
Where Tag = (feld_num << 3) | wire_type. feld_num proto file that is, for each field specifies the unique number, the type of data wire_type for later identification.

network transmission

If it is between Java technology stack, the client and server GRPC by Netty Channel as the data channel, each request is encapsulated in HTTP 2.0 Stream. Netty is an efficient network transmission frame based on the asynchronous IO

HTTP 2.0 protocol to a TCP connection, cut into a plurality of streams, each stream
has its own ID, and the stream is prioritized. Client flow can be sent to the server, the server can also be sent to the client. It is only a virtual channel.
HTTP 2.0 will all transmit information divided into smaller messages and frames, and are binary coded format.
By these two mechanisms, HTTP 2.0 client requests may be assigned to a plurality of different streams, then the contents of the request framing removed, for binary transmission. These frames may play scattered transmitted sequence, and then again for each frame according to the stream identifier in the header
assembly, and may be based on the priority, which determines the data flow priority.

Because based on HTTP 2.0, different GRPC and other RPC, we can define four service methods.
The first, and most common way is a one-way RPC, that is, the client sends a request to the server, gets a response from the server, just like a normal function call.

rpc SayHello(HelloReques) returns (HelloResponse){}

The second way is streaming server RPC, that is returned from the server is not a result, but a number. The client sends a request to the server, can obtain a series of data flow to read the message. The client has been read from the data stream returned
to take, until there is no more news so far.

rpc LotsOfReplies(HelloReques) returns (sream HelloResponse){}

The third embodiment is the RPC client flow, i.e. the client's request is not one, but a group. A client data stream is written and provided with a series of messages sent to the server. Once the client has completed message is written, will wait for the server to read that
some message and returns a response.

rpc LotsOfGreetings(sream HelloReques) returns (HelloResponse) {}

The fourth embodiment is a two-way flow RPC, i.e., both sides can read and write data, respectively, through a series of messages transmitted stream. The two data streams is independent of the operation, so the client and server can read and write its desired any order, the server
may wait for all of the clients before the write reply message, or it can first read to write a message a message, read or otherwise combined. Message in order for each data stream will be maintained.

rpc BidiHello(sream HelloReques) returns (sream HelloResponse){}

If it based on the much richer interaction between HTTP 2.0, the client and the server you want to, not only can be called remotely in one direction, can also be realized when the server status changes, take the initiative to inform the client.
Thus, the transmission problem has been resolved.

Service Management

GRPC itself does not provide a mechanism for service discovery, need the help of other components, we found the server to be accessed, fault-tolerant and load balancing across multiple server.
In fact, the load balancer itself is relatively simple, LVS, HAProxy, Nginx can do, the key question is how to find the server, and the server of the change, modify the configuration dynamically load balancer.

There is a good support for GRPC load balancer Envoy. In fact, Envoy is not just a load balancer, it is a high-performance C ++ to write Proxy repeater can be configured very flexible forwarding rules.
These rules can be static, in a configuration file is loaded at boot time. To reload, generally need to be restarted, but the Envoy supports hot loading and hot restart, which alleviates this problem to some extent.
Of course, the best way is to set rules to dynamic, unified in place to maintain. This unified place is known in the eyes of Envoy Service Discovery (Discovery Service), over a period of time to get here at the configuration, modify the
forwarding policy.
Whether it is static or dynamic, in the configuration which tend to configure four things.
The first is the listener. Envoy Since it is a Proxy, designed to do forward, you have to listen on a port, an access request before it can be forwarded according to the policy, the listener port called listener.
The second is the endpoint, the IP address and port of destination. This is where Proxy will forward the request to the final.
The third is the cluster. A cluster having a plurality of endpoint identical behavior, i.e., three if the server is running, there will be three IP and port, but deployment is identical three services, they form a
th cluster, the cluster from endpoint is called load balancing can be polled.
The fourth is the route. Sometimes a plurality of cluster have similar functions, but different version numbers, route rule by selecting routes the request to a version number, i.e. one cluster.
If it is static, IP address of the server will get the back end, and then placed in the configuration file inside it.

Reference material

Time Geeks "Something about network protocol"

Guess you like

Origin www.cnblogs.com/hushida66/p/11934681.html