使用Spring配置rmi出现的问题。
在本地测试通过后,发布到linux后发现有这样的问题
错误内容是:
org.springframework.remoting.RemoteConnectFailureException: Cannot connect to remote service [rmi://192.168.0.106:1199/HelloService]; nested exception is java.rmi.ConnectException: Connection refused to host: 127.0.0.1;
nested exception is:
java.net.ConnectException: Connection refused
java.rmi.ConnectException: Connection refused to host: 127.0.0.1;
nested exception is:
java.net.ConnectException: Connection refused
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:574)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:185)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:171)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:94)
at org.springframework.remoting.rmi.RmiInvocationWrapper_Stub.invoke(Unknown Source)
at org.springframework.remoting.rmi.RmiClientInterceptor.doInvoke(RmiClientInterceptor.java:347)
at org.springframework.remoting.rmi.RmiClientInterceptor.doInvoke(RmiClientInterceptor.java:294)
at org.springframework.remoting.rmi.RmiClientInterceptor.invoke(RmiClientInterceptor.java:209)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:170)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:176)
多方google后发现网上给出的解释是:
配置RMI启动时的IP
否则spring的rmi 客户端可能无法访问服务端。因为rmi在linux上默认取的localhost是127.0.0.1。
The java.rmi.server.hostname property specifies the host name or address to put in the stubs for remote objects exported in this Java virtual machine. This value is the host name or address used by clients when they attempt to communicate remote method invocations. By default, the RMI implementation uses the server's IP address as indicated by the java.net.InetAddress.getLocalHost API. However, sometimes, this address is not appropriate for all clients and a fully qualified host name would be more effective. To ensure that RMI uses a host name (or IP address) for the server that is routable from all potential clients, set the java.rmi.server.hostname property.
但找了半天,spring配置文档中没有发现sysproperty 这种写法
查看源代码
8088
222.*.*.*
这个是典型的服务器有多个ip引起的rmi连接问题,解决办法非常简单
就是在客户端代码中指定要连接rmi服务器ip。
System.setProperty("java.rmi.server.hostname ", "XX.XX.XX.59");
非常感谢!现在问题已经解决了。System.setProperty这段代码是加在服务器端的。