Java RMI Multiple server/Single client

隐身守侯 提交于 2019-12-12 05:51:47

问题


Hello I am new to Java RMI and I have question regarding Java RMI Server. Now I have a working RMI program with 1 server and 1 client. I am told to create a multiple server with 1 client. There will be 1 main server with load balancing and multiple sub server(I am not sure if this is the correct term for it) where the main server will divide task to be given to the sub server. I don't know how actually this work. Is it possible with Java RMI? if so, does

  1. Different server mean each server need to have different IP? (different machine)
  2. It is possible have multiple server on 1 IP?

If the latter, how does it work? Do I just run all server simultaneously in 1 machine? As for the first one, I guess I will need lots of laptop beside me when doing testing and since now in my client program, client will need to put server IP, therefore, client need to put 3 server IP (assuming I have 1 main server and 2 sub server)? Below is my code for starting my server (for 1 server 1 client)

public class pnServer {
JFrame f = new JFrame("Server UI");
JPanel p = new JPanel();
JTextField tf;
static java.rmi.registry.Registry reg;
GridLayout layout = new GridLayout(0,1);
public pnServer() {
    p.setLayout(layout);
    f.getContentPane().add(p);
    f.setSize(200, 200);
    tf = new JTextField("Server is now ready");
    p.add(tf);
    f.pack();
    f.setDefaultCloseOperation(3);
    f.setVisible(true);
}
public static void main (String args[]) {
    pnServer gui = new pnServer();
    try {
        //System.setSecurityManager(new RMISecurityManager());
        p obj = new p();
        reg = java.rmi.registry.LocateRegistry.createRegistry(1099);
        /*pnInterface stub = (pnInterface) UnicastRemoteObject.exportObject(obj, 0);
        Registry reg = LocateRegistry.getRegistry();
        reg.bind("PrimeNum", stub);*/
        Naming.rebind("rmi://localhost/ROG", obj);
        System.out.println("Server is up!");
    } catch (Exception e) {
        System.err.println("Server exception: " + e.toString());
        e.printStackTrace();
    }
  }
}

I don't expect any code from you guys but I need to have clear view on how to solve this problem. I am sorry if my code above is wrong in any way as I am still fairly new to Java RMI, 1 months to be exact.

Thank you in advance

EDIT: So I have tried creating 1 master server (port 2000) and 3 sub server (port 2001, 2002, 2003) this is what i came up with

public static void main (String args[]) throws RemoteException {
pnServer gui = new pnServer();
pnInterface stub;
Registry reg;
int port = 2001;
String serverName = "";
p obj = new p();

/* Starting master server */
UnicastRemoteObject.unexportObject(obj, true);
stub = (pnInterface) UnicastRemoteObject.exportObject(obj, 2000);
reg = LocateRegistry.createRegistry(2000);
try {
    reg.bind("Server2000", stub);
}catch (AlreadyBoundException ae) {
    reg.rebind("Server2000", stub);
}
System.out.println("Master server is up!\n");

/* Establish connection to sub server with port 2001, 2002, 2003 */
serverConnection = "Connecting to sub server. Please wait...";
for (int i = 0; i<3 ; i++) {
    try {
        serverName = "Server" + port;
        UnicastRemoteObject.unexportObject(obj, true);
        stub = (pnInterface) UnicastRemoteObject.exportObject(obj, port);
        reg = LocateRegistry.createRegistry(port);
        try {
            reg.bind(serverName, stub);
        }catch (AlreadyBoundException ae) {
            reg.rebind(serverName, stub);
        }
        serverConnection = serverConnection + "\nSuccessfully listening to port: " + port; 
    }catch (RemoteException ex) {
        ex.printStackTrace();
        serverConnection = serverConnection + "\nFail to listen to port: " + port;
    }
    port += 1;
}
System.out.println(serverConnection);

}

Using netstat command on windows command, I can see port 2000 - 2003 is being listened and all port have the same PID. Do I achieve what I am trying to do (creating multiple server)? is this the correct approach to do this with Java RMI?


回答1:


Different server mean each server need to have different IP? (different machine)

No.

It is possible have multiple server on 1 IP?

Certainly. Even within the same JVM. Just export more remote objects.

However I fail to see the actual point. If there is only one server host, there is no load balancing going on at all. Al the load stays in the same host.



来源:https://stackoverflow.com/questions/47398129/java-rmi-multiple-server-single-client

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!