以编程方式配置 Akka ClusterRouting

Configure Akka ClusterRouting programmatically

在配置文件中配置集群路由时使用:

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
        }
    }
}

我可以使用以下方法查找路由工作人员:

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

我更喜欢以编程方式配置池,因为我想对我的用户隐藏详细信息。

然而使用:

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

不将调用路由到集群中的路由(仅本地 .

如何正确配置路由?

尝试使用ClusterRouterPool

Akka 医生说 [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.

例子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
       }
     }
 }

以编程方式执行的代码(也来自 akka 文档):

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

我在 scala 中有一个 akka 集群,这是我的代码:

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