Does grpc only support go language?
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?
- Six times faster, Reference link
3) Why is protobuf faster than json?
The binary data flow and json data flow of protobuf are as shown below
- 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 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?
- reduces tcp The connection reduces the pressure on the server and client on memory, CPU, etc.
- 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
- Dynamic dictionary: If there are no header fields in the static dictionary, use the dynamic dictionary
- Huffman encoding: Compression encoding
- Static dictionary: convert commonly used header fields into a dictionary, such as {":method":"GET"} can be represented by a single number 2
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:
- 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
- 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.
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Reading and writing files safely in Go is crucial. Guidelines include: Checking file permissions Closing files using defer Validating file paths Using context timeouts Following these guidelines ensures the security of your data and the robustness of your application.

How to configure connection pooling for Go database connections? Use the DB type in the database/sql package to create a database connection; set MaxOpenConns to control the maximum number of concurrent connections; set MaxIdleConns to set the maximum number of idle connections; set ConnMaxLifetime to control the maximum life cycle of the connection.

Best practices: Create custom errors using well-defined error types (errors package) Provide more details Log errors appropriately Propagate errors correctly and avoid hiding or suppressing Wrap errors as needed to add context

Advantages of the Golang Framework Golang is a high-performance, concurrent programming language that is particularly suitable for microservices and distributed systems. The Golang framework makes developing these applications easier by providing a set of ready-made components and tools. Here are some of the key advantages of the Golang framework: 1. High performance and concurrency: Golang itself is known for its high performance and concurrency. It uses goroutines, a lightweight threading mechanism that allows concurrent execution of code, thereby improving application throughput and responsiveness. 2. Modularity and reusability: Golang framework encourages modularity and reusable code. By breaking the application into independent modules, you can easily maintain and update the code

In Go framework development, common challenges and their solutions are: Error handling: Use the errors package for management, and use middleware to centrally handle errors. Authentication and authorization: Integrate third-party libraries and create custom middleware to check credentials. Concurrency processing: Use goroutines, mutexes, and channels to control resource access. Unit testing: Use gotest packages, mocks, and stubs for isolation, and code coverage tools to ensure sufficiency. Deployment and monitoring: Use Docker containers to package deployments, set up data backups, and track performance and errors with logging and monitoring tools.

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

The difference between the GoLang framework and the Go framework is reflected in the internal architecture and external features. The GoLang framework is based on the Go standard library and extends its functionality, while the Go framework consists of independent libraries to achieve specific purposes. The GoLang framework is more flexible and the Go framework is easier to use. The GoLang framework has a slight advantage in performance, and the Go framework is more scalable. Case: gin-gonic (Go framework) is used to build REST API, while Echo (GoLang framework) is used to build web applications.

How to address common security issues in the Go framework With the widespread adoption of the Go framework in web development, ensuring its security is crucial. The following is a practical guide to solving common security problems, with sample code: 1. SQL Injection Use prepared statements or parameterized queries to prevent SQL injection attacks. For example: constquery="SELECT*FROMusersWHEREusername=?"stmt,err:=db.Prepare(query)iferr!=nil{//Handleerror}err=stmt.QueryR
