Java RMI是什么
Java RMI(Java Remote Method Invocation),即Java远程方法调用。是Java编程语言里,一种用于实现远程过程调用的应用程序编程接口。
注:很多文章或博客把RMI说成是一种消息协议,官方定义是java 编程接口。
RMI 使用 JRMP(Java Remote Message Protocol,Java远程消息交换协议)实现,使得客户端运行的程序可以调用远程服务器上的对象。是实现RPC的一种方式。
RMI 的使用
1、server端:创建远程对象,并注册远程对象
//定义远程对象的接口
public interface HelloService extends Remote {
String say() throws RemoteException;
}
//接口的实现
public class HelloServiceImpl extends UnicastRemoteObject implements HelloService {
public HelloServiceImpl() throws RemoteException{
super();
}
@Override
public String say() throws RemoteException {
return "Hello";
}
}
//注册远程对象
public class Service {
public static void main(String[] args) throws RemoteException, AlreadyBoundException, MalformedURLException {
HelloServiceImpl helloService = new HelloServiceImpl();
LocateRegistry.createRegistry(1099);
Naming.bind("rmi://127.0.0.1/hello",helloService);
}
}
2、client端:查找远程对象,调用远程方法
public class Client {
public static void main(String[] args) throws RemoteException, NotBoundException, MalformedURLException {
HelloService helloService = (HelloService) Naming.lookup("rmi://127.0.0.1/hello");
System.out.println(helloService.say());
}
}
RMI 的原理
RMI本质是TCP网络通信,内部封装了序列化和通信过程,使用代理实现接口调用。下一篇文章带大家手写一个RPC框架,会更加清晰的明白RMI原理。
1、服务端
//调用UnicastRemoteObject构造函数,发布对象
protected UnicastRemoteObject(int port) throws RemoteException
{
this.port = port;
exportObject((Remote) this, port);
}
//创建UnicastServerRef对象,对象内有引用LiveRef(tcp通信)
public static Remote exportObject(Remote obj, int port)
throws RemoteException
{
return exportObject(obj, new UnicastServerRef(port));
}
public Remote exportObject(Remote var1, Object var2, boolean var3) throws RemoteException {
Class var4 = var1.getClass();
Remote var5;
try {
//创建远程代理类,getClientRef提供的InvocationHandler提供了TCP连接
var5 = Util.createProxy(var4, this.getClientRef(), this.forceStubUse);
} catch (IllegalArgumentException var7) {
throw new ExportException("remote object implements illegal remote interface", var7);
}
if (var5 instanceof RemoteStub) {
this.setSkeleton(var1);
}
//包装实际对象,并将其暴露在TCP端口上,等待客户端调用
Target var6 = new Target(var1, this, var5, this.ref.getObjID(), var3);
this.ref.exportObject(var6);
this.hashToMethod_Map = (Map)hashToMethod_Maps.get(var4);
return var5;
}
2、客户端
//客户端通过LocateRegistry的getRegistry方法创建RegistryImpl_Stub代理,调用RegistryImpl_Stub的newCall方法建立与服务端Skeleton的映射
public static Registry getRegistry(String host, int port,
RMIClientSocketFactory csf)
throws RemoteException
{
Registry registry = null;
if (port <= 0)
port = Registry.REGISTRY_PORT;
if (host == null || host.length() == 0) {
// If host is blank (as returned by "file:" URL in 1.0.2 used in
// java.rmi.Naming), try to convert to real local host name so
// that the RegistryImpl's checkAccess will not fail.
try {
host = java.net.InetAddress.getLocalHost().getHostAddress();
} catch (Exception e) {
// If that failed, at least try "" (localhost) anyway...
host = "";
}
}
//调用RegistryImpl_Stub的lookup方法时,看似本地调用,实则通过tcp连接发送消息到服务端
public Remote lookup(String var1) throws AccessException, NotBoundException, RemoteException {
try {
RemoteCall var2 = super.ref.newCall(this, operations, 2, 4905912898345647071L);
try {
ObjectOutput var3 = var2.getOutputStream();
var3.writeObject(var1);
} catch (IOException var18) {
throw new MarshalException("error marshalling arguments", var18);
}
super.ref.invoke(var2);
Remote var23;
try {
ObjectInput var6 = var2.getInputStream();
var23 = (Remote)var6.readObject();
} catch (IOException var15) {
throw new UnmarshalException("error unmarshalling return", var15);
} catch (ClassNotFoundException var16) {
throw new UnmarshalException("error unmarshalling return", var16);
} finally {
super.ref.done(var2);
}
return var23;
} catch (RuntimeException var19) {
throw var19;
} catch (RemoteException var20) {
throw var20;
} catch (NotBoundException var21) {
throw var21;
} catch (Exception var22) {
throw new UnexpectedException("undeclared checked exception", var22);
}
}
RMI 的优劣
1、优势
给分布计算的系统设计、编程都带来了极大的方便。只要按照RMI规则设计程序,可以不必再过问在RMI之下的网络细节了,如:TCP和Socket等等。任意两台计算机之间的通讯完全由RMI负责。调用远程计算机上的对象就像本地对象一样方便。
2、劣势
RMI对服务器的IP地址和端口依赖很紧密,但是在开发的时候不知道将来的服务器IP和端口如何,而客户端程序又依赖这个IP和端口。即客户端如何维护服务端地址和动态感知服务端地址变化的问题。
另一局限性是,RMI是Java语言的远程调用,两端的程序语言必须是Java实现。