Preface
RPC The frame is the knowledge point that the back-end siege lion can never get around , At present, some well-known in the industry are Dubbo
、Spring Cloud
etc. . Many people are stuck in the stage of only using , As a program ape , Have curiosity and study deeply , In order to effectively improve their competitiveness . Students on the next floor , Will go through the source code , See how the function is implemented , This is a good start . Look at the source code, it's easy to forget after a period of time , I think after reading the source code , It's better to develop one yourself , In this way, you will have a deeper understanding of the framework . In my submission ," Will use "、" Can read the source code "、" Will write it out " It's a completely different level .
This series " Of the wheel series RPC", Teach everyone how to build their own RPC frame .
The following is a simple version RPC Source code , welcome Star、Fork. Level co., LTD. , You have better ideas to put forward .
Github:https://github.com/chenchuxin/ccx-rpc
Gitee:https://gitee.com/imccx/ccx-rpc
RPC Structure of framework
One of the simplest RPC The framework is divided into three parts : Registry Center 、 Server side 、 client . The following is the simplest structure flow chart .
Part of the :
- Registry Center : Used to register and obtain services .
- Server side : Means the party providing the service , Also called service provider
Provider
- client : Refers to the party calling the service , Also called service consumer
Consumer
technological process :
- The server registers the service information in the registry , It usually contains the server address 、 Interface classes and methods
- The client obtains the information of the corresponding service from the registry
- According to the information of the service , Call the interface to the server through the network
RPC Frame design
Many details of the above process are not drawn , for example :
- In what form does the server register with the registry ?
- How does the client call the service like an interface ?
- What is the network protocol for invoking the service ?
A basic RPC frame , It needs to include the following parts :
- Registry Center : The registration center is responsible for the registration and search of service information . When the server starts , Scan all services , Then register your service address and service name in the registry . Before the client invokes the service , Find the address of the service through the registry , You can call the service through the address of the service . Common registries are
Zookeeper
、Eureka
etc. . - A dynamic proxy : Client call interface , The framework needs to be able to call services remotely according to the interface , This step is not perceived by the user . thus , You need to use dynamic agents , User call interface , It's actually calling the dynamically generated proxy class . Common dynamic agents are :
JDK Proxy
,CGLib
,Javassist
etc. . - Network transmission :RPC A remote call is actually a network transfer , So network transmission is RPC An essential part of the framework . The network framework has
Java NIO
、Netty
Frame, etc . - Custom protocol : Network transmission needs to formulate a good protocol , A good protocol can improve the efficiency of transmission .
- serialize : Network transmission will certainly involve serialization , Common serialization methods are
Json
、Protostuff
、Kyro
etc. . - Load balancing : When the number of requests is large , You need to increase the number of servers , Once added , It will involve the choice of services , This is load balancing . Common load balancing strategies are : polling 、 Random 、 Weighted polling 、 Weighted random 、 Consistent hashes and so on .
- Cluster fault tolerance : When the request service is abnormal , Should we report the error directly ? Try again ? Or ask for other services ? This is the cluster fault tolerance strategy .
- ....
The above design , The code implementation will be introduced in detail in the next article .