Spring RMI错误:java.rmi.ConnectException: Connection refused to host: 127.0.0.1

来源:互联网 发布:mac 键位修改 编辑:程序博客网 时间:2024/05/16 00:42

错误现象

在linux上部署RMI服务端程序后,测试无法通过,主要错误如下:

Exception in thread "main" org.springframework.remoting.RemoteConnectFailureException: Could not connect to remote service [rmi://xxx]; nested exception is java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is: java.net.ConnectException: Connection refused: connect

错误原因

简单点说,是因为linux上通过hostname获取IP的机制而导致的问题。通过命令hostname -i 查看IP是多少。我看到的 是127.0.0.1。
也就是说:
“hostname -i”=127.0.0.1,看到的错误提示就是“Connection refused to host: 127.0.0.1”。
“hostname -i”=xxx,看到的错误提示就是“Connection refused to host: xxx”

修复办法

方法1(我认为的最好的办法):
启动程序时,添加参数-Djava.rmi.server.hostname=xxip。举例:

java -Djava.rmi.server.hostname=xxip -jar xx.jar

xxip为客户端能够正确访问的ip地址即可。

方法2:
修改linux系统设置,使hostname -i 返回的IP是客户端能够正确访问的ip地址。
具体方法:

cat /proc/sys/kernel/hostname //查看本机hostname是什么。比如是abcvi /etc/hosts                 //新增一行或修改某一行: xxip  abc
阅读全文
0 0
原创粉丝点击