[java]RMI客户端在服务器双网卡或者多网卡情况下无法连接问题

  • Post author:
  • Post category:java


出错堆栈:

org.springframework.remoting.RemoteConnectFailureException: Cannot connect to remote service [rmi://192.168.2.61/Service]; nested exception is java.rmi.ConnectException: Connection refused to host: 192.168.7.11; nested exception is:

java.net.ConnectException: Connection timed out: connect

很奇怪,rmi访问的url是对的,但是后续的访问得到的IP却是双网卡中的另外一个无法访问的。

在本机做个简单试验:

System.out.println(UnicastRemoteObject.exportObject(new Remote(){},0));

输出:

Proxy[Remote,RemoteObjectInvocationHandler[UnicastRef [liveRef: [endpoint:[192.168.2.3:2339](local),objID:[0]]]]]

说明对象在没有注册出去的时候其IP就已经决定,绑定出去以后客户端就按照这个IP按图索骥。Server端有两个网卡,乱找一个就Over了。

这种方式对分布式对象注册很有用,但是碰到多IP的情况下就比较弱智了。sun的faq上给出一个简单答案:


http://java.sun.com/j2se/1.5.0/docs/guide/rmi/faq.html#netmultihomed

一句话:

System.setProperty(“java.rmi.server.hostname”,”192.168.2.3″);//建议从配置文件加载。