为什么java rmi 一直连接到127.0.1.1。ip什么时候是192.168.XX?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23478649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does java rmi keep connecting to 127.0.1.1. When ip is 192.168.X.X?
提问by user264230
I have a java rmi application i simply do:
我有一个 java rmi 应用程序,我只是这样做:
Client:
客户:
Registry registry = LocateRegistry.getRegistry("localhost");
costApi = (CostApi) registry.lookup("server.CostApi");
Everything works fine when I host the server at localhost. When I start the same program at another machine withing the local network, at 192.168.x.x and change to:
当我在本地主机上托管服务器时,一切正常。当我在具有本地网络的另一台机器上启动相同的程序时,在 192.168.xx 并更改为:
Client:
客户:
Registry registry = LocateRegistry.getRegistry("192.168.x.x");
costApi = (CostApi) registry.lookup("server.CostApi");
it does not work anymore and it fails with a very strange error:
它不再工作,并且失败并出现一个非常奇怪的错误:
java.rmi.ConnectException: Connection refused to host: 127.0.1.1; 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.invoke(UnicastRef.java:129)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:194)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:148)
at com.sun.proxy.$Proxy0.dataCost(Unknown Source)
at billing.data.DataBiller.performBilling(DataBiller.java:57)
at billing.data.DataBiller.consumeMessage(DataBiller.java:46)
at general.templates.RabbitWorker.run(RabbitWorker.java:124)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
I'm not even trying to connect to 127.0.1.1 but to 192.168.x.x, how do I solve this?I prefer to use java code only and not modify my machine with config files. I'm using linux
我什至不想连接到 127.0.1.1 而是连接到 192.168.xx,我该如何解决这个问题?我更喜欢只使用 java 代码,而不是用配置文件修改我的机器。我正在使用 linux
回答by user207421
This is usually caused by a misconfiguration. Check your /etc/hosts
file to ensure that:
这通常是由配置错误引起的。检查您的/etc/hosts
文件以确保:
- localhost maps to 127.0.0.1
- your real hostname maps to your real host address
- 本地主机映射到 127.0.0.1
- 您的真实主机名映射到您的真实主机地址
Some Linux distributions are known to have this back to front.
众所周知,某些 Linux 发行版具有此功能。
If the problem persists, try setting java.rmi.server.hostname
at the server to the IP address the client should use when executing remote method calls. It needs to be set before you export any remote objects, including the Registry.
如果问题仍然存在,请尝试java.rmi.server.hostname
在服务器上设置客户端在执行远程方法调用时应使用的 IP 地址。它需要在导出任何远程对象(包括注册表)之前进行设置。
The problem is caused by the IP address embedded in the stub, which ultimately comes from something like InetAddress.getLocalAddress(),
which is fallible as above. It is overridden by java.rmi.server.hostname.
问题是由嵌入在存根中的 IP 地址引起的,它最终来自于类似InetAddress.getLocalAddress(),
上面那样容易出错的东西。它被覆盖java.rmi.server.hostname.
This is item A.1 in the FMI FAQ, but note that the item is mistitled. It doesn't happen during lookup()
, it happens when you call a remote method on the resulting stub.
这是FMI 常见问题解答中的 A.1 项,但请注意该项的标题有误。它不会在 期间发生lookup()
,而是在您对生成的存根调用远程方法时发生。
回答by user3623873
I just ran into the same issue. I'm doing something very similar to what you are doing. What I noticed was that the first time I ran the client program, and it failed ( by design of the firewall test ) - that it failed with an error message showing the actual ip address of the host that I originally specified ( the 192.168.x.x address ), but all subsequent failures show that it is failing to make a connection to 127.0.0.1. Currently I'm suspecting some kind of caching on the client - has the JVM marked that ip address as never accessible again and it's refusing to ever try to connect to it again?
我刚刚遇到了同样的问题。我正在做与您正在做的非常相似的事情。我注意到的是,我第一次运行客户端程序时,它失败了(由于防火墙测试的设计) - 它失败并显示错误消息,显示我最初指定的主机的实际 IP 地址( 192.168.xx address ),但所有后续失败都表明它无法连接到 127.0.0.1。目前我怀疑客户端上存在某种缓存 - JVM 是否将该 IP 地址标记为永远无法再次访问并且拒绝再次尝试连接到它?
UPDATE: In my case, the JVM on the RMI Server side was not able to properly set the java.rmi.server.hostname property at JVM startup. This property was being left as null. When clients connect to a specific RMI Registry and ask for a stub to a particular named object, they receive a stub containing the ip address of the remote machine where the actual object can be found. The RMI server copies the contents of the java.rmi.server.hostname property into the stubs it returns to clients, so if the java.rmi.server.hostname property is "" and it copies that to each stub it creates, each stub contains a reference to remote server with an IP address of "". By default the client jvm reacts to this by attempting to connect to the server object on the localhost, 127.0.0.1. To work around the problem, try this line of code before exporting any remote objects on the server side:
更新:就我而言,RMI 服务器端的 JVM 无法在 JVM 启动时正确设置 java.rmi.server.hostname 属性。此属性被保留为空。当客户端连接到特定的 RMI Registry 并请求一个特定命名对象的存根时,它们会收到一个存根,其中包含可以找到实际对象的远程机器的 IP 地址。RMI 服务器将 java.rmi.server.hostname 属性的内容复制到它返回给客户端的存根中,所以如果 java.rmi.server.hostname 属性是 "" 并且它将它复制到它创建的每个存根中,每个存根包含对 IP 地址为“”的远程服务器的引用。默认情况下,客户端 jvm 通过尝试连接到本地主机 127.0.0.1 上的服务器对象来对此做出反应。为了解决这个问题,
System.setProperty( "java.rmi.server.hostname", "192.168.RMIServer.IP" ) ;
This property will be automatically copied to all remote stubs exported on that server, and clients who receive that stub should then be able to reach the remote server ( assuming any firewalls are configured correctly ).
此属性将自动复制到该服务器上导出的所有远程存根,然后接收该存根的客户端应该能够访问远程服务器(假设所有防火墙配置正确)。