如何将ClusterRouterPool从C#转换为HOCON配置?

时间:2015-11-06 06:13:41

标签: c# mono akka.net akka.net-cluster

我正在学习akka.net,可能会用它来取代部分传统的消息驱动应用。

基本上我正在尝试将 X个节点连接到群集。它是点对点类型,我可能在节点上运行X个actor(同一个actor)。

如果我有10个作业(比如SendEmailActor),理想情况下,我希望在不同节点上执行10个作业中的每一个(均匀分布负载)。

我有一个非常简单的控制台应用程序来演示。

using System;
using System.Configuration;
using Akka;
using Akka.Actor;
using Akka.Cluster;
using Akka.Cluster.Routing;
using Akka.Configuration;
using Akka.Configuration.Hocon;
using Akka.Routing;

namespace Console1
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            Console.Write("Is this the seed node? (Y/n): ");

            var port = Console.ReadLine().ToLowerInvariant() == "y" ? 9999 : 0;

            var section = (AkkaConfigurationSection)ConfigurationManager.GetSection("akka");

            var config =
                ConfigurationFactory.ParseString("akka.remote.helios.tcp.port=" + port)
                    .WithFallback(section.AkkaConfig);

            var cluster = ActorSystem.Create("MyCluster", config);

            var worker = cluster.ActorOf(Props.Create<Worker>().WithRouter(
                                 new ClusterRouterPool(
                                     new RoundRobinPool(10),
                                     new ClusterRouterPoolSettings(30, true, 5))), "worker");

            while (true)
            {
                Console.Read();
                var i = DateTime.Now.Millisecond;
                Console.WriteLine("Announce: {0}", i);
                worker.Tell(i);

            }
        }
    }

    public class Worker : UntypedActor
    {
        protected override void OnReceive(object message)
        {
            System.Threading.Thread.Sleep(new Random().Next(1000, 2000));
            Console.WriteLine("WORKER ({0}) [{1}:{2}]", message, Context.Self.Path, Cluster.Get(Context.System).SelfUniqueAddress.Address.Port);
        }
    }
}

我的app.config看起来像

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="akka" type="Akka.Configuration.Hocon.AkkaConfigurationSection, Akka" />
    </configSections>
    <akka>
    <hocon>
        <![CDATA[
            akka {
                actor {
                    provider = "Akka.Cluster.ClusterActorRefProvider, Akka.Cluster"
                }

                remote {
                    helios.tcp {
                        hostname = "127.0.0.1"
                        port = 0
                    }
                }            

                cluster {
                    seed-nodes = ["akka.tcp://MyCluster@127.0.0.1:9999"]
                }
            }
        ]]>
    </hocon>
    </akka>
</configuration>

我想使用HOCON并设置akka.actor.deployment,我无法让它工作。我不太了解 routees.paths ,以及它与 actor.deployment / worker 的关系以及如何将routees.paths映射到在C#中创建的actor。

actor {
    provider = "Akka.Cluster.ClusterActorRefProvider, Akka.Cluster"
    deployment {
        /worker {
            router = roundrobin-pool
            routees.paths = ["/worker"] # what's this?
            cluster {
                    enabled = on
                    max-nr-of-instances-per-node = 1
                    allow-local-routees = on
            }
        }
    }
}

另一个问题:使用aka.net.cluster是否可以“镜像”节点以提供冗余?或GuaranteedDeliveryActor(我认为它被重命名为AtLeastOnceDelivery)是要走的路?

1 个答案:

答案 0 :(得分:3)

感谢@RogerAlsing。

C#行看起来像这样

var worker = cluster.ActorOf(Props.Create(() => new Worker()).WithRouter(FromConfig.Instance), "worker");

,配置看起来像

akka {
        actor {
            provider = "Akka.Cluster.ClusterActorRefProvider, Akka.Cluster"
            deployment {
                /worker {
                    router = round-robin-pool #broadcast-pool also works
                    nr-of-instances = 10
                    cluster {
                            enabled = on
                            max-nr-of-instances-per-node = 2
                            allow-local-routees = on
                    }
                }
            }
        }

        remote {
            helios.tcp {
                hostname = "127.0.0.1"
                port = 0
            }
        }            

        cluster {
            seed-nodes = ["akka.tcp://MyCluster@127.0.0.1:9999"]
        }
    }