grpc does not only support go language. grpc is a communication protocol based on HTTP/2 and supports multi-language RPC framework; currently provides C, Java and Go language versions, namely grpc, grpc-java, grpc-go; the C version supports C, C, Node.js, Python, Ruby, Objective-C, PHP and C# supported.
The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.
What is grpc
gRPC is a communication protocol based on HTTP/2, supports multi-language RPC framework, and is designed for mobile and HTTP/2. gRPC is designed based on the HTTP/2 standard and brings features such as bidirectional streaming, flow control, header compression, and multiplexing requests on a single TCP connection. These features make it perform better on mobile devices and save power and space.
RPC: The abbreviation of Remote Procedure Call, translated as remote procedure call (can also be translated as remote method call or remote call), it is a computer communication protocol. This protocol can make calling remote services as simple as calling local services, without having to worry about cross-network, cross-platform, cross-language and other issues.
gRPC message serialization method usually uses Protobuf, which is a binary format, small in size, fast in network transmission, takes up less bandwidth and traffic, and has higher calling performance.
Features of gRPC
-
IDL
gRPC Use ProtoBuf to define services. ProtoBuf is a data serialization protocol developed by Google (similar to XML, JSON). ProtoBuf can serialize data and is widely used in data storage, communication protocols, etc.
-
Multi-language support
gRPC supports multiple languages and can automatically generate client and server function libraries based on the language. Currently, the C version grpc, the Java version grpc-java and the Go version grpc-go are provided. Among them, grpc supports C, C, Node.js, Python, Ruby, Objective-C, PHP and C# and other languages. grpc-java has Support Android development.
-
HTTP2
gRPC is designed based on the HTTP2 standard and brings more powerful features, such as bidirectional streaming, header compression, multiplexing requests, etc. These features bring significant benefits such as bandwidth savings, reduced TCP link times, CPU usage savings, and extended battery life. At the same time, gRPC can also improve the performance of cloud services and web applications. gRPC can be applied on both the client and server sides, thereby achieving client-server communication in a transparent manner and simplifying the construction of communication systems.
Why we should use grpc
Good ecology: backed by Google. For example, nginx also provides support for grpc. Reference link
Cross-language: Cross-language, and automatically generates sdk
High performance: For example, the performance of protobuf is higher than json, and the performance of http2.0 is higher than http1.1
Strong typing: the compiler solves a large part of the problem for you
Streaming processing (based on http2.0): supports client streaming, server streaming, and two-way streaming
How are the advantages of grpc realized
1. High performance of grpc: Why is protobuf better than json?
1) What is protobuf?
- Protobuf is a binary format developed by Google for serializing data between different services. It is an IDL (interface description language) language
2) How much faster is it than json?
3) Why is protobuf faster than json?
- Comparing the json data and protobuf data formats, we can know that
-
Small size - no delimiter required: TLV storage method does not require delimiters (comma, Double quotes, etc.) can separate fields, reducing the use of delimiters
-
Small size-empty fields are omitted: If the field has no field value set, then the data when the field is serialized It does not exist at all, that is, no encoding is required, and json will pass the key and the value of the null value
-
Small size-tag binary representation: It uses the numeric value of the field and then converts it into The binary representation is more space-saving than the string representation of json key
-
Encoding and decoding is fast: The tag stores the field type, and you can directly know the length of the value, or when When value is a string, length is used to store the length. You can directly take n bytes from length to get the value of value. If the length of value is not known, we must do string matching
-
For a detailed understanding of protobuf encoding, you can go to : varint and zigzag encoding methods
2. High performance of grpc: why http2.0 has higher performance than http1.1 ?
1) Multiplexing
-
Comparison between http2.0 and http 1.* and http1.1pipling
Schematic diagram
- #http/1.*: One request, one response, a connection is established and closed after completion, each request must establish a connection
- http1.1 pipeling: Pipeling solution is to queue several requests Serialized single-thread processing, subsequent requests wait for the return of the previous request before they can get the opportunity to execute. Once a request times out, subsequent requests can only be blocked, and there is no way. This is what people often call head-of-line blocking
- http2: Multiple requests can be executed in parallel on one connection at the same time. A certain request task is time-consuming and will not affect the normal execution of other connections.
- What are the other advantages of grpc multiplexing
reduces tcp The connection reduces the pressure on the server and client on memory, CPU, etc.- Reduces the TCP connection and ensures that TCP re-establishment is not triggered frequently, so that there will be no frequent slow starts
- Reduces tcp connections and improves network congestion
-
- Why http/1.1 cannot achieve multiplexing but http2.0 can?
Because http/1.1 transmission uses text, while http2.0 uses binary frame transmission-
2) Header compression
- Fixed field compression: http can gzip compress the body through http, which can save bandwidth, but there are also many fields in the header of the message that are not compressed. Compression, such as cookies, user agent accept, these must be compressed
- Avoid duplication: Many field values are repeated in a large number of request and response messages, so it is necessary to avoid duplication Performance
- Encoding improvement: The field is ascii encoded, which is inefficient. Changing to binary encoding can improve
The above is implemented through the - HPACK algorithm, The algorithm mainly consists of three parts
Static dictionary: convert commonly used header fields into a dictionary, such as {":method":"GET"} can be represented by a single number 2- Dynamic dictionary: If there are no header fields in the static dictionary, use the dynamic dictionary
- Huffman encoding: Compression encoding
-
3) Binary division Frame
On the binary framing layer, HTTP 2.0 will divide all transmitted information into smaller messages and frames, and encode them in binary format, among which HTTP1.x The header information will be encapsulated into the Headers frame, and our request body will be encapsulated into the Data frame. - After being framed in this way, these frames can be sent out of order, and then assembled according to the stream identifier in the header of each frame
-
- Compare http/1.1Because it is based on text Splitting each key:value with a newline character will have the following problems:
can only process one request or response at a time, because this kind of data that splits the message with a delimiter cannot stop parsing before it is completed. - It is impossible to predict how much memory is required to parse this kind of data, which will put a lot of pressure on the server
-
4) The server actively pushes resources
- Since the server is supported to actively push resources, some requests can be omitted. For example, if you need two files 1.html and 1.css, if it is http1.0, you need to request it twice and the server will return it twice. But http2.0 allows the client to request once, and then the server directly responds twice
For more programming-related knowledge, please visit: Introduction to Programming! !
The above is the detailed content of Does grpc only support go language?. For more information, please follow other related articles on the PHP Chinese website!