【问题标题】:can a lookup an akka actor address from configuration?可以从配置中查找 akka 演员地址吗?
【发布时间】:2012-02-25 20:43:52
【问题描述】:

使用 Akka 2.0 时,有没有办法将 ActorRef 获取到远程演员,但我正在查找的演员的地址来自配置而不是以编程方式指定它?例如,我想使用

AkkaSystem("mysystem").actorFor("akka://system@remotehost/user/whatever")

但我希望能够通过更改我的 应用程序.conf。

【问题讨论】:

    标签: akka


    【解决方案1】:

    您可以从ActorSystem 中包含的Config 对象中检索任意信息(或者您可以自己使用ConfigFactory 解析外部源):

    val system = AkkaSystem("mysystem")
    val config = system.settings.config
    val remotePath = config.getString("my-config.serviceA")
    val ref = system.actorFor(remotePath)
    

    连同在上面给出的路径的配置中定义一些字符串。然后,您还可以使用 Config 库的强大功能来拼凑路径(例如,分解出远程节点的地址等):

    my-config {
      remotenode = "akka://sys@remote.node:2134"
      serviceA = ${my-config.remotenode}/service/A
    }
    

    【讨论】:

      【解决方案2】:

      您可以在配置中定义部署路径。
      来自Akka docs

      akka {
        actor {
          deployment {
            /sampleActor {
              remote = "akka.tcp://sampleActorSystem@127.0.0.1:2553"
            }
          }
        }
      }
      
      ActorRef actor = system.actorOf(Props.create(SampleActor.class), "sampleActor");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-09-08
        • 1970-01-01
        • 2012-08-08
        • 1970-01-01
        • 2015-07-27
        • 1970-01-01
        • 1970-01-01
        • 2013-04-04
        相关资源
        最近更新 更多