RPC lightweight framework for high concurrency processing

 

RPC (Remote Procedure Call) is a very common basic communication method in distributed systems. The core idea is to abstract the communication between different processes into function calls.

The basic process is:

1. The caller serializes the parameters into the stream and sends it to the server

2. The server deserializes the parameters from the stream and completes the actual processing

3. Serialize the result and return it to the caller.

The usual RPC is defined by the interface form, the interface defines the name of the service, the interface method defines the input parameters and return results of each request, and the implementation details such as serialization and network communication within the RPC are completed by the framework. Said to be completely transparent.

 

A lightweight framework structure diagram of the RPC structure:

 

 

 

According to the structure of the above figure, describe the detailed process of rpc:

1. Startup process

1. Rpc-server is responsible for starting the rpc service, and starts monitoring the specified port number when the service starts;

2. The Handler (Rpc-Handler) that will bind the service processing, as well as the Encoder class and the Decoder class at startup

3. After startup, call the register method of the RpcRegister class to register the discovered Rpc service with the service scheduling center

 

Second, the client synchronous call process

 

1. The user creates an Rpc service client through the Proxy class;

2. When using Hu to call the Rpc service, the Proxy calls the send method of the Rpc-Client (calls the Encoder class in the send method for serialization), and sends the service request to the Rpc-Server;

3. After receiving the request, the service of the Rpc listening port transfers the service to Rpc-Handler for processing;

4. After the Rpc-Handler receives the notification, it first calls the Decoder class to deserialize, and then starts a new thread to process the client request. After the request processing is completed, the Handler directly responds to the client;

5. After the client receives the response, it calls the Decoder class for deserialization. If there is an exception, it will be thrown, otherwise the return value of the server will be returned.

 

Third, the client asynchronous call process

1. The user creates an Rpc service client through the Proxy class;

2. When using Hu to call the Rpc service, the Proxy calls the call method of the Rpc-Client (calls the Encoder class in the send method for serialization), and sends the service request to the Rpc-Server. After the request is sent, the client continues to execute the rest Business logic;

3. After receiving the request, the service of the Rpc listening port transfers the service to Rpc-Handler for processing;

4. After the Rpc-Handler receives the notification, it first calls the Decoder class to deserialize, and then starts a new thread to process the client request. After the request processing is completed, the Handler directly responds to the client;

5. If the user registers the callback function, the client will call the Decoder class to deserialize after receiving the response, and then execute the callback logic; if the callback function is not registered, the client will end after sending the request.

 

See attachment for source code

 

Note: Thank you for the source code contributed by netizens. Most of the source code is used by others, and I only optimized it on this basis.

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327033779&siteId=291194637