Java RMI:rmi Connection refused to host: 127.0.0.1异常解决

又是Linux下RMI异常,不过这次是单网卡的情况,先看异常信息: 
Java代码   收藏代码
  1. [appframe] 2012-10-09 15:20:59,217 - com.opensymphony.xwork2.DefaultActionInvocation -com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:383) -2603911 [http-8282-12] DEBUG  - Executing action method = haveDataContentView  
  2. org.springframework.remoting.RemoteConnectFailureException: Could not connect to remote service [rmi://192.168.2.74:7777/ViewService]; nested exception is java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is:   
  3.     java.net.ConnectException: Connection refused  
  4.     at org.springframework.remoting.rmi.RmiClientInterceptorUtils.convertRmiAccessException(RmiClientInterceptorUtils.java:189)  
  5.     at org.springframework.remoting.rmi.RmiClientInterceptor.doInvoke(RmiClientInterceptor.java:347)  
  6.     at org.springframework.remoting.rmi.RmiClientInterceptor.invoke(RmiClientInterceptor.java:259)  
  7.     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)  
  8.     at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)  
  9.     at $Proxy65.viewDictContentHtml(Unknown Source)  
  10.     at com.plat.sap.action.SummaryPropertyDataAction.haveDataContentView(Unknown Source)  


主要信息为:java.rmi.ConnectException: Connection refused to host: 127.0.0.1 

经查,发现Server应用和Client应用都部署在单网卡的Linux上,且Server调Client端的RMI服务没问题,所以料到异常跟系统的域名或IP配置有关。通过Google异常,发现下面这篇文章,谢谢它最终指引我们解决了问题。现收藏如下: 


java.rmi.ConnectException: Connection refused to host: 127.0.0.1异常主要根源是spring实现中,server端使用了主机名,linux在解析主机名时使用了与windows不同的逻辑。 

在使用主机名时有两种说法: 

说法一、 在server端返回的绑定对象中采用的是server主机名(hostname),写一个rmi客户端程序,你可能会收到如标题这样的异常。这个问题其实是由rmi服务器端程序造成的。客户端程序向服务端请求一个对象的时候,返回的stub对象里面包含了服务器的hostname,客户端的后续操作根据这个hostname来连接服务器端。要想知道这个hostname具体是什么值可以在服务器端bash中打入指令: 
    hostname -i 
如果返回的是127.0.0.1,那么你的客户端肯定会抛如标题的异常了。 

解决这个问题有两个方式: 
1) 修改/etc/hosts 
找到127.0.0.1       hostxxxxx这样的字样。把127.0.0.1改成真实的,可供其他机器连接的ip。 
这样客户端就能得到真实的ip了。 
2) 在rmi服务器端程序启动脚本中加上两行,显式指定hostname。我的脚本: 
  
Java代码   收藏代码
  1.  hostname=`hostname`   
  2. java -cp $CLASSPATH -Djava.rmi.server.codebase=$codebase -Djava.security.policy=$PROJECT_HOME/se_server/conf/se_server.policy -  
  3. Djava.rmi.server.hostname=$hostname com.abc.server.StartServer > $PROJECT_HOME/se_server/logs/init.log 2>&1 &  

不过该方式有个局限,其他机器肯定能识别ip,但是可能无法识别hostname。当然,你也可以直接写死这个hostname,比如:-Djava.rmi.server.hostname=xxx.xxx.xxx.xxx。 这样最省力,就是缺乏灵活性。 


说法二、 返回的是根据主机名对应的ip 

Linux系统使用/etc/hosts文件中localhost解析ip为127.0.0.1,当客户端向服务器Lookup时,服务端就会把解析出来的地址发给客户端,让客户端再根据这个地址去连接,客户端收到127.0.0.1这个地址,也使用/etc/hosts文件中localhost解析ip去连接,实际连接的是自己本身,当然也就不行了。 

我把服务器的IP地址加到服务器的/etc/hosts文件中,并放在127.0.0.1之前,以让该服务能先解析到这个IP,从而正确解析出来机器名所对应的IP。 


举例: 
在服务端的 Naming.rebind("SectionWorkerManager", manager );没有指定ip,(这个语句在Windows下没问题)linux系统自己使用localhost解析为IP 127.0.0.1,当客户端向服务器Lookup时,服务端就会把解析出来的地址发给客户端,让客户端再根据这个地址去连接,客户端收到127.0.0.1这个地址去连接,实际连接的是自己本身,当然也就不行了。 

更正办法:把Naming.rebind("SectionWorkerManager", manager); 

改成Naming.rebind("rmi://10.1.5.xxx:1099/SectionWorkerManager", manager);,直接用IP地址(10.1.5.xxx:1099为服务器本身IP),这样就没问题了; 

或者是用机器名,该服务器的名字为RHELTEST,把它加到服务器的hosts文件中,并放在127.0.0.1之前,以让该服务能正确解析出来机器名所对应的IP;要么用域名解析也行,这种方法比较适合大规模场合。 

在Windows下能正常工作,在linux下却不行,这可能是操作系统解析localhost为ip时时的机制不一样引起的。 

在redhat es5中测试,应该使用的是方法2。 

不过两种方式都能解决该问题,采用哪种方式,根据服务器可做的修改来决定。 
spring rmi对此的特别说明: 

Note: RMI makes a best-effort attempt to obtain the fully qualified host name. If one cannot be determined, it will fall back and use the IP address. Depending on your network configuration, in some cases it will resolve the IP to the loopback address. To ensure that RMI will use the host name bound to the correct network interface, you should pass the java.rmi.server.hostname property to the JVM that will export the registry and/or the service using the "-D" JVM argument. For example: -Djava.rmi.server.hostname=myserver.com。 

全文转载完毕。 

以前还整理过一个双网卡下RMI配置的问题,mark一下,有空弄到网上来和大家分享。
  • 8
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值