Introduction to three commonly used RPC frameworks in JAVA

RPC is the abbreviation of Remote Procedure Call. 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. RMI, Hessian, Dubbo, etc. are widely used. Another feature of RPC is that it can cross languages. This article only takes RPC in the JAVA language as an example.
There is a logical relationship diagram for RPC, taking RMI as an example:

other framework structures are similar, the difference lies in the serialization method of objects, the communication protocol for transferring objects, and the management and failover design of the registry (using zookeeper).
The client and the server can run in different JVMs. The client only needs to introduce an interface. The implementation of the interface and the data required at runtime are all on the server. The main technology of RPC is serialization, deserialization and transmission protocol. JAVA It corresponds to the serialization, deserialization and transmission of serialized data of objects. The serialization and deserialization of RMI comes with JAVA, the serialization and deserialization in Hessian are private, the transmission protocol is HTTP, and the serialization of Dubbo can be selected in various ways. Generally, the serialization protocol of Hessian is used. The transmission is the TCP protocol, using the high-performance NIO framework Netty. For serialization, I also know some things, such as Google's ProBuffer, JBoss Marshalling and Apache Thrift, etc. I wrote a blog post about ProBuffer before.
1. RMI (Remote Method Invocation)
JAVA's own remote method invocation tool, but there are certain After all, it is the design of the JAVA language at the beginning. Later, the principles of many frameworks are based on RMI. The use of RMI is as follows:
External interface
<span style="font-size:12px;">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)
     *
     */  
    @Override  
    public String queryName(String no) throws RemoteException {  
        // concrete implementation of the method  
        System.out.println("hello" + no);  
        return String.valueOf(System.currentTimeMillis());  
    }  
      
}  

RMI client
import java.rmi.AccessException;  
import java.rmi.NotBoundException;  
import java.rmi.RemoteException;  
import java.rmi.registry.LocateRegistry;  
import java.rmi.registry.Registry;  
  
// RMI client  
public class Client {  
  
    public static void main(String[] args) {  
        // registration manager  
        Registry registry = null;  
        try {  
            // Get the service registry manager  
            registry = LocateRegistry.getRegistry("127.0.0.1",8088);  
            // list all registered services  
            String[] list = registry.list();  
            for(String s : list){  
                System.out.println(s);  
            }  
        } catch (RemoteException e) {  
              
        }  
        try {  
            // Get service by name  
            IService server = (IService) registry.lookup("vince");  
            // call remote method  
            String result = server.queryName("ha ha ha ha");  
            // print the result of the call  
            System.out.println("result from remote : " + result);  
        } catch (AccessException e) {  
              
        } catch (RemoteException e) {  
              
        } catch (NotBoundException e) {  
              
        }  
    }  
}  

RMI server
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) {  
        // registration manager  
        Registry registry = null;  
        try {  
            // Create a service registry manager  
            registry = LocateRegistry.createRegistry(8088);  
  
        } catch (RemoteException e) {  
              
        }  
        try {  
            // create a service  
            ServiceImpl server = new ServiceImpl();  
            // Name the service binding  
            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 be extracted as a service alone. In some other frameworks, Zookeeper is often used as the registration management role.

2. Hessian (HTTP-based remote method invocation) is
based on HTTP protocol transmission, which is not perfect in terms of performance. Load balancing and failover depend on the load balancer of the application. The use of Hessian is similar to RMI, the difference is that the Registry is diluted. Role, call through the displayed address, use HessianProxyFactory to create a proxy object according to the configured address, and also introduce Hessian's Jar package.

3. Dubbo (Taobao's open-source TCP-based RPC framework)
is a high-performance RPC framework based on Netty, which is open sourced by Alibaba. The general principle is as follows:

Before understanding Dubbo, you must first have a deep understanding of Zookeeper. After understanding zookeeper , Dubbo will have no secrets.
The detailed description of Dubbo is very detailed in Taobao Open Source. Many production projects use Dubbo in the work. In the process, we also found a lot of things to pay attention to, especially the numerous configurations. Improper settings will be annoying. It is best to customize and optimize it based on the existing open source Dubbo.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326384558&siteId=291194637