scala 在 Play 中如何使用 Akka?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22718905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How is Akka used in Play?
提问by OlivierBlanvillain
Play's home pagesays:
Play的主页说:
Built on Akka, Play provides predictable and minimal resource consumption (CPU, memory, threads) for highly-scalable applications.
Play 构建在 Akka 之上,为高度可扩展的应用程序提供可预测且最小的资源消耗(CPU、内存、线程)。
I'd like to know exactly how and where Akka is used in Play and what are the consequences of having Play build on top of Akka.
我想确切地知道 Akka 在 Play 中的使用方式和位置,以及在 Akka 之上构建 Play 的后果是什么。
回答by James Roper
In Play 2.0, Play delegated all requests to go through an actor. It heavily depended on Akka's future API and other parts.
在Play 2.0 中,Play 委托所有请求通过一个 actor。它严重依赖 Akka 未来的 API 和其他部分。
In Play 2.1, with the move of Akka's future API into Scala 2.10, Play started depending less directly on Akka. It gets all it's execution contexts from Akka, and provides integration with Akka, but that's about the extent of it.
在Play 2.1 中,随着 Akka 未来的 API 迁移到 Scala 2.10,Play 开始减少对 Akka 的直接依赖。它从 Akka 获取它的所有执行上下文,并提供与 Akka 的集成,但这只是它的范围。
In Play 2.3, we're adding new features to aid Akka integration, particularly around WebSockets.
在Play 2.3 中,我们添加了新功能以帮助 Akka 集成,特别是围绕 WebSockets。
In Play 2.4, Play will be ported to the new akka-http (formerly known as spray), at which point, Play will be as built on Akka as you can get.
在Play 2.4 中,Play 将被移植到新的 akka-http(以前称为 Spray),届时,Play 将尽可能建立在 Akka 之上。
What are the consequences? Akka provides a paradigm for programming that makes concurrency simple to deal with. It also provides great abstractions for distributed programming - the hardest thing about distributed programming is dealing with failures (which happen all the time) appropriately. Most tools try to address this by trying to hide failures from you, but unfortunately hiding something doesn't make it go away, and actually really makes things harder because when you try to address specific types of failures, the fact that they are hidden away from you gets in your way. Akka pushes failures in your face, so that when you're coding, you are forced to think about how your application will respond to failures. Consequently you're forced to design/code your application in such a way that it is tolerant to failures. It also gives you the tools to deal with them in a hierarchical fashion, allowing you to specify at what level you want to handle what type of failure, and how the failure should be responded to (die, retry up to n times, etc).
后果是什么?Akka 提供了一种编程范式,使并发性易于处理。它还为分布式编程提供了很好的抽象——分布式编程最困难的事情是适当地处理失败(这种情况一直在发生)。大多数工具试图通过向您隐藏失败来解决这个问题,但不幸的是,隐藏某些东西并不能让它消失,而且实际上会让事情变得更加困难,因为当您尝试解决特定类型的失败时,它们被隐藏的事实从你那里得到了你的方式。Akka 将失败推到你面前,所以当你编码时,你不得不考虑你的应用程序将如何响应失败。因此,您被迫以能够容忍失败的方式设计/编码您的应用程序。
So how does this help Play? The better question is how does it help a Play user? Akka helps me to implement Play itself, but it's possible to implement it without Akka (in fact Netty does most of the heavy lifting now, that will change in Play 2.4). The important thing is that Play seamlessly integrates with Akka, making it easy to handle HTTP requests with actors, handle failures etc, and this helps Play users because it allows them to design their application in such a way that it is scalable and resilient.
那么这对 Play 有何帮助?更好的问题是它如何帮助 Play 用户?Akka 帮助我实现 Play 本身,但可以在没有 Akka 的情况下实现它(实际上 Netty 现在承担了大部分繁重的工作,这将在 Play 2.4 中改变)。重要的是 Play 与 Akka 无缝集成,可以轻松地处理带有 Actor 的 HTTP 请求、处理故障等,这有助于 Play 用户,因为它允许他们以可扩展和弹性的方式设计他们的应用程序。
UPDATE:The above was written 3 years ago, a lot has changed since then. Play 2.4 did provide experimental support for akka-http, but Play still by default uses Netty.
更新:以上是 3 年前写的,从那时起发生了很多变化。Play 2.4 确实为 akka-http 提供了实验性支持,但 Play 仍然默认使用 Netty。
In Play 2.5, we deprecated the iteratees API and switched to Akka streams. This meant that now all asynchronous IO was going through Akka streams. Soon (not sure if that will be Play 2.6 or later), Play will flick the switch to make akka-http the default backing implementation of the server (though not yet the WS client).
在Play 2.5 中,我们弃用了 iteratees API 并切换到 Akka 流。这意味着现在所有异步 IO 都通过 Akka 流。很快(不确定是 Play 2.6 还是更高版本),Play 将轻弹开关,使 akka-http 成为服务器的默认支持实现(尽管还不是 WS 客户端)。
UPDATE 2:Play 2.6now makes akka-http the default backend implementation of it's HTTP server (Netty is still available as an option).
更新 2:Play 2.6现在使 akka-http 成为其 HTTP 服务器的默认后端实现(Netty 仍可作为一个选项)。

