分布式计算(一)RMI

1、创建

  1. 创建接口

    public interface MyRemote extends Remote{
    public String say() throws RemoteException
    }
    String必须实现序列化Serializataion接口
    方法必须抛出RemoteException

  2. 实现接口
    public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote{
    //实现MyRemote
    try{
    MyRemote service =new MyRemoteImpl();//创建MyRemote对象
    Naming.rebind(“Remote Hello”,service);//将MyRemote对象放入rmiregistry,此时只是代码,就算构建也没放入,因为还没启动rmiregistry,所以其实还没有放入
    }
    必须继承UnicastRemoteObject或者其他的与远程有关的功能

  3. 获取stub与skeletion(.class)文件
    调用jdk中的rmic对MyRemoteImpl进行加工,产生两个辅助类
    MyRemoteImpl_Stub.class与MyRemoteImpl_Skeleton.class

  4. 启动rmiregistry

  5. 初始化远程服务
    构建MyRemoteImpl,将MyRemote对象放入自己电脑的rmiregistry,以供别的电脑的调用。

2、调用

  1. 客户端应有MyRemote.class,MyRemoteImpl_Stub.class,服务端应有MyRemote.class,MyRemoteImpl.class,MyRemoteImpl_Stub.class与MyRemoteImpl_Skeleton.class两个辅助类,

  2. 客户端调用代码

    MyRemote service=(MyRemote)Naming.lookup("rmi://ip/注册名");
    String str=service.say();
    
  3. 实际运行过程中,客户端通过注册名查询,返回一个stub对象,调用stub对象的say,stub对象远程调用服务端的skeleton,skeleton调用服务端被放入registry的service对象。在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值