What does RPC look like after peeling off layers of coat?

RPC, the full name of which is remote procedure call. Generally speaking, it refers to calling functions on the remote server locally. To realize remote call, at least the following conditions must be met:

1. Network communication

2. Serialization and deserialization

3. Reflection

Remote communication is the premise of remote call. Only serialized data can be transmitted on the network. After being transmitted to the server, it needs to be de sequenced into objects, then call the service specified by the client on the server through the reflection mechanism, and then return the result to the client, represented by a graph:

Every time the server receives a request from the client, it starts a thread processing, calls the corresponding methods of the specific service object, and returns the returned results to the client.

Based on this principle, an example is written below to realize the RPC function:

The project structure diagram is as follows: client, server, service, request and response.

First, abstract the request class and response class:

Request class: attributes include the requested class name, method name, input parameter type and input data.

Response class: the property has a return object

Server class and service processing class:

The server class is responsible for receiving the client's request and assigning the request to the service processing class for processing.

Server class:

Service processing class:

Service class:

Finally, the client class:

Run the server and then the client to get the results:

Conclusion: RPC is actually based on network communication and uses reflection technology to achieve the purpose of remote function call. The request and response classes must be serializable for transmission over the network, and both client and server must have these classes. This example is just the simplest way to illustrate what RPC is.

At present, there are many RPC frameworks, such as thrift, Hessian, jsonrpc, Dubbo, rpcx, grpc, etc. they are re encapsulated and optimized on this basis to make it easier for users to use and call remote functions, just as convenient as calling local functions.

The implementation frameworks of network communication, serialization and reverse order instantiation and reflection mentioned above are different.

Some choose TCP and some choose HTTP for network communication. TCP is the transport layer protocol, HTTP is the application layer protocol, and the transport layer is lower than the application layer. In terms of data transmission, the lower the layer, the faster. Therefore, in general, TCP must be faster than http.

In terms of serialization, Java provides the default serialization method, but in the case of high concurrency, this method will bring some performance bottlenecks, so there are a series of excellent serialization frameworks on the market, such as protobuf, kryo, Hessian, Jackson, etc., which can replace Java's default serialization to provide more efficient performance.

Reflection technologies include Java's own reflection technology, objenesis and cglib.

In practical applications, a service is not directly a specific class, but an interface, also known as a protocol, that is, a consensus between the client and the server. When the client requests, the interface name is transmitted. The server finds the implementation class of the interface through the interface name, then creates an object and executes the method to return the result. Or the client finds the service implementation class name through the service registry and transmits it to the server, which involves service registration and discovery. The implementation methods of different frameworks are also different, Even some frameworks have traffic monitoring and control, service failure conversion, load balancing and so on.

This article is just holding bricks and attracting jade. I hope you can go to a higher level after understanding the basic principles of RPC!

reference resources:

Lightweight distributed RPC framework

Ali Dubbo crazy update, what's the matter with spring cloud

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>