This paper analyzes several common RPC frameworks in Java
RPC is the abbreviation of remote procedure call. It is widely used in large-scale distributed applications. Its role is to help the vertical splitting of the system and make the system easier to expand. There are many RPC frameworks in java with their own characteristics. RMI, Hessian, Dubbo, etc. are widely used. Another feature of RPC is that it can cross language. This paper only takes RPC in Java language as an example.
There is a logic diagram for RPC. Take RMI as an example:
Other frameworks are similar, with the difference in the serialization method of objects, the communication protocol for transmitting objects, and the management of the registry and the design of failover (using zookeeper).
The client and server can run in different JVMs. The client only needs to introduce an interface. The implementation of the interface and the data required during operation are on the server side. The main dependent technologies of RPC are serialization, deserialization and transmission protocol. In Java, the corresponding technologies are object serialization, deserialization and data transmission after serialization. The serialization and deserialization of RMI comes with Java. The serialization and deserialization in hessian are private. The transmission protocol is http. Dubbo's serialization can be selected from a variety of options. Generally, Hessian's serialization protocol is used, and the transmission protocol is TCP protocol, which uses the high-performance NiO framework netty. I also know something about serialization, such as Google's probuffer, JBoss marshalling and Apache thrift. I wrote a blog about probuffer before
1. RMI (remote method call)
Java has its own remote method calling tool, but it has some limitations. After all, it was designed at the beginning of Java language. Later, the principles of many frameworks are based on RMI. The use of RMI is as follows:
External interface
Service implementation
RMI client
The service registration manager is written in the server. Of course, it can also be extracted as a separate service. In other frameworks, zookeeper is often used as the registration management role.
2. Hessian (HTTP based remote method call)
The transmission based on HTTP protocol is not perfect in terms of performance. Load balancing and failure transfer depend on the application load balancer. The use of Hessian is similar to RMI. The difference is that it plays down the role of registry. Through the displayed address call, Hessian proxyfactory is used to create a proxy object according to the configured address. In addition, Hessian jar package is introduced.
3. Dubbo (Taobao open source TCP based RPC framework)
The high-performance RPC framework based on NiO framework netty is open source by Alibaba. The general principle is as follows:
Before getting to know Dubbo, you should have a deep understanding of zookeeper. When you understand zookeeper, Dubbo will have no secrets.
Zookeeper is the registration center of Dubbo service. Dubbo's original database-based registration center did not use zookeeper. Zookeeper, a distributed service framework, is the data storage of tree directory service and can manage data in clusters. It can be used as the registration center of Dubbo service. Dubbo can deploy in clusters with zookeeper, In case of abnormal shutdown such as power failure, the zookeeper registration center can automatically delete the provider information. When the provider restarts, it can automatically restore the registration data and subscription requests.
The detailed description of Dubbo is very detailed in Taobao open source. Dubbo is used in many production projects at work, and a lot of points needing attention are found in the process, especially the various configurations and improper settings will be annoying. It is best to customize and optimize based on the existing open source Dubbo.