rpc frameworks include: 1. RMI, remote method invocation; 2. Hessian, remote method invocation based on HTTP; 3. Dubbo, Taobao's open source TCP-based RPC framework.
rpc framework has:
RPC is the abbreviation of remote procedure call , widely used in large-scale distributed applications, its role is to facilitate 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. 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 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 transmitting objects, and the management and failover design of the registration center (using zookeeper).
The client and server can run in different JVMs. The client only needs to introduce the interface. The implementation of the interface and the data required for runtime are all on the server side. RPC mainly relies on technology. It is a serialization, deserialization and transmission protocol. In JAVA, it corresponds to the serialization, deserialization and transmission of serialized data. RMI's serialization and deserialization are native to JAVA. Serialization and deserialization in Hessian are private, and the transmission protocol is HTTP. Dubbo's serialization can be selected from a variety of options. Hessian's serialization protocol is generally used. The transmission is TCP protocol, using the high-performance NIO framework Netty. I also know something about serialization, such as Google's ProBuffer, JBoss Marshalling and Apache Thrift, etc. I have previously written a blog post introducing ProBuffer
1. RMI (Remote Method Invocation)
JAVA’s own remote method invocation tool, but it has certain limitations. After all, it is the original design of the JAVA language. Later, the principles of many frameworks were 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; // 服务实现 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 { // 方法的具体实现 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客户端 public class Client { public static void main(String[] args) { // 注册管理器 Registry registry = null; try { // 获取服务注册管理器 registry = LocateRegistry.getRegistry("127.0.0.1",8088); // 列出所有注册的服务 String[] list = registry.list(); for(String s : list){ System.out.println(s); } } catch (RemoteException e) { } try { // 根据命名获取服务 IService server = (IService) registry.lookup("vince"); // 调用远程方法 String result = server.queryName("ha ha ha ha"); // 输出调用结果 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服务端 public class Server { public static void main(String[] args) { // 注册管理器 Registry registry = null; try { // 创建一个服务注册管理器 registry = LocateRegistry.createRegistry(8088); } catch (RemoteException e) { } try { // 创建一个服务 ServiceImpl server = new ServiceImpl(); // 将服务绑定命名 registry.rebind("vince", server); System.out.println("bind server"); } catch (RemoteException e) { } } }
Service registration management The server is written in the Server, and of course it can be extracted as a separate service. In some other frameworks, Zookeeper is often used to play the role of registration management.
2. Hessian (HTTP-based remote method invocation)
Based on HTTP protocol transmission, its performance is still good It's not perfect. Load balancing and failover depend on the application's load balancer. The use of Hessian is similar to RMI. The difference is that the role of Registry is downplayed. It is called through the displayed address and uses HessianProxyFactory to create a proxy object based on the configured address. In addition Also introduce the Hessian Jar package.
3. Dubbo (Taobao open source TCP-based RPC framework)
The high-performance RPC framework based on Netty is open sourced by Alibaba. The overall principle is as follows:
The detailed description of Dubbo is very detailed in Taobao Open Source. Dubbo is used in many production projects at work, and many things that need attention are also discovered in the process, especially that Numerous configurations and improper settings will be annoying. It is best to customize and optimize it based on the existing open source Dubbo.
Related free learning recommendations:
java basic tutorial
The above is the detailed content of What are the rpc frameworks?. For more information, please follow other related articles on the PHP Chinese website!