`

.rmi.ConnectException: Connection refused to host: 127.0.0.1来龙去脉

阅读更多

 

java.rmi.ConnectException: Connection refused to host: 127.0.0.1

 

主要根源是spring实现中,server端使用了主机名,linux在解析主机名时使用了与windows不同的逻辑。

 

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

说法一:在server端返回的绑定对象中采用的是server主机名,

写一个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。我的脚本:

hostname=`hostname`

 

java -cp $CLASSPATH -Djava.rmi.server.codebase=$codebase -

 

Djava.security.policy=$PROJECT_HOME/se_server/conf/se_server.policy -

 

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

inux系统使用/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

 

 

分享到:
评论
1 楼 di1984HIT 2013-11-22  
不错。学习了啊

相关推荐

Global site tag (gtag.js) - Google Analytics