Angular8 officially released, Java13 have released a few months, so fast iterative techniques, increasing the complexity of the frame, but the basic principle is the same. So heart sank to see the nature of the code is very important, this time to give us a hand RPC framework.
The complete code and documentation, I point jump –
1. What is RPC?
RPC remote procedure call means, that is to say two servers A, B, A an application deployed on the server you want to call functions / methods provided by the application server B, because it is not a memory space, can not be called directly, need network data semantics to express and communicate call call.
RPC framework there are many more well-known as Ali Dubbo, google the gRPC, rpcx Go language, Apache’s thrift, Sina motan and so on. Of course, you can also say Dubbo is a superset of RPC framework, in addition to the Spring Cloud is, RPC only one of its modules. (Saying Dubbo has been promoted to the top-level Apache project, and not with hands-on learning at?)
RPC two issues to be addressed:
Solve a distributed system, problems between service calls.
When long-distance calls to local calls as easy as possible to let the caller can not perceive the logic of remote calls.
2. Technical Selection
Basic framework of the project, providing IoC function. Labeled JAR package while convenient, easy to test.
NIO-based network communication frame. Compared with BIO java socket communication implementation, Netty whether it is simple to write code or NIO-based asynchronous non-blocking properties, they are at an advantage.
Serialization, deserialization. RPC communication frame there are many choices: kyro / thift / json / hessian / protobuf / xml, etc., in order to achieve rapid frame RPC, saving time cost function to generate the stub, so Ali employed herein fastjson.
It provides discovery and registration services. There will be multiple instances of a service, so we use the registry as a zookeeper, when calling to obtain services from a zookeeper instances list, and then select call.
3. RPC implementation process
Consumers end process:
Proxy service interface
Service Discovery (connection zookeeper, get a list of the producer)
Long-distance calls (polling producer service list), such as the class name, method name and parameter information to the terminal through a proxy producer
Producer end process:
Loading Services require a remote call, and caches
Start Communication Server (Netty)
Service registration (the communication address into the zookeeper)
Using reflection, and the result of the encoding method of performing a local call is returned to the consumer terminal receives the message
In the call to break the link, you can understand the RPC communication theory and communication theory Netty’s the same time, the code for each method and class have made a comment – practice makes perfect, look at the code word is better than all the blog introduction.
4. unimplemented function
Such mature compared Dubbo RPC framework also not implement the following functions:
How to choose from multiple instances where one out and make calls, which use the load-balancing. The load balancing policy is certainly more than one, how the policy can be made configurable? But also how to implement these strategies?
Server side should really go to each call query interface query anyway? It is not to think about support caching?
Server interfaces changed, the old interface how to do?
After completion of the client call interface, you do not want to wait for the server to return, want to do some other things, how to support?
To shut down the server, the request has not been processed, how to deal with elegant?
Finally, my experience is limited to a limited level, readers are welcome valuable suggestions and comments on the text of the opinion. If you want to get more resources or want to learn more and exchange of technology enthusiasts together, I can focus on the public number “whole food engineer Xiaohui,” replies the background Keywords receive learning materials, into the front and back end technology exchange group and program members sideline group. Programmers also can join the group sideline Q group: 735 764 906 with the exchange.