简体   繁体   English

使用Akka.NET在集群中为每个用户实现一个参与者(子实体模式)

[英]Implement one actor per user (Child-Per-Entity pattern) in a cluster using Akka.NET

I'm doing a little sample to learn a bit about Akka.NET. 我正在做一些示例以了解有关Akka.NET的知识。 I'm trying to implement a Child-Per-Entity pattern (as seen in Akka.NET Design Patterns ), where I have one actor per user (across a cluster). 我正在尝试实现“每个实体的孩子”模式(如Akka.NET设计模式所示 ),其中每个用户(在整个集群中)每个角色都有一个参与者

To do this I was trying to use a ClusterRouterPool with ConsistentHashingPool, that distributed the messages to some parent actors that kept an IDictionary(int,IActorRef) with the child actors indexed by their id (used as the key for the consistent hash). 为此,我尝试使用带有ConsistentHashingPool的ClusterRouterPool,该消息将消息分发给一些父角色,该父角色保留IDictionary(int,IActorRef)并通过其ID索引子角色(用作一致性哈希的键)。

This approach however doesn't seem to be working as I initially expected because: 但是,这种方法似乎并不像我最初预期的那样起作用,因为:

  1. Having different clients creating routers will result in them creating multiple parent actors, which in turn will create repeated child actors. 让不同的客户端创建路由器将导致他们创建多个父角色,进而创建重复的子角色。
  2. Even if I had a single router, if a new node joins the cluster what happens? 即使我只有一个路由器,如果有新节点加入集群,会发生什么? Wouldn't the router re-balance itself causing again the routing being made to new parent actors that would create repeated child actors? 路由器是否会自行重新平衡,导致路由再次路由到新的父角色,从而创建重复的子角色?

There's a somewhat similar question here on StackOverflow that points to using Akka.Cluster.Sharding, but on Akka.NET Design Patterns Aaron has a comment stating he used the consistent hash router approach, hence why I started it in the first place. 在StackOverflow上有一个类似的问题 ,它指向使用Akka.Cluster.Sharding,但是在Akka.NET设计模式上, Aaron发表了一条评论,指出他使用了一致的哈希路由器方法,因此为什么我首先开始使用它。

Thanks 谢谢

I think, that key concept is to understand a difference between Akka.Cluster.Sharding and consistent hash router: 我认为,这个关键概念是要了解Akka.Cluster.Sharding和一致的哈希路由器之间的区别:

  • In Akka.Cluster.Sharding message-actor routing is based on (ShardId, EntityId) identifier. Akka.Cluster.Sharding中,消息执行者路由基于(ShardId,EntityId)标识符。 This is 1-to-1 relationship, which means that messages with different identifiers will always be routed their dedicated actors. 这是一对一的关系,这意味着具有不同标识符的消息将始终被路由到其专用参与者。 Moreover cluster sharding will assure, that messages are send correctly to their actors even when number of nodes in the cluster will change. 此外,集群分片将确保即使集群中的节点数将发生更改,消息也会正确发送给其参与者。
  • In consistent hash router approach important thing to understand is a concept of the key space - it's a space of all possible message identifiers to be received. 一致的哈希路由器方法中,重要的要理解的是密钥空间的概念-它是要接收的所有可能的消息标识符的空间。 This space is then divided by the number of actors behind the router - this means that each actor is not associated with a single identifier, but a range of it. 然后,将这个空间除以路由器后面的actor数量-这意味着每个actor都不与单个标识符关联,而是与它的范围关联。 This is 1-to-many relationship, so that it's possible that two messages with different ids will be routed to the same actor. 这是一对多关系,因此可能会将具有不同ID的两条消息路由到同一参与者。 Moreover as number of nodes in the cluster will change, number of actors behind that router will also change - as result, key range is not fixed for an actor and it may change over time. 此外,随着群集中节点数的变化,该路由器后面的actor的数量也会发生变化-结果,actor的密钥范围不是固定的,并且可能会随时间变化。 This means that message with the same identifier may be routed to a different actor when the cluster size will change. 这意味着,当群集大小更改时,具有相同标识符的消息可能会被路由到其他参与者。

Therefore I think, that cluster sharding is what you're looking for. 因此,我认为您正在寻找集群分片。 Inside examples section of Akka.NET core repository you may find a sample which addresses your case directly. 在Akka.NET核心存储库的“示例”部分中,您可能会找到一个直接解决您的案例的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM