如何将单个对象与多个 RMI 服务器绑定



我是RMI技术的新手,面临以下问题。

我们有多个相同类型的设备连接到本地系统,其中每个设备上的RMI服务在不同的端口上运行。

当我们尝试通过RMI将单个设备连接到本地系统时,它工作正常。当我们尝试将第二个设备连接到本地系统时,我们收到如下错误 -

您能帮助我们解决以下问题吗?

提前谢谢。


    java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is: 
        java.net.ConnectException: Connection refused: connect
        at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:601)
        at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:198)
        at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:184)
        at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:322)
        at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
        at java.rmi.Naming.rebind(Naming.java:160)
        at com.rmi.server.RMIServer.exportAndBindObject(Unknown Source)

演示.java


    this.myRMIServer = new RMIServer(this.RMIServerPort,this.RMIClientPort, new RMISocketFactory());
    this.helloWorld = new HelloWorld();
    this.myRMIServer.exportObject(this.helloWorld);
    this.myRMIServer.exportAndBindObject(this.rmiServiceName, this.helloWorld);

RMIServer.java


    public RMIServer(int port, int rmiPort, java.rmi.server.RMISocketFactory sf)
            throws RemoteException {
        this.sf = sf;
        this.rmiPort = rmiPort;
        this.regPort = port;
        synchronized (this) {
            if (registry == null)
                registry = LocateRegistry.createRegistry(port);
        }
    }
    public void exportAndBindObject(String name, RemoteObject ro)
            throws RemoteException, MalformedURLException {
        exportObject(ro);
        String url = "//127.0.0.1:" + this.regPort + "/" + name;
        Naming.rebind(url, ro);
    }

您正在port上创建注册表,但在找不到它的regPort上绑定到注册表。

我不知道this.RMIClientPort的目的是什么。我会摆脱它。RMI 服务器不考虑客户端端口。

此外,您还将导出两次HelloWorld对象:一次在 Demo 中.java在调用 exportObject() 的地方,一次在RMIServer.exportAndBindObject()再次调用 exportObject()。因此,其中一个操作一定失败了,否则它没有导出任何内容。所以你的exportObject()方法有问题。

最新更新