Java面试题总结15之简述你对RPC,RMI的理解

RPC:在本地调用远程的函数,远程过程调用,可以跨语言实现,httpClient

RMI:远程方法调用,Java中用于实现RPC的一种机制,RPC的Java版本是J2EE的网络调用机制,跨JVM调用对象的方法,面向对象的思维方式

RMI实现:直接或间接实现接口Java.rmi.Remote 成为存在服务器端的远程对象,供客户端访问并提供一定的服务

远程对象必须实现Java.rmi.server.uniCastRemoteObject类,这样草能保证客户端访问获得远程对象时,该远程对象将会把自身的一个拷贝以Socket的形式传输给客户端,此时客户端所获得的这个拷贝称为存根,而服务器端本身已存在的远程对象则称之为骨架,其实此时的存根时客户端的一个代理,用于与服务器端的通信,而骨架也可认为是服务器端的一个代理,用于接受客户端的请求之后调用远程方法来响应客户端的请求

java 复制代码
import java.rmi.Remote;
import java.rmi.RemoteException;

public  interface  IService extends Remote {
    String service(String content) throws RemoteException;
}
java 复制代码
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class IServiceImpl extends UnicastRemoteObject implements IService {
    private  String name;

   public  IServiceImpl(String name) throws  RemoteException {
       this.name = name;
   }

    @Override
    public String service(String content) {
        return "server >>" + content;
    }
}
java 复制代码
import javax.naming.Context;
import javax.naming.InitialContext;
public class Server {

    public static void main(String[] args) {
        try {
            IService service02 =    new IServiceImpl("service02");
            Context context = new InitialContext();
            context.rebind("rmi://127.0.0.1/service02",service02);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("00000");
    }
}
java 复制代码
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class Client {
    public static void main(String[] args) {
        String url = "rmi://127.0.0.1/";

        try {
          Context  context = new InitialContext();
            IService service02 = (IService) context.lookup(url+"service02");
            Class stubClass = service02.getClass();
            System.out.println(service02 + "is" + stubClass.getName());

            Class[] interfaces = stubClass.getInterfaces();
            for (Class c : interfaces) {
                System.out.println("implement" +c.getName()+ "interface");
            }
            System.out.println(service02.service("hello"));
        } catch (Exception e) {
          e.printStackTrace();
        }

    }
}
相关推荐
运维行者_6 分钟前
OPM 与传统管理工具的区别,在网络修复与自动化运维方面的优势在哪里?
运维·服务器·开发语言·网络·自动化·php·ssl
广州灵眸科技有限公司30 分钟前
瑞芯微(EASY EAI)RV1126B 音频输入
linux·开发语言·网络·音视频
网安INF2 小时前
SSL/TLS密钥派生机制与安全攻防体系深度解析
网络·安全·网络安全·密码学·ssl
TG:@yunlaoda360 云老大2 小时前
华为云国际站代理商NAT的规格有哪些?
服务器·网络·华为云
talenteddriver2 小时前
web: http请求(自用总结)
前端·网络协议·http
CICI131414132 小时前
焊接机器人负载能力选择标准
网络·数据库·人工智能
黄俊懿3 小时前
【深入理解SpringCloud微服务】Seata(AT模式)源码解析——全局事务的回滚
java·后端·spring·spring cloud·微服务·架构·架构师
Xの哲學3 小时前
Linux UPnP技术深度解析: 从设计哲学到实现细节
linux·服务器·网络·算法·边缘计算
Trouvaille ~3 小时前
【Linux】从磁盘到文件系统:深入理解Ext2文件系统
linux·运维·网络·c++·磁盘·文件系统·inode
乾元3 小时前
AI 驱动的入侵检测与异常会话判别:从规则到行为分析前言:从“捕获敌人”到“守卫秩序”
运维·网络·人工智能·网络协议·安全