Akka-您应该创建一个actor实例多少个? [英] Akka - How many instances of an actor should you create?

查看:100
本文介绍了Akka-您应该创建一个actor实例多少个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Akka框架的新手,我正在Netty + Akka上构建HTTP服务器应用程序。

I'm new to the Akka framework and I'm building an HTTP server application on top of Netty + Akka.

到目前为止,我的想法是创建一个每种请求的参与者。例如。我本来有个演员要去/ my-resource进行POST,而要另一个演员要给/ my-resource进行GET。

My idea so far is to create an actor for each type of request. E.g. I would have an actor for a POST to /my-resource and another actor for a GET to /my-resource.

我很困惑的是我应该怎么去关于演员创作?我是否应该:

Where I'm confused is how I should go about actor creation? Should I:


  1. 为每个请求创建一个新的actor(这意味着对于每个请求,我都应该执行TypedActor.newInstance ()的适当演员)?创建一个新演员多少钱?

  1. Create a new actor for every request (by this I mean for every request should I do a TypedActor.newInstance() of the appropriate actor)? How expensive is it to create a new actor?

在服务器启动时为每个参与者创建一个实例,并对每个请求使用该参与者实例吗?我读过一个演员一次只能处理一条消息,所以这不是瓶颈吗?

Create one instance of each actor on server start up and use that actor instance for every request? I've read that an actor can only process one message at a time, so couldn't this be a bottle neck?

还有其他事情吗?

感谢您的任何反馈。

推荐答案

好,您为要管理的每个可变状态实例创建一个Actor。

Well, you create an Actor for each instance of mutable state that you want to manage.

在您的情况下,如果 my-resource 是单个对象,而您可能只是一个演员顺序对待每个请求-可以轻松确保在修改之间仅返回一致的状态。

In your case, that might be just one actor if my-resource is a single object and you want to treat each request serially - that easily ensures that you only return consistent states between modifications.

如果(更有可能)您管理多个资源,则通常最好是每个资源实例一个参与者除非您遇到成千上万的资源。虽然您还可以运行按请求的参与者,但如果您不考虑这些请求所访问的状态,则会得到一个奇怪的设计。如果您仅针对每个POST请求创建一个Actor,就会发现自己担心如何阻止他们同时修改同一资源,这清楚地表明您错误地定义了Actor。

If (more likely) you manage multiple resources, one actor per resource instance is usually ideal unless you run into many thousands of resources. While you can also run per-request actors, you'll end up with a strange design if you don't think about the state those requests are accessing - e.g. if you just create one Actor per POST request, you'll find yourself worrying how to keep them from concurrently modifying the same resource, which is a clear indication that you've defined your actors wrongly.

我通常有相当琐碎的请求/回复参与者,其主要目的是抽象与外部系统的通信。因此,他们与实例参与者的通信通常仅限于一对请求/响应来执行实际操作。

I usually have fairly trivial request/reply actors whose main purpose it is to abstract the communication with external systems. Their communication with the "instance" actors is then normally limited to one request/response pair to perform the actual action.

这篇关于Akka-您应该创建一个actor实例多少个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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