简体   繁体   English

如何在 Akka 集群中动态创建一个actor

[英]How to create an actor in Akka cluster dynamically

如何使用集群客户端在akka集群中动态创建一个演员,这将通过akka远程处理从集群外部传递消息。

So you don't provide a lot of details about what you using (Typed vs Untyped, JVM or .NET).所以你没有提供很多关于你使用什么的细节(Typed vs Untyped,JVM 或 .NET)。 But, regardless, the short answer is just "send a message to the ShardRegion ".但是,无论如何,简短的回答只是“向ShardRegion发送消息”。 The whole gist of sharding is that the actor is dynamically created automatically and transparently whenever a message is received.分片的整个要点是,只要收到消息,actor 就会自动且透明地动态创建。

eg If you have a Customer actor, and you send a "get the account balance of account 1001" the ShardRegion will automatically extract the customer number from the message , figure out which shard that actor belongs in, and route the message to the node that owns that shard.例如,如果您有一个客户参与者,并且您发送“获取账户 1001 的账户余额”,ShardRegion 将自动从消息中提取客户编号,确定该参与者属于哪个分片,并将消息路由到该节点拥有那个碎片。 The ShardRegion on that node will then automatically create the Actor (if it doesn't already exist) and then forward the message to the actor.然后,该节点上的 ShardRegion 将自动创建 Actor(如果它不存在),然后将消息转发给 Actor。

So, the even shorter answer is "it just happens automatically as needed".因此,更简短的答案是“它只是根据需要自动发生”。 But it sounds like you might want to read through all of the sharding documentation just so you understand what is going on.但听起来您可能想通读所有分片文档,以便了解发生了什么。

EDIT (Responding to a comments):编辑(回应评论):

I was trying to reply to the following comment.我试图回复以下评论。 But it was too long to respond in a comment so I'm adding this section.但是在评论中回复太长了,所以我添加了这一部分。

Can we spawn actors by user defined actor when we pass some message.But can it be done distributed across other nodes?means if i create 10 actors in 5 nodes will it create 2 actor in each node?当我们传递一些消息时,我们可以通过用户定义的演员生成演员吗?但是它可以分布在其他节点上吗?意味着如果我在 5 个节点中创建 10 个演员,它会在每个节点中创建 2 个演员吗? In sharding it can be done by sharding each actor在分片中,可以通过对每个参与者进行分片来完成

Part of the challenge of answering this question is that there is some ambiguity in what you mean by "create an actor".回答这个问题的部分挑战在于“创建演员”的含义有些模糊。

Fundamentally the only direct way that an actor is created is an actor spawning a child.从根本上说,创建演员的唯一直接方式是演员产生孩子。 There is a root level actor associated with the actor system.有一个与参与者系统相关联的根级别参与者。 That can spawn children.那可以生孩子。 And then the children can spawn children, and so forth.然后孩子可以产生孩子,依此类推。 All of those children and sub children are, by definition, on the same node.根据定义,所有这些孩子和子孩子都在同一个节点上。 So in this "direct" interpretation, all actor creation is ALWAYS local.所以在这种“直接”的解释中,所有演员的创作总是本地的。 No matter what.无论。 Full stop.句号。

Furthermore, you specifically mention cluster client.此外,您特别提到了集群客户端。 Which means that you are a client and not an actor.这意味着您是客户而不是演员。 Which means, in the strictest sense of the term, you cannot create actors.这意味着,从最严格的意义上讲,您不能创建演员。 At all.完全没有。 Remotely or locally, it doesn't matter, you can't create actors AT ALL if you are a cluster client.远程或本地,没关系,如果您是集群客户端,则根本无法创建参与者。 Only actors can create actors.只有演员才能创造演员。

But, in practical terms, this isn't true.但是,实际上,这是不正确的。 Because a very common case is that actors will allow you to spawn actors indirectly in response to a message.因为一个非常常见的情况是,actor 允许您间接生成actor 以响应消息。 This is what cluster sharding does: it essentially runs a "proxy" on every node that will automatically spawn actors as needed.这就是集群分片的作用:它本质上在每个节点上运行一个“代理”,将根据需要自动生成参与者。 The proxies will even stop and restart an actor on a different node to "rebalance" as needed.代理甚至会根据需要停止并重新启动不同节点上的参与者以“重新平衡”。

Which goes back to my original point, that there is nothing magic about how cluster sharding does this.这又回到了我最初的观点,集群分片如何做到这一点并没有什么神奇之处。 You could run your own proxy on each node.您可以在每个节点上运行自己的代理。 Either directly.要么直接。 Or via a cluster based router.或通过基于集群的路由器。 Or via an event bus.或通过事件总线。 Or any of a bunch of other mechanisms.或任何其他机制。

So, getting back to your specific question:所以,回到你的具体问题:

if i create 10 actors in 5 nodes will it create 2 actor in each node?*如果我在 5 个节点中创建 10 个演员,它会在每个节点中创建 2 个演员吗?*

To a certain extent, the question doesn't really make sense.在某种程度上,这个问题并没有真正的意义。 As noted above, as a cluster client, you can't actually create actors at all.如上所述,作为集群客户端,您实际上根本无法创建参与者。

But once you start talking about indirect creation, it's basically whatever you want, it just depends on how you write it.但是一旦你开始谈论间接创造,它基本上就是你想要的,它只取决于你如何编写它。

In Akka Sharding, which was your original question, the actors will be created on whatever node their hash dictates.在 Akka Sharding 中,这是您最初的问题,参与者将在其哈希指示的任何节点上创建。 Which hopefully is fairly uniform but might not be exactly even.希望这是相当均匀的,但可能不完全均匀。 But the sharding API doesn't have a "create actor" API, it just creates actors as needed.但是分片 API 没有“创建参与者”API,它只是根据需要创建参与者。

On the other hand, if you using some sort of cluster aware router to spawn actors, the functionality can be whatever you desire.另一方面,如果您使用某种集群感知路由器来生成演员,那么功能可以是您想要的任何东西。

But I feel like this question is getting too vague to answer meaningfully.但我觉得这个问题太模糊了,无法有意义地回答。

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

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