2012-11-22 53 views
2

我想构建一个在静态IP地址和端口上具有公共服务器的参与者系统。将有许多客户知道服务器的地址。服务器不知道客户端的IP地址。不知道远程主机地址的远程演员

配置的服务器:

akka { 
    actor { 
    provider = "akka.remote.RemoteActorRefProvider" 
    } 
    remote { 
    transport = "akka.remote.netty.NettyRemoteTransport" 
    netty { 
     hostname = "46.38.232.161" 
     port = 2552 
    } 
    } 
} 

客户端的配置:

akka { 
    actor { 
    provider = "akka.remote.RemoteActorRefProvider" 
    } 
    remote { 
    transport = "akka.remote.netty.NettyRemoteTransport" 
    netty { 
     port = 2553 
    } 
    } 
} 

客户端可能来自整个互联网。现在我想从客户端的角色发送消息给服务器上的角色。服务器如何知道,在哪里发回他的消息?当我发送ActorPath到服务器,所以他会知道相应的客户端的地址,这些不包含客户端的IP地址。

+0

不自动运行?位置透明度和一切... – agilesteel

回答

1

akka.remote.netty.hostname属性可能在您的application.conf中设置为可访问的IP地址或可解析名称,以防您希望通过网络进行通信。实际上,使用Akka时,您的“客户”节点也将成为服务器。

的情况下,如果地址是在应用程序启动的未知,从阿卡文档考虑下面的代码段:

import akka.actor.ActorSystem 
import com.typesafe.config.ConfigFactory 

val customConf = ConfigFactory.parseString(""" 
    akka.actor.deployment { 
    /my-service { 
     router = round-robin 
     nr-of-instances = 3 
    } 
    } 
""") 

// ConfigFactory.load sandwiches customConfig between default reference 
// config and default overrides, and then resolves it. 
val system = ActorSystem("MySystem", ConfigFactory.load(customConf)) 
+0

这是问题所在。我无法在我的'application.conf'中显式设置'akka.remote.netty.hostname',因为客户端地址在部署之前是未知的。 – pvorb

+0

我编辑了答案 - 您可以编程方式创建ActorSystem – idonnie

1

Actor有一个方法,称为sender,可以在演员的receive方法中调用以获取发送当前消息的参与者(ActorRef)的引用。 ActorRef有一个ActorPath它有一个RootActorPath它有一个Address其中包含演员居住的ActorSystem的主机和端口。