Start the wheel journey, this time wheel: RPC framework.
For a while, I'll write a series of articles on how to implement an RPC framework (I've implemented an example framework with code on my github). This is the first article in the series, which describes the structure and concerns of RPC framework from a holistic perspective. the focus point of the RPC framework first, what is RPC.
The full name of RPC is remote Procedure call.
There are many RPC frameworks, such as HSF, Dubbo, and so on. With the RPC framework, we can write business code without having to think about communication between services, which is as simple as invoking a local method when invoking a remote service. So, write an RPC framework of which parts should be composed and what to focus on. 1. Simplify the local call process
Now that we want to invoke the remote service as we call the local method, we should generate a proxy to hide the details of invoking the remote service. These details include, but are not limited to, the points of concern listed below. 2. Service Discovery and Service registration if we want to call service B in service A, we first have to know the address of service B. So, we need to have a service registry, through which the service can register its own information and get information about other services. The client also needs a change in the address of the target service of the Watch Service Registry 3. Network communication model between network communication Service and service, Nio/io, etc. how the client uses the connection to the server side instead of recreating a new connection on each request. After the client receives the return, how to know which request is returned and make the correct handling. 4. Serialization of Messages
How messages are serialized through the communication between services. Hessian,xml, JSON, protobuf 、......, even Java native serialization way, you have to choose one. 5. Load Balancing
The client gets a bunch of addresses through the service registry, which one to tune. The easiest way to do this is by RR, WRR to do the lb.
If you do this more deeply, you can optimize it from the following perspectives: Dynamic adjustments based on the metrics of the service instance, such as response time, and so on, using a consistent hash to increase the local cache utilization 6. Disaster-tolerant health monitoring: When a service node hangs off, How to delete this service address at the service registry. Service invocation timeout and retry: When a service instance is invoked, the timeout or error is handled. Service throttling: How to limit the maximum number of concurrent numbers. This can be analyzed from two angles from the client and service side. ?? My RPC Framework Implementation
Now my RPC framework has simply implemented these concerns and will continue to refine the code. This RPC framework is called HRPC, and if you want to see all the code you can access the project GitHub address
With regard to these concerns and specific code implementations, I will write a few more blogs about this series in the next few minutes. If you are interested, you can keep an eye on my blog.