What is RPC? What is the principle of RPC?
What is RPC?
RPC (remote procedure call) – remote procedure call is a protocol that requests services from remote computer programs through the network without understanding the underlying network technology. For example, 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? Of course, HTTP request is OK, but it may be slow and some optimizations are not good. RPC is to solve this problem.
What is the principle of RPC?
Here is a brief introduction. Please refer to the following article for details:
- The service consumer (client) calls the service in the way of local call;
- After receiving the call, the client stub is responsible for assembling the methods, parameters, etc. into a message body capable of network transmission;
- The client stub finds the service address and sends the message to the server;
- The server stub decodes the message after receiving it;
- The server stub calls the local service according to the decoding result;
- The local service executes and returns the result to the server stub;
- The server stub packages the returned result into a message and sends it to the consumer;
- The client stub receives the message and decodes it;
- Service consumers get the final result.
Here’s another online sequence diagram:
What problems does RPC solve?
From the above introduction to RPC, RPC mainly solves the following problemsMake the call between different services in distributed or micro service system as simple as local call.
Summary of common RPC frameworks?
- RMI (built in JDK) JDK’s own RPC has many limitations and is not recommended.
- Dubbo: Dubbo is a high-performance excellent service framework of Alibaba open source, which enables applications to realize the output and input functions of services through high-performance RPC, and can be seamlessly integrated with spring framework. At present, Dubbo has become the official component of 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 checking 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 remoting on HTTP tool that provides RMI functions in a simple way. Compared with WebService, Hessian is simpler and faster. It uses binary RPC Protocol, because it uses binary protocol, so it is very suitable for sending binary data.
- Thrift： Apache thrift is an open-source cross language RPC communication framework of Facebook, which has been donated to Apache foundation for management. Due to its cross language features and excellent performance, it has been applied in many Internet companies. Companies with ability will even develop a distributed service framework based on thrift, adding functions such as service registration and service discovery.
With HTTP, why use RPC to call services?
RPC is just a design
RPC is just a concept, a design, just to solve the problem Call between different servicesIt usually contains transport protocol and Serialization protocol these two items.
However, HTTP is a protocol, RPC framework can use HTTP protocol as the transport protocol or directly use TCP as the transport protocol, and different protocols are generally used to adapt to different scenarios.
HTTP and TCP
Maybe now many friends who are not familiar with computer network have been confused. In order to really understand, we need to review the basic knowledge of computer network
We usually talk about the architecture of five layer protocol of computer network, which refers to application layer, transport layer, network layer, data link layer and physical layer.
The task of application layer is to complete specific network applications through the interaction between application processes.HTTP belongs to the application layer protocol, it will transfer data (HTML files, picture files, query results, etc.) based on TCP / IP communication protocol. HTTP protocol works on the client server architecture. As an HTTP client, the browser sends all the requests to the HTTP server (web server) through URL. According to the received request, the web server sends the response information to the client. HTTP protocol is based on TCP protocol.
The main task of transport layer is to provide general data transmission service for the communication between two host processes. TCP is a transport layer protocol, mainly to solve how to transmit data in the network. Compared to UDP,TCP What is provided isConnection orientedYes,reliableData transmission service.
More complete RPC framework functions
The mature RPC framework also provides “automatic service registration and discovery”, “intelligent load balancing”, “visual service governance and operation and maintenance”, “runtime traffic scheduling” and other functions, which can be regarded as one of the reasons for choosing RPC for service registration and discovery!
- Introduction to HTTP protocol （ (Ruan Yifeng)
A common misconception
Many articles will also mention that compared with the custom TCP packet protocol, the increased overhead of HTTP protocol lies in the establishment and disconnection of the connection, but this view has been deniedSince there are HTTP requests, why use RPC calls?
First of all, we should deny that compared with the custom TCP packet protocol, the increased overhead of HTTP protocol lies in the establishment and disconnection of the connection. HTTP protocol supports the reuse of connection pool, that is to say, a certain number of connections can be established without frequent creation and destruction. Second, HTTP can also use protobuf, a binary encoding protocol, to encode content. Therefore, the biggest difference between the two is in the transport protocol.