What is the RPC? What is the principle of RPC?

What is RPC?

Remote Procedure Call (RPC) is a protocol that requests services from Remote computer programs over the network without understanding the underlying network technology. For example, two different services A and B are deployed on two different machines. What if service A wants to invoke A method in service B? Using HTTP requests is certainly possible, but it may be slower and some optimizations are not well done. RPC was invented to solve this problem.

What is the principle of RPC?

I here this is a brief mention, the details can be viewed in the following article:

www.importnew.com/22003.html

  1. A service consumer (client) invocation invokes a service as a local invocation;
  2. After receiving the call, the Client stub is responsible for assembling methods and parameters into a message body that can be transmitted over the network.
  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 invokes the local service based on the decoding result.
  6. The local service executes and returns the result 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 end result.

Here’s another time sequence from the web:

What problem does RPC solve?

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

Summary of common RPC frameworks?

  • RMI (JDK built-in) : JDK built-in RPC, which has many limitations and is not recommended.

  • Dubbo: Dubbo is an open source high-performance service framework from Alibaba, which enables applications to realize the output and input functions of services through high-performance RPC, and can be seamlessly integrated with the Spring framework. Dubbo is now 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 with pluggable support for load balancing, tracking, health checking, and authentication. It is also suitable for 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 in a simple way. Compared with WebService, Hessian is simpler and faster. The binary RPC protocol is used, and because it is a binary protocol, it is suitable for sending binary data.

  • Thrift: Apache Thrift is Facebook’s open source cross-language RPC communication framework, which 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 a set of distributed service framework based on Thrift. Add features such as service registration, service discovery, and so on.

If you have HTTP, why use RPC for service calls?

RPC is just a design

RPC is just a concept and a design designed to solve the invocation problem between different services. It generally includes transport protocol and serialization protocol.

However, HTTP is a protocol, and RPC framework can use HTTP as the transport protocol or DIRECTLY use TCP as the transport protocol. Different protocols are generally used to adapt to different scenarios.

HTTP and TCP

May now a lot of friends who are not familiar with computer networks have been confused, to really understand, but also need to review the basic knowledge of computer networks:

We usually talk about the computer network five layer protocol architecture refers to: application layer, transmission layer, network layer, data link layer, physical layer.

The application-layer is responsible for completing specific network applications through interaction between application processes. **HTTP is an application layer protocol, which is based on TCP/IP communication protocol to transfer data (HTML files, image files, query results, etc.). The HTTP protocol works on a client-server architecture. As the 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 based on the received request. HTTP is built on top of TCP.

The main task of the Transport layer is to provide a common data transfer service for communication between two host processes. TCP is a transport layer protocol that deals with how data is transferred over the network. Compared with UDP,TCP provides connection-oriented and reliable data transmission services.

The RPC framework is more versatile

Mature RPC framework also provides “automatic service registration and discovery”, “intelligent load balancing”, “visual service governance and operation and maintenance”, “run-time traffic scheduling” and other functions, which can be one of the reasons for choosing RPC for service registration and discovery.

Related reading:

  • Introduction to HTTP protocol (Ruan Yifeng)

A common misconception

In many articles, it is also mentioned that HTTP protocol increases the overhead of connection establishment and disconnection compared to the custom TCP protocol. However, this opinion has been denied.

The first is to deny the fact that HTTP is more expensive in connection establishment and disconnection than custom TCP. The HTTP protocol supports connection pool multiplexing, which means that a certain number of connections are continuously opened and connections are not frequently created or destroyed. It is important to note that HTTP can also encode content using Protobuf, a binary encoding protocol, so the biggest difference between the two is the transport protocol.

Snailclimb Link: Why do calls between services use RPC instead of HTTP directly? Source: making