Table of Contents
What is grpc
How are the advantages of grpc realized
2. High performance of grpc: why http2.0 has higher performance than http1.1 ? " >2. High performance of grpc: why http2.0 has higher performance than http1.1 ?
Home Backend Development Golang Does grpc only support go language?

Does grpc only support go language?

Dec 16, 2022 pm 03:51 PM
golang go language grpc

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.

Does grpc only support go language?

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.

Does grpc only support go language?

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?

  • The binary data flow and json data flow of protobuf are as shown below

Does grpc only support go language?

  • 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

Does grpc only support go language?

  • #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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to safely read and write files using Golang? How to safely read and write files using Golang? Jun 06, 2024 pm 05:14 PM

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 pool for Golang database connection? How to configure connection pool for Golang database connection? Jun 06, 2024 am 11:21 AM

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.

What are the best practices for error handling in Golang framework? What are the best practices for error handling in Golang framework? Jun 05, 2024 pm 10:39 PM

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

What are the advantages of golang framework? What are the advantages of golang framework? Jun 06, 2024 am 10:26 AM

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

Detailed practical explanation of golang framework development: Questions and Answers Detailed practical explanation of golang framework development: Questions and Answers Jun 06, 2024 am 10:57 AM

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.

How to save JSON data to database in Golang? How to save JSON data to database in Golang? Jun 06, 2024 am 11:24 AM

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.

Golang framework vs. Go framework: Comparison of internal architecture and external features Golang framework vs. Go framework: Comparison of internal architecture and external features Jun 06, 2024 pm 12:37 PM

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 solve common security problems in golang framework? How to solve common security problems in golang framework? Jun 05, 2024 pm 10:38 PM

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

See all articles