问题描述
我有大量的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:
- 调度程序(
调度程序
,PinnedDispatcher
,CallingThreadDispatcher
) - 路由器
- 游泳池
- 组
- 事件巴士
- Dispatchers (
Dispatcher
,PinnedDispatcher
,CallingThreadDispatcher
) - Routers
- Pools
- Groups
- Event Buses
根据文档:
调度员是:
但这并不能真正解释调度员是什么或与演员的关系。
But that doesn't really explain what a dispatcher is or what it's relationship to an actor is.
路由器是:
游泳池是:
组是:
事件总线是:
这听起来像调度程序和路由器。
This sounds just like dispatchers and routers.
所以我主要担心的是:
- 调度程序,路由器和事件总线之间的区别是什么,何时使用它们?
- 何时使用池vs组?
推荐答案
调度程序基本上是一个线程池。 Akka将调度程序用于多种用途(例如,将消息放入正确的邮箱中,或者从actor邮箱中提取一条消息并进行处理)。每次需要执行这些操作之一时,就会选择并使用线程池中的线程。 Akka默认带有 default-dispatcher
,其配置可在搜索 default-dispatcher
。您已经在使用 default-dispatcher
,但是您可以定义一个不同的调度程序,以确保您有一个保留的线程池用于其他目的(例如,使用akka时用于netty线程) -remote或akka-cluster)。
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中的 router 是一个演员,它使用某种路由逻辑将消息路由到列表的路线。取决于逻辑,路由器的类型很多:广播,平衡,RoundRobin ...您可以在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).
池表示正在为给定类型按需创建路由。例如,您可能需要 RandomPool
的 MyFancyActor
和3个实例。 Akka将创建三个 MyFancyActor
的演员,第四个将是实际的路由器。路由器参与者每次收到消息时,都会将消息转发到3个 MyFancyActor
参与者之一。池负责重新启动参与者,并观察它们的生命周期,以确保您正在运行的实例数为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.
一个组表示路由为在定义路由器之前被创建。定义路由器后,您将需要传递先前创建的路由角色的列表。小组不会监视演员的生命周期,您需要自己进行操作。
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内部人员,例如订阅 DeadLetter
s。您将使用它来了解您的 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 DeadLetter
s 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消息传递机制示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!