【问题标题】:Cannot establish connection with RMI Registry on a remote server无法与远程服务器上的 RMI Registry 建立连接
【发布时间】:2020-03-05 18:09:24
【问题描述】:

目前我正在尝试在远程机器上使用 RMI 启动 java 服务器。服务器似乎已启动,但我无法在客户端上调用它的方法。 在客户端上,我得到一个如下所示的异常:

Exception in thread "main" java.rmi.ConnectException: Connection refused to host: 10.0.0.6; nested exception is: 
    java.net.ConnectException: Operation timed out (Connection timed out)

这是我的 RMI 服务器构造函数:

    private RmiServer(String address, int port) throws RemoteException, SQLException, MalformedURLException {
        System.out.println("This address = " + address + ", Port = " + port);
        Registry registry;
        try {
            registry = LocateRegistry.createRegistry(PORT);
        } catch (RemoteException e)
        {
            registry = LocateRegistry.getRegistry(PORT);
        }
        System.setProperty("java.rmi.server.hostname", address);
        try {
            registry.bind(REGISTRY_NAME, this);
            Naming.bind(address, this);
        }
        catch (AlreadyBoundException e)
        {
            registry.rebind(REGISTRY_NAME, this);
            Naming.rebind(address, this);
        }
        ...
    }

这是我的客户端代码:

    static public void main(String[] args) throws IOException {
        try {
            Registry registry = LocateRegistry.getRegistry(SERVER_ADDRESS, SERVER_PORT);
            rmiServer = (ReceiveMessageInterface) registry.lookup(REGISTRY_NAME);
        } catch (RemoteException | NotBoundException e) {
            System.err.println(e.getMessage());
            isServerOK = false;
        }
        Scanner reader = new Scanner(new InputStreamReader(System.in));
        boolean isAuthenticated = false;
        int pin;
        String cardNumber;
        SessionInfo session = null;
        if (isServerOK) {
            while (true) {
                if (!isAuthenticated) {
                    System.out.println("Welcome Customer to HDFC Bank ATM : \nPlease enter your card number: ");
                    cardNumber = reader.next();
                    System.out.println("Please enter your PIN: ");
                    pin = reader.nextInt();
                    String cardHolderName = rmiServer.verifyPIN(cardNumber, pin);
                    ...
                }
                ...
            }
            ...
        }
        ...
    }

我用这个字符串启动服务器:

java -cp flyway-core-6.0.8.jar:mssql-jdbc-7.4.1.jre8.jar:mysql-connector-java-8.0.18.jar:. -Djava.security.policy=java.security.AllPermission kmalfa.RmiServer

我的服务器在 linux 虚拟机上运行。我使用 Microsoft Azure 服务。我关闭了客户端和虚拟机上的防火墙。我允许 Azure 设置中的所有传入连接用于测试目的,但它没有帮助。 可能是什么问题?

【问题讨论】:

  • 唯一可能的解决方案是设置我的代码库,但我不确定如何正确设置它...也许有人可以详细解释一下?...
  • 不,不是。您应该删除安全管理器。你不需要它。这个问题是 RMI FAQ 上的 A.1 项..
  • @user207421 我尝试使用 RMI 常见问题解答项目 A.1 中的解决方案,但它对我没有帮助。我仍然得到异常。
  • 如果在显示的 IP 地址相同的情况下仍然出现相同的异常,则说明您没有做对。
  • @user207421 你能解释一下我需要如何绑定注册表吗?我更新了我的问题中的代码。我是否正确设置了注册表?我如何需要在服务器上使用 Naming.bind 并在客户端上使用 Naming.lookup?

标签: java azure client-server rmi firewall


【解决方案1】:

解决了。

只需要打电话

System.setProperty("java.rmi.server.hostname", address);

不在RmiServer构造函数中,而是在调用它之前,在main

的开头

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-25
    • 1970-01-01
    • 2014-09-27
    • 2016-08-12
    • 2020-04-06
    • 2012-10-17
    • 1970-01-01
    相关资源
    最近更新 更多