zoukankan      html  css  js  c++  java
  • 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

    ​ 结果调用了方法

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

  • 相关阅读:
    VSPackge插件系列:常用IDE功能的封装
    C#如何加载程序运行目录外的程序集
    MSBuild编译扩展
    VSPackge插件系列:如何正确获取DTE
    VSPackge插件系列:简单文本编辑器的实现
    一步步实现自己的框架系列(四):页面与页面服务的创建
    DW 图片不显示的情况 ———网页只显示字不显示图片的情况 目录下的图片名被改动不显示图片的情况
    数据库--增、删、改、查(笛卡尔积)
    C#结构体
    C# 3循环 for语句
  • 原文地址:https://www.cnblogs.com/hmcjsc/p/15713044.html
Copyright © 2011-2022 走看看