On RPC and Http

What is RPC and what is the principle of RPC?

What is RPC?

RPC (Remote Procedure Call) is a protocol that requests services from remote computer programs through the network without understanding the underlying network technology. For example, if two different services A and B are deployed on two different machines, what if service A wants to use a method in service B? Of course, it is possible to use Http request, but it may be slow and some optimizations are not good. The emergence of RPC is to solve this problem.

What is the principle of RPC?

 

1. The service consumer (client) calls the service by calling it locally;

2. After receiving the call, the client stub is responsible for assembling methods, parameters, etc. into a message body capable of network transmission;

3. The client stub cannot find the service address and sends the message to the server;

4. The server stub decodes after receiving the message;

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.

The timing diagram is as follows:

 

 What problem does RPC solve?

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

Summary of common RPC frameworks

RMI (JDK comes with) : JDK comes with RPC, there are many limitations, it is not recommended.

Dubbo : Dubbo is a high-performance and excellent service framework open sourced by Alibaba, which enables applications to implement service output and input functions through high-performance RPC, and can be seamlessly integrated with the Spring framework. Currently, Dubbo has become a Spring Cloud Alibaba Official components.

gRPC : gRPC is a modern open-source high-performance RPC framework that can run in any environment. It can effectively connect services in and across data centers through plug-in support to achieve load balancing, tracking, operation check 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 remotingonhttp tool that provides RMI functionality using a simple method. Compared to WebService, Hessian is simpler and faster. The binary RPC protocol is used. Because the binary protocol is used, 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. Due to its cross-language features and excellent performance, it has been applied in many Internet companies. Capable companies will even develop based on thrift. A distributed service framework adds functions such as service registration and service discovery.

Since there is Http, why use RPC to make service calls?

RPC is just a design

RPC is just a concept, a design, in order to solve the calling problem between different services, it generally includes two transmission protocols and serialization protocols.

The transport protocol that implements RPC can be built directly on TCP or on the HTTP protocol. Most RPC frameworks use TCP links (gRPC uses HTTP2).

Http和TCP

Many friends who are not familiar with computer networks may have been confused. To really understand, you need to simply review the basics of computer networks:

The architecture of the five-layer protocol of the computer network we usually refer to: the application layer, the transport layer, the network layer, the data link layer, and the physical layer.

The task of the application-layer is to complete a specific network application through the interaction between application processes. Http belongs to the application layer protocol, he will transfer data (Html ​​files, pictures, query results, etc.) based on the TCP / IP communication protocol. The Http protocol works on the client-server framework. As the Http client, the browser sends all requests to the Http server, namely the Web server, through the URL. After receiving the request, the Web server sends a response message to the client. The Http protocol is built on the TCP protocol.

The main task of the transport layer is to provide general data transmission services to the communication between the two host processes. TCP is a transport layer protocol that mainly solves how data is transmitted in the network. Compared with UDP and TCP, it provides a link-oriented and reliable data transmission service.

The main key lies in the difference between the TCP protocol used by Http and our custom TCP protocol.

The TCP packet of the http1.1 protocol contains too much information that may be useless during transmission:

 

 Using custom TCP protocol for transmission will avoid the above problem and greatly reduce the overhead of transmitting data. This is the real reason why the 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 in discovery", "intelligent load balancing", "visual service management and operation and maintenance", "runtime traffic scheduling" and so on. These are also regarded as One reason for choosing RPC for service registration and discovery.

A common misconception

Many articles will also mention that the Http protocol compared to the custom TCP message protocol, the increased overhead is the establishment and disconnection of the link, but this view has been denied, the following intercept is taken from one of the answers:

 

 

Guess you like

Origin www.cnblogs.com/xiaomowang/p/12693007.html
RPC
RPC