gpt4 book ai didi

scala - Akka 路由器的重要性

转载 作者:行者123 更新时间:2023-12-04 10:36:53 26 4
gpt4 key购买 nike

我对 Akka 的重要性存有疑虑。路由器。我在当前的项目中使用了 Akka Routers。但是,我对它的重要性感到有些困惑。出以下两种方法,哪个更有利。

  • 有路由器和路由。
  • 根据需要创建尽可能多的 Actor 。

  • 我知道路由器将根据策略在其路由之间分配传入消息。此外,我们可以有基于路由器的监管者策略。
    我也明白 Actor 也是轻量级的,创建尽可能多的 Actor 并不是一项开销。因此,我们可以为每个传入的消息创建参与者,并在处理 si 完成后在必要时将其杀死。

    所以我想了解一下上面的设计哪个更好?或者换句话说,在这种情况下(1)比(2)有优势,或者反之亦然。

    最佳答案

    好问题。在阅读 Akka 文档之前,我也有类似的疑问。原因如下:

  • 效率 .来自 docs :

    On the surface routers look like normal actors, but they are actually implemented differently. Routers are designed to be extremely efficient at receiving messages and passing them quickly on to routees.

    A normal actor can be used for routing messages, but an actor's single-threaded processing can become a bottleneck. Routers can achieve much higher throughput with an optimization to the usual message-processing pipeline that allows concurrent routing. This is achieved by embedding routers' routing logic directly in their ActorRef rather than in the router actor. Messages sent to a router's ActorRef can be immediately routed to the routee, bypassing the single-threaded router actor entirely.

    The cost to this is, of course, that the internals of routing code are more complicated than if routers were implemented with normal actors. Fortunately all of this complexity is invisible to consumers of the routing API. However, it is something to be aware of when implementing your own routers.

  • 默认实现 多种路由策略。你总是可以自己写,但它可能会变得棘手。你要兼顾监管、恢复、负载均衡、远程部署等。
  • Akka 路由器模式将为 熟悉 给 Akka 用户。如果您推出自定义路由,那么每个人都必须花时间了解所有极端情况和影响(+ 测试?:))。

  • TL;DR 如果你不太关心效率,如果你更容易产生新的 Actor ,那就去吧。否则使用路由器。

    关于scala - Akka 路由器的重要性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30044189/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com