一个//ip:port/name能够惟必定位一个RMI服务器上的发布了的对象html
其中 host 是注册表所在的主机(远程或本地),port 是注册表接受调用的端口号,name 是未经注册表解释的简单字符串。host 和 port 二者都是可选项。若是省略了 host,则主机默认为本地主机。若是省略了 port,则端口默认为 1099,该端口是 RMI 的注册表 rmiregistry 使用的“著名”端口。java
简单说下工做的原理:服务器
这里假设RMI有3个部分构成,第一个是RMIService即JDK提供的一个能够独立运行的程序(bin目录下的rmiregistry),第二个是RMIServer即咱们本身编写的一个java项目,这个项目对外提供服务。第三个是RMIClient即咱们本身编写的另一个java项目,这个项目远程使用RMIServer提供的服务。函数
首先,RMIService必须先启动并开始监听对应的端口。测试
其次,RMIServer将本身提供的服务的实现类注册到RMIService上,并指定一个访问的路径(或者说名称)供RMIClient使用。url
最后,RMIClient使用事先知道(或和RMIServer约定好)的路径(或名称)到RMIService上去寻找这个服务,并使用这个服务在本地的接口调用服务的具体方法。spa
通俗的讲完了再稍微技术的讲下:server
首先,在一个JVM中启动rmiregistry服务,启动时能够指定服务监听的端口,也可使用默认的端口。xml
其次,RMIServer在本地先实例化一个提供服务的实现类,而后经过RMI提供的Naming,Context,Registry等类的bind或rebind方法将刚才实例化好的实现类注册到RMIService上并对外暴露一个名称。htm
最后,RMIClient经过本地的接口和一个已知的名称(即RMIServer暴露出的名称)再使用RMI提供的Naming,Context,Registry等类的lookup方法从RMIService那拿到实现类。这样虽然本地没有这个类的实现类,但全部的方法都在接口里了,想怎么调就怎么调吧。
值得注意的是理论上讲RMIService,RMIServer,RMIClient能够部署到3个不一样的JVM中,这个时候的执行的顺序是RMIService---RMIServer—RMIClient。另外也能够由RMIServer来启动RMIService这时候执行的顺序是RMIServer—RMIService—RMIClient。
除了这些外再说几点理论上的废话:
一、使用RMI的时候RMIClient须要知道RMIServer注册时暴露出来的路径或名称。
二、RMIClient要有实现类的接口。
三、接口必定要继承Remote。
四、实现类除了实现接口外还要实现Serializable接口,有须要的话还要继承UnicastRemoteObject。
RMI的基础是接口,RMI构架基于一个重要的原理:定义接口和定义接口的具体实现是分开的。
一个简单的RMI系统,通常能够分红4个文件,下面来介绍各个文件的建立和做用
第一步:建立一个远程对象接口
import java.rmi.Remote;
import java.rmi.RemoteException;
/*
* 这个接口继承自Remote,每个定义的方法都必须抛出一个RemoteException异常对象
* 咱们可供远程调用的方法就是经过这里开公开
*/
public interface IRMI extends Remote{
public String invoke() throws RemoteException;
}
第二步:建立接口的具体实现类
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
/*
* 远程对象的实现
* 公开方法的具体实现就是这里定义的
*/
public class IRMIImpl extends UnicastRemoteObject implements IRMI {
protected IRMIImpl() throws RemoteException {
super(); // 这个实现必须有一个显式的构造函数,而且要抛出一个RemoteException异常
}
private static final long serialVersionUID = 6131922116577454476L;
public String invoke() throws RemoteException { //该方法公开
return "hello,world!";
}
public String tryInvoke() throws RemoteException{ //该方法未公开,若要公开请在接口中定义
return "try to remote me";
}
}
第三步:建立RMI服务器
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
/*
* 远程对象的注册类 该类应该在服务器端执行,执行以后
* 该机器将变为RMI服务器 客户端能够经过正确的url来访问
* 服务器上的远程对象,执行对外报露的方法
*/
public class RMIServer {
static int port = 8888;
/*
* 建立一个Registry对象.
* LocateRegistry用于获取名字服务或建立名字服务.
* 调用LocateRegistry.createRegistry(int port)方法能够在某一特定端口建立名字服务,从而用户无需再手工启动rmiregistry
* @return 返回一个Registry对象
*/
private static Registry createRegistry() {
Registry registry = null;
try {
registry = LocateRegistry.getRegistry(port); //若是该端口未被注册,则抛异常
registry.list(); //拿到该端口注册的rmi对象
} catch (final Exception e) {
try {
registry = LocateRegistry.createRegistry(port);//捕获异常,端口注册
} catch (final Exception ee) {
ee.printStackTrace();
}
}
return registry;
}
/**
* 将对象注册到rmi服务器上
*/
public static void bind() {
Registry registry = createRegistry();
try {
IRMIImpl impl = new IRMIImpl();
registry.rebind("mytask", impl); //这就是绑定,client里lookup必须和"mytast"同样才能远程调用impl
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
bind();
} catch (Exception e) {
e.printStackTrace();
}
}
}
上面是一种比较好的写法,若是只是要测试,能够直接在main()方法中写:
ImplementClass ic = new ImplementClass(); //具体实现类
Registry r = LocateRegistry.createRegistry(8888);
r.bind("mytask", ic);
//Naming.rebind("rmi://localhost:8888/mytask", ic); 可替换上句
1.注册一个端口 2.在注册端口绑定taskName和implementClass 3.客户端就能够经过url和taskName来找到implementClass。
第四步:建立RMI客户端
import java.rmi.Naming;
public class RMIClient {
/**
* 调用远程对象中的方法
* @throws Exception
*/
public static void getRemoteObject() throws Exception{
/*获得远程发布的服务
返回与指定 name 关联的远程对象的引用(一个stub)*/
IRMI obj = (IRMI)Naming.lookup("rmi://localhost:"+RMIServer.port+"/mytask"); //注:经过接口拿
System.out.println(obj.invoke()); //调用远程服务的方法
}
public static void main(String[] args) {
try {
getRemoteObject();
} catch (Exception e) {
e.printStackTrace();
}
}
}
实际使用过程当中遇到奇怪状况
registry.rebind("bpserver", ic);绑定服务就一直不动
换成Naming.rebind("rmi://localhost:2005/bpserver", ic); 就能够,其中原理待研究?
参考
http://skanion.iteye.com/blog/1168747
http://blog.sina.com.cn/s/blog_4918a7d90100oftg.html