运行简单的远程Akka应用程序 [英] Get Simple Remote Akka Application Running
问题描述
我正在尝试建立一个简单的服务器/客户端akka(使用Akka 2.0.3)应用程序,但是它无法连接。事先这里是基本代码:
I am trying to set up a simple server/client akka (using Akka 2.0.3) application, but it failed to connect. Beforehand here is the basic code:
import com.typesafe.config.ConfigFactory
import akka.actor.Actor
import akka.actor.ActorSystem
import akka.actor.Props
class Server extends Actor {
def receive = {
case s: String => println("Got " + s)
}
}
val serverSystem = ActorSystem("server", ConfigFactory.load(ConfigFactory.parseString("""
akka {
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
remote {
transport = "akka.remote.netty.NettyRemoteTransport"
netty {
hostname = "localhost"
port = 5678
}
}
}
""")))
val server = serverSystem.actorOf(Props[Server], name = "server")
Thread.sleep(500)
println("started")
Thread.sleep(500)
val clientSystem = ActorSystem("client", ConfigFactory.load(ConfigFactory.parseString("""
akka {
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
}
""")))
val remoteServer = clientSystem.actorFor("akka://server@XXX:5678/user/server")
remoteServer ! "HEY"
Thread.sleep(3000)
clientSystem.shutdown
serverSystem.shutdown
我知道配置应该放在外部文件中。
如果将 XXX
替换为 localhost
它可以正常工作:
I know that the configurations should be placed in external files.
If you replace XXX
with localhost
it works:
started
Got HEY
但是,如果我将外部(已解析)IP(位于家庭路由器后面的PC)用于 XXX
HEY
消息永远不会到达。我认为这是由于防火墙问题引起的,并在路由器上转发了相关的TCP和UDP端口,并在Windows防火墙上打开/允许了它们。因此之后,下面的代码起作用了( XXX
也被我的外部IP代替了)。可以通过 ClientTest
连接已启动的 ServerTest
:
But if I used my external (resolved) IP (PC behind home router) for XXX
the HEY
message never arrives. I thought it is due to some firewall problem and forwarded the related TCP and UDP ports at my router and also opened/allowed them at my Windows firewall. So after that the following code worked (also XXX
replaced with my external IP). A started ServerTest
can be connected by a ClientTest
:
import java.net.ServerSocket
object ServerTest extends App {
println("START server")
val ss = new ServerSocket(5678)
val s = ss.accept()
println(s)
println("END")
}
import java.net.Socket
object ClientTest extends App {
println("START client")
val s = new Socket("XXX", 5678)
println(s)
println("END")
}
所以这不是端口/防火墙问题,不是吗?!那么问题出在哪里呢?
So it´s not a port/firewall problem, isn´t it?! So where is the problem???
推荐答案
localhost
通常表示 127.0.0.1
,它只是计算机中可能的许多接口(卡)之一。绑定到localhost的服务器将不接收连接到其他接口(包括具有外部地址的接口)的连接。
localhost
usually means 127.0.0.1
, which is only one of the possibly many interfaces (cards) in a computer. The server binding to localhost won't receive connections connecting to the other interfaces (including the one with the external address).
您应在服务器中指定外部地址,或 0.0.0.0
表示绑定到所有接口。
You should either specify the external address in the server, or 0.0.0.0
which means "bind to all interfaces".
这篇关于运行简单的远程Akka应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!