Java RMI 多服务器/单客户端 [英] Java RMI Multiple server/Single client

查看:73
本文介绍了Java RMI 多服务器/单客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我是 Java RMI 的新手,我对 Java RMI 服务器有疑问.现在我有一个带有 1 个服务器和 1 个客户端的有效 RMI 程序.我被告知要创建一个带有 1 个客户端的多服务器.将有 1 个具有负载平衡的主服务器和多个子服务器(我不确定这是否是正确的术语),其中主服务器将分配给子服务器的任务.我不知道这实际上是如何工作的.Java RMI 有可能吗?如果是这样,是否

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. 不同的服务器意味着每个服务器需要有不同的IP?(不同的机器)
  2. 在 1 个 IP 上可能有多个服务器吗?

如果是后者,它是如何工作的?我只是在一台机器上同时运行所有服务器吗?至于第一个,我想我在测试时需要很多笔记本电脑,因为现在在我的客户端程序中,客户端需要放置服务器 IP,因此,客户端需要放置 3 个服务器 IP(假设我有 1 个主要服务器和 2 个子服务器)?下面是我启动服务器的代码(用于 1 个服务器 1 个客户端)

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();
    }
  }
}

我不希望你们提供任何代码,但我需要清楚地了解如何解决这个问题.如果我上面的代码有任何错误,我很抱歉,因为我对 Java RMI 还是很陌生,准确地说是 1 个月.

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.

提前致谢

所以我尝试创建 1 个主服务器(端口 2000)和 3 个子服务器(端口 2001、2002、2003)这是我想出的

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);

}

在 windows 命令上使用 netstat 命令,我可以看到端口 2000 - 2003 正在被监听,并且所有端口都有相同的 PID.我是否实现了我想要做的事情(创建多个服务器)?这是使用 Java RMI 执行此操作的正确方法吗?

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?

推荐答案

不同的服务器意味着每个服务器需要有不同的IP?(不同的机器)

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

没有

1 个 IP 上可能有多个服务器吗?

It is possible have multiple server on 1 IP?

当然.即使在同一个 JVM 中.只需导出更多远程对象.

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.

这篇关于Java RMI 多服务器/单客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆