解决:java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is:
实验环境windows10;idea2010.1.3;scala 2.11.12;报错详情当注册一个服务时,出现如下报错:Exception in thread "main" java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is:java.net.ConnectE...
·
实验环境
windows10;
idea2018.1.3;
scala 2.11.12;
报错详情
当注册一个服务时,出现如下报错:
Exception in thread "main" java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is:
java.net.ConnectException: Connection refused: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:338)
at sun.rmi.registry.RegistryImpl_Stub.rebind(RegistryImpl_Stub.java:147)
at java.rmi.Naming.rebind(Naming.java:177)
at com.atguigu.chapter17.rmi.MyRemoteImpl$.main(MyRemoteImpl.scala:30)
at com.atguigu.chapter17.rmi.MyRemoteImpl.main(MyRemoteImpl.scala)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.<init>(Socket.java:434)
at java.net.Socket.<init>(Socket.java:211)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:148)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
... 7 more
解决方案
Naming.rebind("rmi://127.0.0.1:9992/RemoteHello", service)
使用这种方式出现错误,于是我们可以换种方式:
参考文章-StackOverflow
val registry: Registry = LocateRegistry.createRegistry(9999)
registry.rebind("RemoteHello", service)
这时就可以成功启动了,如下午所示:
项目源码
这边贴上源码,可以自己测试。
- 项目结构图
MyRemote.scala
package com.atguigu.chapter17.rmi
import java.rmi.{Remote, RemoteException}
// 这是一个接口文件,该文件会提供给远程端和本地使用
trait MyRemote extends Remote {
//一个抽象方法
@throws(classOf[RemoteException])
def sayHello(): String //throws RemoteException
}
MyRemoteClient.scala
package com.atguigu.chapter17.rmi
import java.rmi.Naming
/**
* Author: Solarzhou
* data: 2019/12/6 15:18
* desc:
*/
class MyRemoteClient {
def go() = {
val service = Naming.lookup("rmi://127.0.0.1:9999/RemoteHello").asInstanceOf[MyRemote]
val str = service.sayHello()
println("str = " + str)
}
}
object MyRemoteClient {
def main(args: Array[String]): Unit = {
new MyRemoteClient().go()
}
}
MyRemotelmpl.scala
package com.atguigu.chapter17.rmi
import java.rmi.registry.{LocateRegistry, Registry}
import java.rmi.{Naming, RemoteException}
import java.rmi.server.UnicastRemoteObject
/**
* Author: Solarzhou
* data: 2019/12/6 14:54
* desc:
*/
// 这里就实现了MyRemote trait
class MyRemoteImpl extends UnicastRemoteObject with MyRemote {
@throws(classOf[RemoteException])
override def sayHello(): String = {
"Hello World!~"
}
}
object MyRemoteImpl {
def main(args: Array[String]): Unit = {
val service: MyRemote = new MyRemoteImpl()
//准备把服务绑定到9999端口
//第一种方式
val registry: Registry = LocateRegistry.createRegistry(9999)
registry.rebind("RemoteHello", service)
//Naming.rebind("RemoteHello", service)
// 第二种方式
// Naming.rebind("rmi://127.0.0.1:9992/RemoteHello", service)
println("远程服务开启,在127.0.0.1 的 9999端口监听,服务名 RemoteHello")
}
}
更多推荐
所有评论(0)