can a lookup an akka actor address from configurat

2019-05-30 13:48发布

When using Akka 2.0, Is there a way to get an ActorRef to a remote actor but have the address of the actor I am looking up come from configuration instead of specifying it programatically? I want to, for instance use

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

but I want to be able to change remotehost just by changing my application.conf.

标签: akka
2条回答
淡お忘
2楼-- · 2019-05-30 14:31

You can define deployment path in the configuration.
From Akka docs:

akka {
  actor {
    deployment {
      /sampleActor {
        remote = "akka.tcp://sampleActorSystem@127.0.0.1:2553"
      }
    }
  }
}

ActorRef actor = system.actorOf(Props.create(SampleActor.class), "sampleActor");
查看更多
不美不萌又怎样
3楼-- · 2019-05-30 14:41

You can retrieve arbitrary information from the Config object contained in the ActorSystem (or you could parse external sources with ConfigFactory yourself):

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

together with defining some string in the configuration at the path given above. You can then also use the power of the Config library to piece together the path (e.g. factor out the remote node’s address etc.):

my-config {
  remotenode = "akka://sys@remote.node:2134"
  serviceA = ${my-config.remotenode}/service/A
}
查看更多
登录 后发表回答