rpc learning

Source address: https://github.com/feijian8/rpc

 

1、rpc-registry

a. Provide service registration service interface

ServiceRegistry provides a registration service for services in zookeeper. By creating a ZkClient object, registering the required service directory and creating a temporary service attribute ip+port, the data structure is as follows:

/**

* Example of data in zk:

* [zk: localhost:2181(CONNECTED) 20] ls /registry/com.xxx.rpc.sample.api.HelloService

* [address-0000000001]

* ��t127.0.0.1:8001

*/

 

b. Provide service discovery service interface

ServiceDiscovery provides the service discovery service in zookeeper. Through the ZkClient object, the service address corresponding to the service object package name is found. If there is only one microservice, it is ok to directly return the ip and port corresponding to the service. Otherwise, return several The ip and port of a random service in the microservice are ok

, the service address corresponding to the package name is as follows: [zk: localhost:2181(CONNECTED) 43] ls /registry/com.xxx.rpc.sample.api.HelloService-sample.hello2 [address-0000000002, address-0000000003, address- 0000000004].

 

2、rpc-registry-zookeeper

This project implements the registration and discovery of project microservices by rpc-dependent zookeeper services.

 

3、rpc-common

a. RpcRequest: encapsulates the body of the rpc request.

       RpcResponse: Encapsulates the response message body returned by rpc.

    b. RpcDecoder: Provides a decoder for rpc.

       RpcEncoder: An encoder that provides rpc.

    c. SerializationUtil: Implements object serialization tool based on Protostuff.

    

4、rpc-client

a. RpcClient: It mainly provides a tcp connection to create netty through ip and port, sends RpcRequest request data to the specified service address, and returns the required RpcResponse object to RpcProxy.

b. RpcProxy: Obtain the servicename through the dynamic proxy, use ZooKeeperServiceDiscovery to obtain the ip and port of the microservice stored in zk, and then use ip+port to build the required RpcClient, send the requested RpcRequest object to the microservice address, and return the required RpcResponse object,

Finally, the object passed in through the generic type returns the external interface method object to the client.

Among them, RpcProxy can directly return the message body through servicename, or return different message bodies through the interface of different versions of serviceName+serviceVersion, which is applicable to the continuous iterative development process of the version.

 

This completes the entire rpc calling process!

5、rpc-server

a. RpcServer: Provides the release of rpc services, scans classes containing RpcService annotations and initializes them through spring's annotations, initializes rpc-annotated services to zk, and registers rpc services by calling rpc-registry services.

b. When creating and initializing the Netty server Bootstrap object, add decoding and encoding processing to the request and response objects, and set up the processing business handler to obtain the method and parameter of the service through reflection. After execution, encapsulate it into the response and return it to the client of the rpc end.

c, rpcserver processing business is asynchronous processing,

6、rpc-sample-api

A simple bean class that defines an interface and an auxiliary model class that provides interface declarations to clients.

7、rpc-sample-client

a. The rpc client invokes the sample code, simulates and implements the rpc client request process, obtains the RpcClient instance through RpcProxy, calls send to send a message to the server through the nio long connection channel of netty, and returns the result RpcResponse to the client for display.

b, 2, 3, and 4 demonstrate different parameter passing and multithreading, respectively.

8、rpc-sample-server

a. Demonstrates that RpcBootstrap starts the rpc server, and then the server loads the rpc registry to register the annotations containing @RpcService into zk, exposing the service interface for client calls.

 

 

 

 

 

Guess you like

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