herzrasen
herzrasen

Reputation: 387

Configure Akka ClusterRouting programmatically

When configuring the Cluster Routing in the config file using:

akka.actor.deployment {
    /jobDispatcher/singleton/workerRouter {
        router = round-robin-pool
        nr-of-instances = 5
        cluster {
            enabled = on
            max-number-of-instances-per-node = 1
            allow-local-routees = on
        }
    }
}

I can lookup a routed worker using:

ActorRef actor = context().actorOf( //
  FromConfig.getInstance().props( //
    Props.create(MyRoutedActor.class)), //
  "workerRouter");

I would prefer configuring the Pool programmatically since I want to hide the details from my user.

However using:

ActorRef actor = context().actorOf(new ClusterRouterPool(new RoundRobinPool(5), //
  new ClusterRouterPoolSettings(100, 1, true, "")) //
    .props(Props.create(MyRoutedActor.class)),
  "workerRouter");

does not route the calls to Routees in the cluster (only the local .

How do I configure the routing correctly?

Upvotes: 0

Views: 819

Answers (1)

gaston
gaston

Reputation: 498

Try to use ClusterRouterPool

Akka doc says [http://doc.akka.io/docs/akka/2.4/scala/cluster-usage.html]:

Pool - router that creates routees as child actors and deploys them on remote nodes. Each router will have its own routee instances. For example, if you start a router on 3 nodes in a 10-node cluster, you will have 30 routees in total if the router is configured to use one instance per node. The routees created by the different routers will not be shared among the routers. One example of a use case for this type of router is a single master that coordinates jobs and delegates the actual work to routees running on other nodes in the cluster.

The example http://doc.akka.io/docs/akka/2.4/java/cluster-usage.html#Router_with_Pool_of_Remote_Deployed_Routees

akka.actor.deployment {
   /statsService/singleton/workerRouter {
       router = consistent-hashing-pool
          cluster {
          enabled = on
          max-nr-of-instances-per-node = 3
          allow-local-routees = on
         use-role = compute
       }
     }
 }

The code to do programmatically (also from the akka docs):

int totalInstances = 100;
int maxInstancesPerNode = 3;
boolean allowLocalRoutees = false;
String useRole = "compute";
ActorRef workerRouter = getContext().actorOf(
   new ClusterRouterPool(new ConsistentHashingPool(0),
      new ClusterRouterPoolSettings(totalInstances, maxInstancesPerNode,
        allowLocalRoutees, useRole)).props(Props
    .create(StatsWorker.class)), "workerRouter3");

I have an akka cluster in scala, and this is my code:

  val workerRouter = context.actorOf(
  ClusterRouterGroup(AdaptiveLoadBalancingGroup(MixMetricsSelector), ClusterRouterGroupSettings( //RoundRobinGroup(Nil)
  totalInstances = 1000, routeesPaths = List("/user/worker"),
  allowLocalRoutees = true, useRole = Some("workerRole"))).props(),
name = "pool")

Upvotes: 1

Related Questions