Akka 消息传递机制示例 [英] Akka messaging mechanisms by example

查看:27
本文介绍了Akka 消息传递机制示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有相当多的 Apache Camel(路由/中介/编排引擎;轻量级 ESB)经验,我正在绞尽脑汁试图了解 Akka 之间的区别:

I have a fair amount of Apache Camel (routing/mediation/orchestation engine; lightweight ESB) experience and am racking my brain trying to understand the difference between Akka:

  • 调度器(DispatcherPinnedDispatcherCallingThreadDispatcher)
  • 路由器
  • 群组
  • 事件总线

根据文档:

调度员是:

...是什么让 Akka Actors滴答作响",可以说它是机器的引擎.

...is what makes Akka Actors "tick", it is the engine of the machine so to speak.

但这并不能真正解释调度员是什么,或者它与演员的关系是什么.

But that doesn't really explain what a dispatcher is or what it's relationship to an actor is.

路由器是:

消息可以通过路由器发送,以有效地将它们路由到目标参与者,称为路由.路由器可以在角色内部或外部使用,您可以自己管理路由或使用具有配置功能的自包含路由器角色.但这听起来很像调度员.

Messages can be sent via a router to efficiently route them to destination actors, known as its routees. A Router can be used inside or outside of an actor, and you can manage the routees yourselves or use a self contained router actor with configuration capabilities. But it sounds an awful lot like a dispatcher.

是:

[A type of] 路由器 [that] 创建路由作为子actor,如果它们终止,则将它们从路由器中删除.

[A type of] router [that] creates routees as child actors and removes them from the router if they terminate.

是:

[A type of] actor [where routees] 在路由器外部创建,路由器使用 actor 选择将消息发送到指定路径,而无需监视终止.

[A type of] actor [where routees] are created externally to the router and the router sends messages to the specified path using actor selection, without watching for termination.

事件总线是:

...一种向演员组发送消息的方式

...a way to send messages to groups of actors

这听起来就像调度员和路由器一样.

This sounds just like dispatchers and routers.

所以我主要关心的是:

  • 调度程序、路由器和事件总线之间有什么区别,以及何时使用它们?
  • 何时使用池与组?

推荐答案

dispatcher 基本上是一个线程池.Akka 将调度程序用于多种用途(例如将消息放入正确的邮箱中或从参与者邮箱中提取消息并对其进行处理).每次需要执行这些操作之一时,都会从线程池中选择一个线程并为其使用.Akka 默认带有 default-dispatcher 配置,您可以在 reference.conf 搜索 default-dispatcher.你已经在使用 default-dispatcher 但你可以定义一个不同的调度器来确保你有一个保留的线程池用于其他目的(例如当使用 akka-remote 或 akka-cluster 时的 netty 线程).

A dispatcher is basically a thread-pool. Akka uses dispatcher for multiple things (like enqueueing messages in the right mailbox or pick up a message from an actor mailbox and process it). Every time one of these actions need to be performed a thread from the thread-pool is selected and used for it. Akka comes by default with a default-dispatcher with the config you can find here in reference.conf searching for default-dispatcher. You are already using the default-dispatcher but you can define a different dispatcher to ensure you have a reserved thread-pool for other purposes (for example for the netty threads when using akka-remote or akka-cluster).

Akka 中的路由器是一个actor,它使用某种路由逻辑将消息路由到路由列表.根据逻辑,有多种类型的路由器:广播、平衡、循环……您可以在 akka 文档中找到所有这些.路由器的路由节点可以是一个池,也可以是一个组.路由器最流行的用例之一是垂直扩展您的应用,这意味着最大限度地利用系统中所有可用的 CPU(同时使用多个线程).

A router in Akka is an actor that uses some kind of routing logic to route messages to a list of routees. There are many types of router depending on the logic: Broadcast, Balancing, RoundRobin... you can find all of them in the akka docs. The routees of the router can be a pool or a group. One of the most popular use cases of routers is to vertically scale your app which means maximize the use of all the CPU available in your system (using multiple threads at the same time).

意味着正在为给定类型按需创建路由.例如,您可能需要具有 3 个实例的 MyFancyActorRandomPool.Akka 将创建 MyFancyActor 的三个 actor 和第四个作为实际路由器的 actor.每次路由器 actor 收到一条消息时,都会将消息转发给 3 个 MyFancyActor 演员之一.池负责重启 actor 并观察它们的生命周期,以确保您有 n 个实例在运行.

A pool means that the routees are being created on-demand for a given type. For example you may want a RandomPool of MyFancyActor with 3 instances. Akka will create three actors of MyFancyActor and a fourth one that will be the actual router. Every time the router actor gets a message will be forwarding the message to one of the 3 MyFancyActor actors. A pool takes care of restarting actors and watch their lifecycle to ensure you have n number of instances running.

表示在定义路由器之前正在创建路由.一旦你定义了你的路由器,你将需要传递一个你之前创建的 routees actor 的列表.一个小组不会监控您的演员的生命周期,您需要自己做这件事.

A group means that the routees are being created before you are defining the router. Once you are defining your router you will need to pass a list of your routees actor that you previously created. A group will not monitor the lifecycle of your actors and you will need to do this yourself.

事件总线是您可以通过演员订阅特定类型消息的频道.如果有那种特定类型的消息,你的演员就会得到它.这用于某些 Akka 内部机制,例如当消息无法到达其目的地或有关集群形成的事件(在 akka-cluster 中)时订阅 DeadLetter .您将使用它来了解 ActorSystem 中发生的事件.

Event buses are channels where you can subscribe for a particular type of message with an actor. If there is a message of that particular type, you actor will get it. This is used for some Akka internals like subscribing to DeadLetters when a message is unable to reach its destination or events regarding the formation of a cluster (in akka-cluster). You will use this to be aware of events happening in your ActorSystem.

这篇关于Akka 消息传递机制示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆