Akka.NET群集客户端

时间:2018-12-04 19:01:24

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

我尝试使用Akka.NET创建应用程序。

主要目标是使服务器可以同时处理许多客户端连接和请求。我为此选择了Akka.NET。我有一个群集,该群集现在仅包含1个节点。

我也有同时开始连接到服务器的群集客户端(ClusterClient)。客户端的逻辑很简单:它连接到服务器并在其中订阅角色。发布仍然没有问题,当然,只要连接稳定,客户就能获得一切。在〜4000-5000个客户端连接附近的某个地方,重新连接开始,并且连接因此丢失。我尝试将第二个节点添加到群集,并为每个节点建立3000个连接,但这没有成功。

问题是如何在AKKA.Net上建立服务器,该服务器将拥有大量连接(例如100 000-1 000 000)。我可以为此目的使用群集吗?

服务器

using Akka.Actor;
using Akka.Configuration;
using System;
using System.Configuration;
using Akka.Cluster.Tools.Client;
using System.Threading;

namespace CoreSPServer
{
    class Program
    {
        static void Main(string[] args)
        {
            Config config = ConfigurationFactory.ParseString(ConfigurationManager.AppSettings["ClusterConfig"]);
            ActorSystem system = ActorSystem.Create("ClusterSystem", config);
            var publisher = system.ActorOf(Props.Create(() => new Publisher()), "Publisher");
            var clientSub = system.ActorOf(Props.Create(() => new ClientSubscriber()), "Sub");

            ClusterClientReceptionist.Get(system).RegisterService(clientSub);

            if (Console.ReadLine() == "start")
            {
                    publisher.Tell("test");
                    Thread.Sleep(10);
            }

            Console.ReadKey();
        }
    }
}

ClientSubscriber和发布者

class ClientSubscriber : ReceiveActor
{
    public ClientSubscriber()
    {
        var mediator = DistributedPubSub.Get(Context.System).Mediator;

        Receive<IActorRef>(senderToSub =>
        {
            mediator.Tell(new Subscribe("content", senderToSub));
        });
    }
}

public class Publisher : ReceiveActor
    {
        public Publisher()
        {
            var mediator = DistributedPubSub.Get(Context.System).Mediator;

            Receive<string>(str =>
            {
                var upperCase = str.ToUpper();
                mediator.Tell(new Publish("content", upperCase));
            });
        }
    }

客户

static void Main(string[] args)
        {
            var config = ConfigurationFactory.ParseString(ConfigurationManager.AppSettings["ClientConf"]);
            ActorSystem Sys = ActorSystem.Create("ClusterClient", config);

            //Connection path to Cluster Node
            var initialContacts = new List<ActorPath>(){
                    ActorPath.Parse("akka.tcp://ClusterSystem@localhost:5001/system/receptionist"),
            }.ToImmutableHashSet();
            var settings = ClusterClientSettings.Create(Sys).WithInitialContacts(initialContacts);

            for(int i = 0; i < 5000; i++)
            {
                    IActorRef c = Sys.ActorOf(ClusterClient.Props(settings), "client" + Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));
                    var asd = Sys.ActorOf(Props.Create<Subscriber>(), "clientSub" + Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));
                    c.Tell(new ClusterClient.Send("/user/Sub5001", asd));
                    Thread.Sleep(1/10);
            }

            Console.ReadKey();
        }

服务器配置

akka {
extensions = [&quot;Akka.Cluster.Tools.Client.ClusterClientReceptionistExtensionProvider, Akka.Cluster.Tools&quot;]
actor.provider = cluster
remote {
    dot-netty.tcp {
        port = 5001
        public-hostname = localhost
        auto-down-unreachable-after = off
    }
}
cluster {
   seed-nodes = [&quot;akka.tcp://ClusterSystem@localhost:5001&quot;]
}
}

0 个答案:

没有答案