[Microservices] Why use RPC instead of HTTP for calls between services?

 

1. What is RPC

RPC (Remote Procedure Call)—Remote Procedure Call, which is a protocol for requesting services from remote computer programs through the network without needing to understand the underlying network technology. For example, if two different services A and B are deployed on two different machines, what should service A do if it wants to call a method in service B? It is certainly possible to use HTTP requests, but it may be slower and some optimizations are not good. The emergence of RPC is to solve this problem.

2. Principle of RPC

  1. The service consumer (client) calls the service in a local call mode;
  2. The client stub is responsible for assembling methods and parameters into a message body that can be transmitted over the network after receiving the call;
  3. The client stub finds the service address and sends the message to the server;
  4. The server stub decodes the message after receiving it;
  5. The server stub calls the local service according to the decoding result;
  6. The local service is executed and the result is returned to the server stub;
  7. The server stub packages the returned result into a message and sends it to the consumer;
  8. The client stub receives the message and decodes it;
  9. The service consumer gets the final result.

Here is another online sequence diagram:

13465705-2ed5b4178fe31833.png

3. What problem does RPC solve

From the above introduction to RPC, in summary, RPC mainly solves: making calls between different services in a distributed or microservice system as simple as local calls.

Common RPC framework

  • RMI (built in JDK):  The RPC built in JDK has many limitations and is not recommended.
  • Dubbo:  Dubbo is a high-performance and excellent service framework open sourced by Alibaba. It enables applications to implement service output and input functions through high-performance RPC, and can be seamlessly integrated with the Spring framework. Dubbo has become an official component in Spring Cloud Alibaba.
  • gRPC  : gRPC is a modern open source high-performance RPC framework that can run in any environment. It can effectively connect services within and across data centers through pluggable support to achieve load balancing, tracking, health checks, and authentication. It also applies to the last mile of distributed computing to connect devices, mobile applications and browsers to back-end services.
  • Hessian:  Hessian is a lightweight remoteonhttp tool that provides RMI functions in a simple way. Compared to WebService, Hessian is simpler and faster. The binary RPC protocol is adopted, because it is a binary protocol, so it is very suitable for sending binary data.
  • Thrift:  Apache Thrift is Facebook's open source cross-language RPC communication framework. It has been donated to the Apache Foundation for management. Because of its cross-language features and excellent performance, it has been used in many Internet companies. Competent companies will even develop research based on thrift. A set of distributed service framework, adding functions such as service registration and service discovery.

3.1 With HTTP, why use RPC for service calls?

  • RPC is just a design

RPC is just a concept and a design to solve the  problem of calling between different services . It generally contains two  transmission protocols  and  serialization protocols  . The transmission protocol that implements RPC can be directly built on top of TCP, or it can be built on top of HTTP protocol. Most RPC frameworks use TCP connections (gRPC uses HTTP2).

  • HTTP 和 TCP

Maybe now many friends who are not familiar with computer networks have been confused. If you want to really understand, you need to briefly review the basic knowledge of computer networks:

We usually talk about the five-layer protocol architecture of computer networks: application layer, transport layer, network layer, data link layer, and physical layer.

  • The task of the application-layer is to complete specific network applications through the interaction between application processes. HTTP is an application layer protocol, it will transfer data (HTML files, image files, query results, etc.) based on the TCP/IP communication protocol. The HTTP protocol works on a client-server architecture. As an HTTP client, the browser sends all requests to the HTTP server, namely the WEB server, through the URL. The web server sends response information to the client according to the received request. The HTTP protocol is built on top of the TCP protocol.
  • The main task of the transport layer is to provide general data transmission services for the communication between the two host processes . TCP is a transport layer protocol, mainly to solve how data is transmitted in the network. Compared with UDP, TCP  provides connection-oriented and reliable data transmission services.

The main key lies in the difference between the TCP protocol used by HTTP and our custom TCP protocol in terms of messages.

  • The TCP packet of the http1.1 protocol contains too much information that may be useless during transmission:
HTTP/1.0 200 OK 
Content-Type: text/plain
Content-Length: 137582
Expires: Thu, 05 Dec 1997 16:00:00 GMT
Last-Modified: Wed, 5 August 1996 15:55:28 GMT
Server: Apache 0.84
 
<html>
  <body>Hello World</body>
</html>
 

Using a custom TCP protocol for transmission will avoid the above problem and greatly reduce the overhead of transmitting data.  This is the real reason why custom TCP protocol RPC is usually used to make service calls. In addition, the mature RPC framework also provides functions such as "service automatic registration and discovery", "intelligent load balancing", "visual service management and operation and maintenance", "runtime traffic scheduling", etc., which can also be regarded as Choose RPC for service registration and discover one of the reasons!

3.2 A common misconception

Many articles will also mention that compared to the custom TCP message protocol, the increased overhead of the HTTP protocol is the establishment and disconnection of the connection, but this view has been denied. The following is an interception from one of the answers:

First of all, we must deny that compared to the custom TCP message protocol, the increased overhead of the HTTP protocol lies in the establishment and disconnection of connections. The HTTP protocol supports connection pool reuse, that is, a certain number of connections are established without disconnection, and connections are not frequently created and destroyed. What I want to say is that HTTP can also use Protobuf, a binary encoding protocol, to encode content, so the biggest difference between the two is in the transmission protocol.

Digression

In addition, it should be noted that Spring Cloud Netflix does not use the RPC framework to make calls between different services, but uses the HTTP protocol for calls. Although the speed is not as fast as RPC, the use of the HTTP protocol will also bring Many other benefits

Guess you like

Origin blog.csdn.net/qq_41893274/article/details/113991782