当前位置: 首页 > article >正文

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

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

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

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

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

import java.rmi.Remote;
import java.rmi.RemoteException;

public  interface  IService extends Remote {
    String service(String content) throws RemoteException;
}
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;
    }
}
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");
    }
}
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();
        }

    }
}


http://www.kler.cn/news/273058.html

相关文章:

  • 如何用 UDP 实现可靠传输?并以LabVIEW为例进行说明
  • springboot277流浪动物管理系统
  • python 直方图
  • js基础语法大全(时间戳,uuid,字符串转json)
  • 大模型—概念
  • 从零开始学HCIA之SDN04
  • HTML_CSS练习:HTML注释
  • 掘根宝典之C++RTTI和类型转换运算符
  • 【通信原理笔记】【二】随机信号分析——2.4 复随机过程
  • 提升地理空间分析效率,火山引擎ByteHouse上线GIS能力
  • 基于正点原子潘多拉STM32L496开发板的简易示波器
  • 【Unity】Transform、Rigidbody、CharacterController移动
  • Linux:搭建ntp服务器
  • Python面试笔记
  • Vue.js+SpringBoot开发食品生产管理系统
  • 【GPT-SOVITS-02】GPT模块解析
  • Android APP启动优化
  • 18个惊艳的可视化大屏(第26辑):航空与运输业
  • 基于单片机的老人防丢系统设计
  • python 基础知识点(蓝桥杯python科目个人复习计划65)