Akka.Net聚类简单解释

Akka.Net Clustering Simple Explanation

我尝试使用 akka.net 做一个简单的集群。

目标是让服务器接收请求并akka.net通过集群处理它。

为了测试和学习,我创建了一个简单的 WCF 服务,它接收一个数学方程,我想发送这个方程来求解。

我有一个项目服务器和另一个客户端。

服务器端配置为:

<![CDATA[
      akka {
            actor {
              provider = "Akka.Cluster.ClusterActorRefProvider, Akka.Cluster"
              debug {
                receive = on
                autoreceive = on
                lifecycle = on
                event-stream = on
                unhandled = on
              }
              deployment {
                /math {
                  router = consistent-hashing-group #round-robin-pool # routing strategy
                  routees.paths = [ "/user/math" ]
                  virtual-nodes-factor = 8
                  #nr-of-instances = 10 # max number of total routees
                  cluster {
                     enabled = on
                                           max-nr-of-instances-per-node = 2
                                           allow-local-routees = off
                                           use-role = math
                  }
                }
              }
            }
            remote {
                helios.tcp {
                    transport-class = "Akka.Remote.Transport.Helios.HeliosTcpTransport, Akka.Remote"
                                    applied-adapters = []
                                    transport-protocol = tcp
                    port = 8081
                    hostname = "127.0.0.1"
                }
            }
          cluster {
              seed-nodes = ["akka.tcp://ClusterSystem@127.0.0.1:8081"] # address of seed node
             }

          }
  ]]>

客户端的配置是这样的:

 <![CDATA[
      akka {
            actor.provider = "Akka.Cluster.ClusterActorRefProvider, Akka.Cluster"
            remote {
                log-remote-lifecycle-events = DEBUG
                log-received-messages = on

                helios.tcp {
                    transport-class = "Akka.Remote.Transport.Helios.HeliosTcpTransport, Akka.Remote"
                                    applied-adapters = []
                                    transport-protocol = tcp
                    port = 0
                    hostname = 127.0.0.1
                }
            }
            cluster {
              seed-nodes = ["akka.tcp://ClusterSystem@127.0.0.1:8081"] # address of the seed node
              roles = ["math"] # roles this member is in
             }
            actor.deployment {
              /math {
                router = round-robin-pool # routing strategy
                routees.paths = ["/user/math"]
                nr-of-instances = 10 # max number of total routees
                cluster {
                   enabled = on
                   allow-local-routees = on
                   use-role = math
                   max-nr-of-instances-per-node = 10
                }
              }
            }
          }
  ]]>

集群连接似乎正确建立。我看到状态 [UP] 以及与服务器端出现的角色 "math" 的关联。

事件发生在WebCramler 上的示例之后,我没有实现发送消息。我总是收到死信。

我这样试:

actor = sys.ActorOf(Props.Empty.WithRouter(FromConfig.Instance), "math");

var actor = sys.ActorSelection("/user/math");

有人知道好的教程或可以帮助我吗? 谢谢

一些备注:

首先:假设您的发送工作是从服务器到客户端的。然后,您就可以有效地在客户端远程部署参与者。 这意味着只有服务器节点需要 actor.deployment 配置部分。 客户端只需要默认的集群配置(当然还有你的角色设置)。

第二:先尽量简单。请改用 round-robin-pool。它简单得多。尝试让它工作。然后从那里开始工作。 这样更容易消除 configuration/network/other 问题。

您的用法:actor = sys.ActorOf(Props.Empty.WithRouter(FromConfig.Instance), "math");是正确的。

您的 round-robin-pool 配置示例:

deployment {
                /math {
                  router = round-robin-pool # routing strategy
                  nr-of-instances = 10 # max number of total routees
                  cluster {
                     enabled = on
                     max-nr-of-instances-per-node = 2
                     allow-local-routees = off
                     use-role = math
                  }
                }
              }

试试这个。如果有帮助,请告诉我。

编辑:

看了你的样本后确定。我改变了一些东西

  • ActorManager->Process:您为每个请求创建一个新的路由器参与者。不要那样做。创建路由 actor 一次,并重复使用 IActorRef.
  • 摆脱了 MathAgentWorker 项目中的最小集群大小设置
  • 因为您没有使用远程 actor 部署。我将 round-robin-pool 更改为 round-robin-group

之后就成功了。

另请记住,如果您使用 consistent-hashing-group 路由器,则需要指定散列密钥。有多种方法可以做到这一点,在您的示例中,我认为最简单的方法是将您发送到路由器的消息包装在 ConsistentHashableEnvelope 中。查看文档以获取更多信息。

最终 akka 部署部分如下所示:

deployment {
                    /math {
                        router = round-robin-group # routing strategy
                        routees.paths = ["/user/math"]
                        cluster {
                           enabled = on
                           allow-local-routees = off
                           use-role = math
                        }
                     }
                  }

在 MathAgentWorker 上,我只更改了集群部分,现在看起来像这样:

cluster {
                  seed-nodes = ["akka.tcp://ClusterSystem@127.0.0.1:8081"] # address of the seed node
                  roles = ["math"] # roles this member is in
                 }

ActorManager.Process 唯一做的是:

 return await Program.Instance.RouterInstance.Ask<TResult>(msg, TimeSpan.FromSeconds(10));