Using grpc for internal communication of microservices
Grpc is a remote service invocation framework open-source by Google, which has the characteristics of multiplexing and bidirectional streaming communication.
Hello, this article will show you why we should use grpc instead of restful or JSON to develop the internal communication interface of microservices.
What is grpc?
Grpc is a high performance, open source, universal RPC framework. In short, it can help us establish transparent communication system between server and client. Google developed grpc and made it open source. Through it, a client-side consumer service can call a server-side method on another host just like calling a local method. In essence, grpc still follows the conventional remote procedure call (RPC) technology, but uses more modern technical solutions such as http2.0 and protocol buffer to ensure the interoperability and performance improvement of server and client to the greatest extent.
How to use grpc communication between services?
When the client makes a request to the server, the grpc class library of the client uses the protocol buffer and encapsulates the remote procedure call (RPC) and sends it to the server through http2. The server will unseal it and use the protocol buffer to call the corresponding program. The process of responding to the data is similar to the process of sending the request, except that one is from the client to the server, and the other is from the server to the client.
From a development point of view, the biggest advantage of using grpc on the server side and the client side is that your server-side code and client-side code do not need to worry that it will affect your parsing of JSON or other similar text format messages. Grpc receives binary format, but deserializes it as an object. Similarly, we can define the service interface through IDL, which is a very powerful feature to help us deal with the interoperability of multiple microservices.
Why is grpc efficient?
- It is based on http2 and supports both traditional request response model and bidirectional flow model.
- The data can be converted to the JSON protocol
- Two way flow model
- The network transfers binary data, which is more lightweight than text data such as JSON.
- Multilingual support
When to use grpc?
At first, almost all micro services communicate with each other through the JSON data interface. A service may call an empty service or multiple services, and the called service may also call other services. If any of these services run slowly, it will affect the running speed of the whole system, because the restful (JSON) API does not support http2’s multiplexing and bidirectional flow model. The traditional restful interface uses JSON, XML or other formats as data carriers, which makes the service run slowly, takes up a lot of memory, and does not compress the transmission process.
Grpc solves all these problems, but it is only used for the communication between system application microservices. The external service interface of the system still uses http-json interface. This ensures that there is no impact on the development technology stack of external users.
Compared with traditional rest APIs, Apis created with grpc can bring incredible performance improvements to your applications.
Welcome to my blog, there are many excellent collections
- This article reprints to indicate the source (must take the link, cannot only turn the text): letter brother blog.