RMI心得 (注册端口)

虽然现在在分布式中RMI已经很少用到,但作为最简单的分布式应用,我们还是稍微了解一下好,这篇文章可能有我理解错误的地方,希望大家指出,谢谢。

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();
}
}
}


运行RMI系统:启动RMI服务器,启动客户端即可。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值