Java RMI 使用

RMI方法实现
https://blog.csdn.net/MAOZEXIJR/article/details/78841094

rmiInfo
https://labs.portcullis.co.uk/tools/

Hello.java

package rmi;

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

public interface Hello extends Remote{ // 远程对象的接口必须拓展 java.rmi.Remote 接口
    String sayHello() throws RemoteException; // 接口中所有方法必须抛出 RemoteException 异常, 因为远程调用缺乏可靠性,总是存在失败的可能。
}

Server.java

package rmi;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

public class Server implements Hello{

    public Server(){super();};
    
    //The implementation class Server implements the remote interface Hello,
    //providing an implementation for the remote method sayHello. The method sayHello does not need to declare that it throws any exception because the method implementation itself does not throw RemoteException nor does it throw any other checked exceptions. 
    public String sayHello() {
        return "Hello, world";
    }

    public static void main(String[] args){
        try {
            // 创建本机 1099 端口上的RMI registry
            Registry registry = LocateRegistry.createRegistry(1099);
            
            // jdk 1.5 以后利用 UnicastRemoteObject 动态生成 Stub
            // jdk 1.5 之前需要使用 rmic 命令来创建
            Server obj = new Server();
            
            
            Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 1099);
            // 有些RMI示例代码会让接口实现类直接继承自 UnicastRemoteObject ,效果是一样的,参见 UnicastRemoteObject 的构造函数,或则在接口实现类构造函数中做这个工作也可以

            // 将 Stub 绑定到RMI注册表中,方式多样,上文已经提过
            registry.bind("Hello", stub);
            
            System.err.println("Server ready");
        } catch (Exception e) {
            System.err.println("Server exception: " + e.toString());
            e.printStackTrace();
        }
    }
}

Client.java

package rmi;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;


public class Client {
	
	public static void main(String[] args) {
        try {
             Registry registry = LocateRegistry.getRegistry("localhost");
             Hello hello = (Hello) registry.lookup( "Hello");
             String ret = hello.sayHello();
             System. out.println( ret);
       } catch (Exception e) {
              e.printStackTrace();
       }
    }
}

Groovy调用RMI方法

package rmi;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
 Registry registry = LocateRegistry.getRegistry("localhost");
Hello hello = (Hello) registry.lookup( "Hello");
String ret = hello.sayHello();
log.info ( ret);

报错

问题原因:

异常说的很明确CardUserInfo这个对象无权限使用。因为服务端返回来的CardUserInfo这个对象和客户端的CardUserInfo不一致。RMI要求这两个类必须一直,包括包名和方法属性等。

解决方案: 将Hello类打包成jar文件,放在 SmartBear\SoapUI-Pro-5.1.2\bin\ext

 用 rmiInfo-0.3.jar 查看

猜你喜欢

转载自www.cnblogs.com/sui84/p/12682039.html