can a lookup an akka actor address from configurat

2019-05-30 14:14发布

问题:

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.

回答1:

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
}


回答2:

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");


标签: akka