Let’s first take a look at the definition of Java RMI:
RMI (Remote Method Invocation, remote method invocation) is implemented in JDK1.2 using Java. It Greatly enhances Java's ability to develop distributed applications. As a popular network development language, Java's huge power is reflected in its powerful ability to develop distributed network applications, and RMI is one of the core solutions for developing 100% pure Java network distributed application systems. In fact it can be considered as the Java version of RPC. But traditional RPC cannot be well applied to distributed object systems. Java RMI supports communication between program-level objects stored in different address spaces to achieve seamless remote calls between remote objects.
RMI remote calling steps
Interaction diagram of RMI:
RMI consists of 3 parts, the first one is rmiregistry (JDK Provides a program that can be run independently (in the bin directory), the second is a server-side program that provides remote objects to the outside world, and the third is a client-side program that wants to call methods of remote objects.
First, start the rmiregistry service. When starting, you can specify the port that the service listens on, or you can use the default port (1099).
Secondly, the server first instantiates an implementation class that provides services locally, and then uses the bind or rebind method of Naming/Context/Registry (Registry used in the example below) and other classes provided by RMI to instantiate the implementation just now The class is registered with rmiregistry and exposed to the outside world with a name.
Finally, the client obtains the implementation class from RMIService through the local interface and a known name (that is, the name exposed by rmiregistry) and then uses the lookup method of Naming/Context/Registry and other classes provided by RMI. In this way, although there is no implementation class of this class locally, all the methods are in the interface, and the method of the object can be called remotely.
The specific communication process between the stub and the backbone network:
The method call is from the client object through the stub and the remote reference layer (Remote Reference Layer) and transport layer (Transport Layer) downward, passed to the host, and then again through the transport layer, upward through the remote call layer and backbone network (Skeleton), to reach the server object.
The stub acts as a proxy for the remote server object, making the object available for activation by clients.
The remote reference layer handles semantics, manages communication of single or multiple objects, and determines whether calls should be sent to one server or multiple.
The transport layer manages the actual connection and keeps track of remote objects that can accept method calls.
The backbone network completes the actual method call to the server object and obtains the return value.
The return value is passed back to the client through the remote reference layer and the server-side transport layer, and then returned upward through the transport layer and remote call layer. Finally, the stub gets the return value.
JAVA RMI simple example
This example is the addition and subtraction method of the client calling the server-side remote object. The specific steps are:
1. Define a remote interface
import java.rmi.Remote; import java.rmi.RemoteException; /** * 必须继承Remote接口。 * 所有参数和返回类型必须序列化(因为要网络传输)。 * 任意远程对象都必须实现此接口。 * 只有远程接口中指定的方法可以被调用。 */ public interface IRemoteMath extends Remote { // 所有方法必须抛出RemoteException public double add(double a, double b) throws RemoteException; public double subtract(double a, double b) throws RemoteException; }
(Learning video sharing: java video tutorial)
2. Remote interface implementation class
import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; import remote.IRemoteMath; /** * 服务器端实现远程接口。 * 必须继承UnicastRemoteObject,以允许JVM创建远程的存根/代理。 */ public class RemoteMath extends UnicastRemoteObject implements IRemoteMath { private int numberOfComputations; protected RemoteMath() throws RemoteException { numberOfComputations = 0; } @Override public double add(double a, double b) throws RemoteException { numberOfComputations++; System.out.println("Number of computations performed so far = " + numberOfComputations); return (a+b); } @Override public double subtract(double a, double b) throws RemoteException { numberOfComputations++; System.out.println("Number of computations performed so far = " + numberOfComputations); return (a-b); } }
3. Server side
import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import remote.IRemoteMath; /** * 创建RemoteMath类的实例并在rmiregistry中注册。 */ public class RMIServer { public static void main(String[] args) { try { // 注册远程对象,向客户端提供远程对象服务。 // 远程对象是在远程服务上创建的,你无法确切地知道远程服务器上的对象的名称, // 但是,将远程对象注册到RMI Registry之后, // 客户端就可以通过RMI Registry请求到该远程服务对象的stub, // 利用stub代理就可以访问远程服务对象了。 IRemoteMath remoteMath = new RemoteMath(); LocateRegistry.createRegistry(1099); Registry registry = LocateRegistry.getRegistry(); registry.bind("Compute", remoteMath); System.out.println("Math server ready"); // 如果不想再让该对象被继续调用,使用下面一行 // UnicastRemoteObject.unexportObject(remoteMath, false); } catch (Exception e) { e.printStackTrace(); } } }
4. Client The result of end
import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import remote.IRemoteMath; public class MathClient { public static void main(String[] args) { try { // 如果RMI Registry就在本地机器上,URL就是:rmi://localhost:1099/hello // 否则,URL就是:rmi://RMIService_IP:1099/hello Registry registry = LocateRegistry.getRegistry("localhost"); // 从Registry中检索远程对象的存根/代理 IRemoteMath remoteMath = (IRemoteMath)registry.lookup("Compute"); // 调用远程对象的方法 double addResult = remoteMath.add(5.0, 3.0); System.out.println("5.0 + 3.0 = " + addResult); double subResult = remoteMath.subtract(5.0, 3.0); System.out.println("5.0 - 3.0 = " + subResult); }catch(Exception e) { e.printStackTrace(); } } }
is as follows:
server end
##client end Reference:https://blog.csdn.net/xinghun_4/article/details/45787549
Related recommendations:The above is the detailed content of Analysis of Java RMI remote calling steps. For more information, please follow other related articles on the PHP Chinese website!