gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:53:58 29 4
gpt4 key购买 nike

我正在学习 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)是可行的方法吗?

最佳答案

感谢@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"]
}
}

关于c# - 如何将 ClusterRouterPool 从 C# 转换为 HOCON 配置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33560574/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com