2012-11-22 54 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的主機和端口。