【问题标题】:How to do naming system with RMI in different hosts?如何在不同主机中使用 RMI 进行命名系统?
【发布时间】:2017-12-15 14:32:32
【问题描述】:

我有两个java类,一个是客户端,另一个是服务器,在客户端我必须给服务器IP地址,但我想让它动态,这样客户端就知道第三台机器的IP(命名系统),它搜索方法(如 DNS)并返回提供此方法的特定服务器的 IP 地址。

已编辑:

类客户端:

public class Client {
private Client() {}

public static void main(String[] args) {

    String host = (args.length < 1) ? null : args[0];
    try {
        //Registry registry = LocateRegistry.getRegistry(host);
        Registry registry = LocateRegistry.getRegistry("192.168.1.9",1091);

        Calculator stub = (Calculator) registry.lookup("Hello");

        String response = stub.add(4,2);
        System.out.println("response: " + response);
    } catch (Exception e) {
        System.err.println("Client exception: " + e.toString());
        e.printStackTrace();
    }
}

}

服务器:

public class Server implements Calculator{

public Server() {}

public String add(int a,int b) {
    return "Hello, a+b= "+(a+b);
}
public String sub(int a,int b) {
    return "Hello, a-b= "+(a-b);
}
public static void main(String args[]) {

    try {
        Server obj = new Server();
        Calculator stub = (Calculator) UnicastRemoteObject.exportObject(obj, 0);

        // Bind the remote object's stub in the registry

        Registry registry = LocateRegistry.createRegistry(1091);
        registry.bind("Hello", stub);

        System.err.println("Server ready");
    } catch (Exception e) {
        System.err.println("Server exception: " + e.toString());
        e.printStackTrace();
    }
}

}

【问题讨论】:

  • 请提供您的代码或您目前尝试过的代码。
  • 我不知道这个问题“太宽泛”了。

标签: java dns rmi


【解决方案1】:

您不能使用 RMI 注册表来执行此操作。只有与注册表在同一主机上运行的进程才能绑定到它。您必须使用不同的命名服务,例如 LDAP。

但是,您描述的其余部分是 RMI 已经完成的。您在注册表中查找一个名称,它会返回一个存根,该存根知道如何与相应的远程对象进行通信。

【讨论】:

  • 所以我必须实现 LDAP 并将其放在单独的机器中以便客户端对其进行寻址,并将服务器的正确 IP 地址返回给客户端
  • 比这更简单。只需将 LDAP 服务器用作注册表。将远程对象绑定到 LDAP 服务器;让客户端查找 LDAP 服务器以获取存根,然后调用 remone 方法。 RMI 会为您处理好 IP 地址。
猜你喜欢
  • 2019-08-28
  • 2022-01-26
  • 1970-01-01
  • 2019-08-26
  • 2021-11-06
  • 2011-12-30
  • 1970-01-01
  • 2011-02-11
  • 2020-09-23
相关资源
最近更新 更多