2

我正在学习 akka.net,并且可能会使用它来替换我们传统的消息驱动应用程序的一部分。

基本上我正在尝试将X 个节点加入到一个集群中。它是点对点类型,我可能会在一个节点上运行 X 个演员(同一个演员)。

如果我有 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 {
    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)是要走的路吗?

4

1 回答 1

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"]
        }
    }
于 2015-11-09T04:21:27.853 回答