2013-04-24 67 views
0

我在群集的一部分机器上运行我的rmi服务器。 IP很可能会改变,因此通过这个过程进行连接并不可靠。如何在RMI服务器中使用机器别名

直到现在在工作的开发盒上有一个基于IP的连接是好的,但移动到UAT我需要改变它,我不知道如何。

下面我有服务器和客户端类的代码。在这里你可以看到我使用机器ip连接。我如何用机器别名替换它?这是可能的还是我感到困惑?

感谢

private final String address = "111.111.111.111"; 
private Registry registry; 
private int port = 6789; 
private static Logger logger = Logger.getLogger(RmiServer.class); 

public RmiServer() throws RemoteException { 
    try { 
     registry = LocateRegistry.createRegistry(port); 
     registry.rebind("rmiServer", this); 
     logger.info("Server is Ready! Connected on: " + address + ":" + port + " ." + 
    } catch (RemoteException e) { 
     logger.error("Unable to start the server. Exiting the application.", e); 
     System.exit(-1); 
    } 
} 

private final String serverAddress = "111.111.111.111"; 
private String serverPort = "6789"; 
private ReceiveMessageInterface rmiServer; 
private Registry registry; 
private Logger logger = Logger.getLogger(RMIClient.class); 

public RMIClient(){ 
    try { 
     registry = LocateRegistry.getRegistry(serverAddress, (new Integer(serverPort)).intValue()); 
     rmiServer = (ReceiveMessageInterface) (registry.lookup("rmiServer")); 

     logger.info("Client started correctly"); 
    } catch (RemoteException e) { 
     logger.error("Remote object exception occured when connecting to server. Exiting application", e); 
     System.exit(-1); 
    } catch (NotBoundException e) { 
     logger.error("Not Bound Exception occured when connecting to server. Exiting application", e); 
     System.exit(-1); 
    } 
} 

回答

1
  1. 如果你问你要做的服务器导出存根在他们正确的IP地址是什么,这就是java.rmi.server.hostname属性是什么。在导出任何注册表或远程对象之前,将其设置为服务器JVM中所需的主机名或IP地址。

  2. 如果您问服务器更改其IP地址后如何让客户知道注册表的位置,则没有单个可接受的解决方案。您可以让服务器定期组播其IP地址,或采用一些sneakernet方法。