简体   繁体   中英

High level method for java RMI client communication to multiple servers

I'm trying to get a client to communicate with multiple server processes, but so far, I can't find anything on how a client can discover and discriminate between multiple server processes. Any high level explanation for how this works as well as where I can go to learn more would be appreciated.

Currently my approach is to find a single server and interact with it as follows:

ServerInterface server;
try {
Server = (ServerInterface) Naming.lookup(String.format("//%s:%d/Server", hostName, serverPort));
}
catch (Exception e) {
System.out.println("Unable to lookup server");
return;
}
server.operation();

It is not obvious to me how I can use this to find multiple servers and choose one to perform operations on.

You can check what services are available using Naming.list(name) on a particular host/port combination. Say that I have started 4 RMI servers named myname1..4 registering myname1/2 for port 1099 and myname3/4 on port 1100, then the following queries would return these results:

Naming.list("//:1099")
==> String[2] { "//:1099/myname1", "//:1099/myname2" }
Naming.list("//:1100")
==> String[2] { "//:1100/myname3", "//:1100/myname4" }
Naming.list("//MY_MACHINE:1100")
==> String[2] { "//MY_MACHINE:1100/myname3", "//MY_MACHINE:1100/myname4" }

You are in control of which names to use and which machines they run on so you could devise a simple strategy for naming which means you can search for the most suitable server, and implement your own server lookup layer so that all your clients connect in the same manner.

Note that this strategy does not take account of the current server loads, and there are other strategies you might consider when deciding for how you resolve the best server to use. Say you have 2 data centres (could be different countries.) and pairs of machines in each (DCA1/DCA2/DCB1/DCB2), A client running on DCA1 ought to favour lookup on same host, before trying any in same data-centre. then from other data-centres.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM