如何将actor部署到单独的进程/机器并使用ConsistentHashingGroup路由器?

时间:2017-03-22 12:23:45

标签: akka.net akka-cluster

如何将actor部署到单独的进程/机器并使用ConsistentHashingGroup路由器?

我的策略是通过动态构建包含系统节点地址的配置文件来利用 ConsistentHashingGroup 路由器。这些节点并非都在同一个进程上,但也可以在不同的进程上运行。

下面的配置文件示例依赖于静态地址。但是,我认为我需要动态生成配置文件,因为在编译时不知道其他节点/机器上的actor路径。

     let config = ConfigurationFactory.ParseString(@"
         routees.paths = [
             ""akka://ClusterSystem/user/Worker1"" #testing full path
             ""akka://ClusterSystem/user/Worker2""
             ""akka://ClusterSystem/user/Worker3""
             user/Worker4
         ]")

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

你知道。在现实世界中。演员不会神奇地出现。您可以将它们部署为池路由器的一部分,例如,或其他一些机制。或者你明确地开始它们。

如果您从特定节点运行哈希组。你知道它在设计时从哪里开始。您还可以让演员利用pub / sub将自己的地址发布到一个众所周知的主题。另一个演员订阅并在你的哈希组中注册它们。

通过这种方式,您可以在集群内定义一种协议/会话,演员可以使用它来使自己成为特定哈希组的一部分。

这有意义吗?

答案 1 :(得分:0)

我首先必须引用我想在配置中使用的远程actor名称:

var config = ConfigurationFactory.ParseString(@"
akka {  
    log-config-on-start = on
    stdout-loglevel = DEBUG
    loglevel = DEBUG
    actor {
        provider = ""Akka.Remote.RemoteActorRefProvider, Akka.Remote""

        debug {  
          receive = on 
          autoreceive = on
          lifecycle = on
          event-stream = on
          unhandled = on
        }

        deployment {
            /localactor {
                router = consistent-hashing-pool
                nr-of-instances = 5
                virtual-nodes-factor = 10
            }
            /remoteactor1 {
                router = consistent-hashing-pool
                nr-of-instances = 5
                remote = ""akka.tcp://system2@localhost:8080""
            }
            /remoteactor2 {
                router = consistent-hashing-pool
                nr-of-instances = 5
                remote = ""akka.tcp://system2@localhost:8080""
            }
            /remoteactor3 {
                router = consistent-hashing-pool
                nr-of-instances = 5
                remote = ""akka.tcp://system2@localhost:8080""
            }
        }
    }
    remote {
        dot-netty.tcp {
            port = 8090
            hostname = localhost
        }
    }
}
");

然后我远程部署了演员:

  //create a remote deployed actor
  var remote1 = system.ActorOf(Props.Create(() => new SomeActor(null, 123)).WithRouter(FromConfig.Instance), "remoteactor1");
  var remote2 = system.ActorOf(Props.Create(() => new SomeActor(null, 456)).WithRouter(FromConfig.Instance), "remoteactor2");
  var remote3 = system.ActorOf(Props.Create(() => new SomeActor(null, 789)).WithRouter(FromConfig.Instance), "remoteactor3");

然后我必须将最近部署的actor添加到ConsistentHashingGroup实例:

   var hashGroup = system.ActorOf(Props.Empty.WithRouter(new ConsistentHashingGroup(config)));

   Task.Delay(500).Wait();

   var routee1 = Routee.FromActorRef(remote1);
   hashGroup.Tell(new AddRoutee(routee1));

   var routee2 = Routee.FromActorRef(remote2);
   hashGroup.Tell(new AddRoutee(routee2));

   var routee3 = Routee.FromActorRef(remote3);
   hashGroup.Tell(new AddRoutee(routee3));

然后我可以使用路由器实例发送消息:

   for (var i = 0; i < 5; i++)
   {
       for (var j = 0; j < 7; j++)
       {
           var message = new SomeMessage(j, $"remote message: {j}");
           hashGroup.Tell(message, someSender);
       }
   }