2013-04-24 63 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方法。