RPC的要素RMI?log4j核弹级漏洞?Java RMI,远程方法调用简单实例

RPC的要素RMI?log4j核弹级漏洞?Java RMI,远程方法调用简单实例

java的RMI即远程方法调用,Remote Method Invcation,实现远程类对象的传输,支持JVM同环境调用。

image-20211220210918897

  1. RMIServer端实现

    • 创建被调用对象接口并继承Remote接口,IServerInt

      package cn.rmiServer;
      
      import java.rmi.Remote;
      import java.rmi.RemoteException;
      
      /**
       * @author 王居三木超
       * @version 1.0
       * @date 2021/12/20 20:10
       * @description TODO
       **/
      public interface IServerInt extends Remote {
          String init() throws RemoteException;
      }
      
    • 实现被调用对象接口IServerInt,继承UnicastRemoteObject,IServerImpl

      package cn.rmiServer;
      
      import java.rmi.RemoteException;
      import java.rmi.server.UnicastRemoteObject;
      
      /**
       * @author 王居三木超
       * @version 1.0
       * @date 2021/12/20 20:10
       * @description TODO
       **/
      public class IServerImpl extends UnicastRemoteObject implements IServerInt{
      
          protected IServerImpl() throws RemoteException {
          }
      
          @Override
          public String init() throws RemoteException{
              return "Hello";
          }
      }
      
      
    • 创建服务发布

      package cn.rmiServer;
      
      import java.net.MalformedURLException;
      import java.rmi.Naming;
      import java.rmi.RemoteException;
      import java.rmi.registry.LocateRegistry;
      
      /**
       * @author 王居三木超
       * @version 1.0
       * @date 2021/12/20 20:15
       * @description TODO
       **/
      public class Server {
          /**
           * <p>
           * create Method and registry to port
           * next bind Method to rmi://hostname/path
           * </p>
           */
          public static void main(String[] args) {
              try {
                  //create Method
                  IServerImpl iServer = new IServerImpl();
                  //registry to port
                  LocateRegistry.createRegistry(1099);
                  //bind Method 
                  Naming.rebind("rmi://127.0.0.1/start", iServer);
                  System.out.println("start success");
              } catch (RemoteException | MalformedURLException e) {
                  e.printStackTrace();
              }
          }
      }
      
      

2.RMI客户端实现

package cn.rimClient;

import cn.rmiServer.IServerInt;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

/**
 * @author 王居三木超
 * @version 1.0
 * @date 2021/12/20 20:11
 * @description TODO
 **/
public class ClientStart {
    public static void main(String[] args) throws RemoteException, NotBoundException, MalformedURLException {
        String URL = "rmi://127.0.0.1/";
        IServerInt lookup = (IServerInt) Naming.lookup(URL + "start");
        System.out.println(lookup.init());
    }
}

  1. 启动服务端

    image-20211220211407059

  2. 启动客户端查看结果

image-20211220211448395

​ 结果调用了方法

本例为单例测试,真实远端调用将远程接口类封装部署在客户端即可。

posted @ 2021-12-20 21:41  王居三木  阅读(83)  评论(0编辑  收藏  举报