与Amazon AWS EC2服务器上运行的RMI Server的连接问题

时间:2018-10-04 16:29:08

标签: java amazon-web-services rmi

我是使用AWS云的新手,所以我尝试在Amazon ec2应用程序上部署简单的echo rmi java,我已经打开了所有入站/出站端口,并且 我用命令启动

java EchoRMIServer private_ip

使用腻子ssh客户端。

rmi服务器的主要服务器是:

public static void main(String[] args) {
    // Register the remote object with the RMI registry
    final int REGISTRYPORT = 1100;
    if(args.length!=1){
        System.out.println("EchoRMIServer <registryHost>");
    }
    String registryHost = args[0];
    String serviceName = "EchoService";
    try {
        String completeName = "rmi://" + registryHost + ":" + REGISTRYPORT + "/" + serviceName;
        EchoRMIServer serverRMI = new EchoRMIServer();
        System.out.println(completeName);
        Registry registry=  LocateRegistry.createRegistry(REGISTRYPORT);
        registry.bind(completeName, serverRMI);
        System.out.println("EchoRMIServer bound");
    }
    catch (Exception e) {
        System.err.println("EchoRMIServer exception: ");
        e.printStackTrace(); 
    }
}

当客户在:

//Start the RMI client
public static void main(String[] args)  {

    final int REGISTRYPORT = 1100;
    String registryHost = null;
    String serviceName = "EchoService"; 
    BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
    // Check for hostname argument 
    try {
        if (args.length != 1) { 
            System.out.println("Usage: EchoRMIClient <hostname>");
            System.exit(1); 
        }   
        registryHost = args[0];

        // Construct a name to use to look up the remote object,
        // using the same name used by EchoRMIServer to bind its remote object to the RMI registry
        String completeName = "rmi://" + registryHost + ":" + REGISTRYPORT + "/" + serviceName;
        // Look up the remote object by name in the server host's registry
        System.out.println(completeName);
        Registry registry = LocateRegistry.getRegistry(registryHost,REGISTRYPORT);
        EchoInterface serverRMI = (EchoInterface) registry.lookup(completeName);


        //Interact with user
        String message, echo;
        System.out.print("Message? ");
        message = stdIn.readLine();
        //Call remote method
        echo = serverRMI.getEcho(message);
        System.out.println("Echo from remote method: "+echo+"\n");
    } // end try
    catch (Exception e) {
        System.err.println("EchoRMIClient exception: ");
        e.printStackTrace(); 
    }
} // end main

我使用以下命令在计算机上启动客户端:

 java EchoRMIClient public_ec2_instance_ip

但是我遇到了错误:

 EchoRMIClient exception: 
 java.rmi.NotBoundException: rmi://X.X.X.X:1100/EchoService
at sun.rmi.registry.RegistryImpl.lookup(RegistryImpl.java:227)
...

0 个答案:

没有答案