Java RMI

时间:2017-11-13 14:03:28

标签: java rmi

我正在使用Java RMI分布式系统,我在本地计算机上运行ubuntu有2个VM(ubuntu-(master)IP = 192.168.101.131和ubuntu-(slave)IP = 92.168.101.137)。我可以在两个VM上运行我的bankServer.jar(在tomcat中)。 ubuntu-(主)数据库自动在ubuntu-(slave)上复制。到现在为止还挺好。服务器提到(以葡萄牙语)在默认端口1099中运行。

Cubuntu@ubuntu:/var/www/html/classes$ java -cp ./code/:jar/serverBanco.jar -
Dja.rmi.server.codebase=file:jar/computeInterfaces.jar -
Djava.rmi.server.hostname=192.168.101.131 -
Djava.security.policy=file:jar/all.policy bank.BankServer
Default RMI Port = 1099
Servidor iniciado...

现在,当我在本地计算机上运行客户端时,我的程序首先ping主机以查看他是否在线,如果可以的话,就像这样绑定注册表:

InetAddress inet = null;
     InetAddress inet2 = null;
     String rmi_adress;
        //ping 
        inet = InetAddress.getByAddress(new byte[] { (byte) 192, (byte) 168, 101, (byte) 137 });
        System.out.println("Enviando Ping Request para host: " + inet);
        System.out.println(inet.isReachable(5000) ? "Resposta: Host online" : "Resposta: Host offline");

        inet2 = InetAddress.getByAddress(new byte[] { (byte) 192, (byte) 168, 101, (byte) 131 });
        System.out.println("Enviando Ping Request para host: " + inet2);
        System.out.println(inet2.isReachable(5000) ? "Resposta: Host online" : "Resposta: Host offline");

        try {
            registry = LocateRegistry.getRegistry("rmi://127.0.0.1/BankServer");
            if (inet.isReachable(5000)) {
                rmi_adress = "rmi://192.168.101.137/BankServer";
                registry = LocateRegistry.getRegistry(rmi_adress);      
                System.out.println("RMI Port set to " + rmi_adress);
                bank = (OperationsInterface) registry.lookup(rmi_adress);
            }else {
                rmi_adress =  "rmi://192.168.101.131/BankServer";
                registry = LocateRegistry.getRegistry(rmi_adress);      
                System.out.println("RMI Port set to " + rmi_adress);
                bank = (OperationsInterface) registry.lookup(rmi_adress);
            }

但是在运行客户端时无法识别主机。这是我得到的例外:

Enviando Ping Request para host: /192.168.101.137
Resposta: Host online
Enviando Ping Request para host: /192.168.101.131
Resposta: Host online
RMI Port set to rmi://192.168.101.137/BankServer
Exception in thread "main" java.rmi.UnknownHostException: Unknown host: 
rmi://192.168.101.137/BankServer; nested exception is: 
    Java.net.UnknownHostException: rmi://192.168.101.137/BankServer
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:616)
    at 
sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
    at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
    at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:342)
    at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
    at atm.ATM.main(ATM.java:52)
Caused by: java.net.UnknownHostException: rmi://192.168.101.137/BankServer
    at 
java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at java.net.Socket.connect(Socket.java:538)
    at java.net.Socket.<init>(Socket.java:434)
    at java.net.Socket.<init>(Socket.java:211)
    at... 

现在可能会发生什么?任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:0)

您应该在获得注册表时尝试删除rmi://

答案 1 :(得分:0)

LocateRegistry.getRegistry()的参数是主机名,而不是RMI URL。见Javadoc。