简体   繁体   English

为什么RMI localhost客户端使用no localhost ip连接RMI localhost服务器

[英]Why RMI localhost client uses a no localhost ip to connect the RMI localhost server

We are using a client/server RMI communication in the same computer (so all ip should be localhost). 我们在同一台计算机上使用客户端/服务器RMI通信(因此所有ip都应该是localhost)。

We start the registry (using the default port 1099) 我们启动注册表(使用默认端口1099)

registry = LocateRegistry.createRegistry(port);

and export some objects to the RMI registry 并将一些对象导出到RMI注册表

Naming.rebind("//" + "localhost" + ":" + port + "/" + name, object);

From another process we retrieve some objects (remember everything runs in localhost) 从另一个进程我们检索一些对象(记住一切都在localhost中运行)

MyRemoteObject ro = (MyRemoteObject) Naming.lookup("//" + "localhost" + ":" + port + "/" + name);

The issue happen when you run the app starting with local area network working and in the middle of the process you disable the network connection. 当您从局域网工作开始运行应用程序时,会发生此问题,并且在此过程中您禁用网络连接。 If you run the app and the LAN is working no issue popup, and if you run the app and the LAN is not working no issue popup. 如果您运行应用程序并且局域网正在运行没有问题弹出窗口,并且如果您运行应用程序并且LAN不工作没有问题弹出窗口。 It just happens when you change the LAN while you are running the app. 当您在运行应用程序时更改LAN时,就会发生这种情况。

The exception thrown while executing Naming.lookup() method is this one: 执行Naming.lookup()方法时抛出的异常是这样的:

java.lang.RuntimeException: java.rmi.ConnectIOException: Exception creating connection to: 192.168.xx; nested exception is: java.net.NoRouteToHostException: No route to host: connect

Debugging a little bit I found out that the 调试了一下我发现了

RemoteObject ($Proxy0) -> RemoteObjectInvocationHandler -> UnicastRef2 -> LiveRef -> TCPEndpoint RemoteObject($ Proxy0) - > RemoteObjectInvocationHandler - > UnicastRef2 - > LiveRef - > TCPEndpoint

had the ip of the host (eg: 192.168.xx) instead of "localhost" or 127.0.0.1 (what it would be what I wanted). 有主机的IP(例如:192.168.xx)而不是“localhost”或127.0.0.1(它将是我想要的)。 And the isLocal boolean of liveRef object is always false. 而且,liveRef对象的isLocal布尔值始终为false。

I don't know if it is clear enough. 我不知道它是否足够清楚。 Sorry!!! 抱歉!!!

Do you have any suggestions? 你有什么建议吗?

My Tries: 我的尝试:

I tried this solutions 我试过这个解决方案

  1. Run the jvm with -Djava.rmi.server.hostname=localhost argument 使用-Djava.rmi.server.hostname = localhost参数运行jvm
  2. Redefine RMIServerSocketFactory to return 127.0.0.1 everytime. 每次重新定义RMIServerSocketFactory以返回127.0.0.1。 (TCPEndpoint has 192.168.xx ip and isLocal is always false) (TCPEndpoint的IP为192.168.xx,isLocal始终为false)
  3. Call rebind and lookup with no host in the URI. 在URI中调用没有主机的重新绑定和查找。 This is supposed to mean localhost. 这应该是localhost。

but none of these have worked. 但这些都没有奏效。

Any suggestion will be welcome. 任何建议都将受到欢迎。

java.rmi.server.hostname should do the trick . java.rmi.server.hostname 应该可以解决这个问题 You did set it on server, right? 你确实在服务器上设置了它,对吗?

If java.rmi.server.hostname=localhost does not work, how about java.rmi.server.hostname=127.0.0.1 or java.rmi.server.hostname=::1 ? 如果java.rmi.server.hostname=localhost不起作用,那么java.rmi.server.hostname=127.0.0.1java.rmi.server.hostname=::1怎么样?

You could try to start your server with this Property set -Djava.rmi.server.hostname=localhost 您可以尝试使用此属性集-Djava.rmi.server.hostname=localhost启动服务器

See also Section 3.5.4 另见第3.5.4节

This because the server binds to an interface, either 127.0.0.1 or 192.168.xx 这是因为服务器绑定到接口127.0.0.1或192.168.xx

Sorry, guys. 对不起大家。 My Mistake. 我的错。

As you point out I was setting the "java.rmi.server.hostname=localhost" argument to the clients processes. 正如您所指出的那样,我正在为客户端进程设置“java.rmi.server.hostname = localhost”参数。 It runned like charm when I setted the argument to process which run RMI server. 当我将参数设置为运行RMI服务器的进程时,它就像魅力一样运行。

Thanks a lot. 非常感谢。

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

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