RPC is the abbreviation of remote procedure calls. It is widely used in large-scale distributed applications. Its function is to help the vertical split of the system and make the system easier to expand. There are many RPC frameworks in Java, each with its own characteristics, and the widely used ones include RMI, Hessian, Dubbo, etc. Another feature of RPC is that it can cross languages. This article only takes RPC in JAVA language as an example.
There is a logical relationship diagram for RPC, taking RMI as an example:
Other framework structures are similar, the differences are in the serialization method of the object, the communication protocol for transmitting the object, the management of the registration center and the failover design (using zookeeper).
The client and the server can run in different JVMs. Client only needs to introduce interfaces. The implementation of the interface and the data required at runtime are all on the Server side. RPC's main dependency technology is serialization, deserialization and transmission protocols. JAVA corresponds to the serialization, deserialization and transmission of objects and the transmission of serialized data. RMI serialization and deserialization are owned by JAVA. The serialization and deserialization in Hessian are private, the transmission protocol is HTTP, and Dubbo serialization can be selected in many ways. Generally, Hessian serialization protocol is used, and the transmission is TCP protocol, which uses the high-performance NIO framework Netty. Regarding serialization, I also know some things, such as Google's ProBuffer, JBoss Marshalling, Apache Thrift, etc. I wrote a blog post introducing ProBuffer before.
1. RMI (remote method call)
The remote method calling tool that comes with JAVA has certain limitations. After all, it was the design of the JAVA language at the beginning. Later, the principles of many frameworks were based on RMI. The use of RMI is as follows:
External interface
public interface IService extends Remote { public String queryName(String no) throws RemoteException; }</span>Service implementation
import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; // Service Implementation public class ServiceImpl extends UnicastRemoteObject implements IService { /** */ private static final long serialVersionUID = 682805210518738166L; /** * @throws RemoteException */ protected ServiceImpl() throws RemoteException { super(); } /* (non-Javadoc) * @see com.suning.ebuy.wd.web.IService#queryName(java.lang.String) */ @Override public String queryName(String no) throws RemoteException { // Specific implementation of the method System.out.println("hello" + no); return String.valueOf(System.currentTimeMillis()); } }RMI Client
import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; // RMI server public class Server { public static void main(String[] args) { // Register Manager Registry registry = null; try { // Create a service registry = LocateRegistry.createRegistry(8088); } catch (RemoteException e) { } try { // Create a service ServiceImpl server = new ServiceImpl(); // Bind the service and name the registry.rebind("vince", server); System.out.println("bind server"); } catch (RemoteException e) { } } }The service registration manager is written in the Server, and of course it can also be extracted as a separate service. In some other frameworks, Zookeeper is often used as a registration management role.
2. Hessian (remote method call based on HTTP)
Based on HTTP protocol transmission, the performance is not perfect. Load balancing and failure transfer depend on the application's load balancer. Hessian's use is similar to RMI. The difference is that it downplays the role of Registry. Through the displayed address call, HessianProxyFactory uses a proxy object based on the configured address. In addition, Hessian's Jar package is also introduced.
3. Dubbo (TCP-based RPC framework open source on Taobao)
The high-performance RPC framework based on NIO framework Netty is open source by Alibaba. The overall principle is as follows:
Before understanding Dubbo, you must first have an in-depth understanding of Zookeeper. After understanding Zookeeper, Dubbo will have no secrets.
Zookeeper is the registration center of Dubbo services. Dubbo's original database-based registration center did not use Zookeeper. Zookeeper is a distributed service framework. It is a data storage for tree-type directory services and can manage data in clusters. It can be used as a registration center for Dubbo services. Dubbo can perform cluster deployment with Zookeeper. When a provider has an abnormal downtime such as power outage or other off-air, the Zookeeper registration center can automatically delete the provider information. When the provider restarts, it can automatically restore the registration data and subscription requests.
Dubbo's detailed description is very detailed in Taobao Open Source. In many production projects, Dubbo is used in many production projects. During the process, I also found many things to pay attention to, especially the many configurations, which are not properly set up, and it is best to customize and optimize them based on the existing open source Dubbo.