简体   繁体   English

如何设置RMI注册的自定义端口?

[英]How to set a custom port for RMI regisrty?

In my RMI program I want to set up my RMI registry on the port 8080, but when I do it, I get an exception. 在我的RMI程序中,我想在端口8080上设置我的RMI注册表,但是当我这样做时,出现了异常。

Here is my server code 这是我的服务器代码

public class Server {
    public static void main(String[] args) {
        try {
            LocateRegistry.createRegistry(8080);
            MathServerImpl mathServer = new MathServerImpl();
            Naming.rebind("MathServer", mathServer);
            System.out.println("Math Server has started and is running");
        } catch (RemoteException | MalformedURLException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
}

The code works properly for the port 1099, which is it's default port as far as I know, but for this case it gives me a java.net.ConnectException, and here is the log. 代码对于端口1099正常工作,据我所知,它是默认端口,但是在这种情况下,它给了我java.net.ConnectException,这是日志。

java.rmi.ConnectException: Connection refused to host: 10.100.25.173; nested exception is: 
    java.net.ConnectException: Connection refused
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
    at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
    at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
    at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:341)
    at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
    at java.rmi.Naming.rebind(Naming.java:177)
    at server.Server.main(Server.java:16)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at java.net.Socket.connect(Socket.java:528)
    at java.net.Socket.<init>(Socket.java:425)
    at java.net.Socket.<init>(Socket.java:208)
    at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
    at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:147)
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
    ... 11 more

You are creating a registry on port 8080 but then attempting to rebind your object in the default registry on port 1099. You need to either: 您正在端口8080上创建注册表,但随后尝试在端口1099的默认注册表中rebind对象。您需要执行以下任一操作:

  • pass a full URL to Naming.rebind including the port number, ie //localhost:8080/MathServer or 将完整的URL传递给Naming.rebind包括端口号,即//localhost:8080/MathServer
  • save a reference to the Registry object returned by LocateRegistry.createRegistry and bind the object using that registry's instance methods rather than the static methods of Naming 保存对LocateRegistry.createRegistry返回的Registry对象的引用,并使用该注册表的实例方法(而不是静态Naming的静态方法)绑定该对象

Similarly, you will need to use the full //localhost:8080/MathServer URL form in your clients when they lookup the object, to make sure they're talking to the right registry. 同样,在客户端lookup对象时,您将需要在客户端中使用完整的//localhost:8080/MathServer URL表单,以确保它们正在与正确的注册表通信。

Naming.rebind("MathServer", mathServer);

更改为

Naming.rebind("//localhost:8080/MathServer", mathServer);

If you are linux Ubuntu user then while starting RMI registry use command 如果您是Linux Ubuntu用户,则在启动RMI注册表时使用命令

rmiregistry 8080 & rmir​​egistry 8080&

For Windows 对于Windows

start rmiregistry 8080 启动rmiregistry 8080

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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