scala 如何使用 Akka 实现 REST Web 服务?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6668999/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 03:11:58  来源:igfitidea点击:

How to implement a REST Web Service using Akka?

scalarestakkascalatra

提问by Kamesh Rao Yeduvakula

I intend to implement a pure Akka powered REST based Web API. I am not sure about using spray. I would consider using Scalatra if it is any good. Basically I am interested in using the concurrency benefits of Scala Actor model. I don't want the Servlet container to be an hindrance in that process.

我打算实现一个纯 Akka 驱动的基于 REST 的 Web API。我不确定使用喷雾。如果有任何好处,我会考虑使用 Scalatra。基本上我对使用 Scala Actor 模型的并发优势很感兴趣。我不希望 Servlet 容器成为该过程中的障碍。

What can be other options?

还有什么其他选择?

Update 1: Which is better frontend for implementing REST based on Akka backend? - Spray, Scalatra or Dropwizard or any other?

更新 1:哪个前端更适合基于 Akka 后端实现 REST?- 喷雾、Scalatra 或 Dropwizard 或任何其他?

采纳答案by Thomas Lockney

The important thing to realize about Akka is that it's not an all-or-nothing environment. You can, to a large degree, mix and match different libraries to compose the system that makes sense for you. For instance, I have written apps that use Dropwizardfronting a backend which uses Akka, without issue. I have also done the same with Clothesline(implemented in Clojure, but with a Scala wrapper). And later this week, I hope to experiment with using Unfiltered's websocket implementationto sit in front of Akka and a RabbitMQ pipe for feeding near real-time data to the client browsers -- I specifically mention this example because the Unfiltered websocket impl sits on top of Nettyrather than a servlet container).

关于 Akka 需要意识到的重要一点是,它不是一个全有或全无的环境。您可以在很大程度上混合和匹配不同的库来组成对您有意义的系统。例如,我编写了使用Dropwizard 的应用程序,前端使用 Akka 的后端,没有问题。我也对Clothesline做了同样的事情(在 Clojure 中实现,但使用Scala 包装器)。本周晚些时候,我希望尝试使用Unfilteredwebsocket 实现坐在 Akka 和一个 RabbitMQ 管道前面,以将近乎实时的数据提供给客户端浏览器——我特别提到这个例子,因为 Unfiltered websocket impl 位于在Netty之上而不是 servlet 容器)。

Update: Since having written this answer a few years back, I've begun using Sprayexclusively for RESTful development with Akka. While pretty much any JVM REST library can be used, Spray fits very naturally into Akka's actor-based model and the library has clearly reached a level of maturity where it can easily be the default choice. The upcoming integration of Spray into Akka as the new akka-http module is a clear indication of this.

更新:自从几年前写下这个答案以来,我已经开始使用Spray专门用于 Akka 的 RESTful 开发。虽然几乎可以使用任何 JVM REST 库,但 Spray 非常自然地适合 Akka 基于 actor 的模型,并且该库显然已经达到了可以轻松成为默认选择的成熟度。即将作为新的 akka-http 模块将 Spray 集成到 Akka 中就清楚地表明了这一点。

回答by Nthalk

If you want the CODE to do it, then here it is. It took me a bit to really figure out what's going on, because there are a TON of examples, and it's not clear what they are all doing or how to put it all together. Turns out it was more simple than I thought:

如果您希望 CODE 做到这一点,那么它就在这里。我花了一些时间才真正弄清楚发生了什么,因为有很多例子,但不清楚它们都在做什么或如何将它们组合在一起。事实证明它比我想象的更简单:

package com.nthalk.akkatest

import akka.actor.Actor.actorOf
import akka.actor.Actor
import akka.camel.Consumer
import akka.camel.Message
import akka.camel.CamelServiceManager

class MyActor extends Actor with Consumer {
  def endpointUri = "jetty:http://localhost:8877/"
  def receive = {
    case msg: Message => { self.reply("State Rest Service: Achieved") }
    case _ => { self.reply("Really, no message?") }
  }
}

object App extends scala.App {
  actorOf[MyActor].start
  CamelServiceManager.startCamelService
}

And my build.sbt looks like:

我的 build.sbt 看起来像:

organization := "com.nthalk"

name := "akkatest"

version := "0.1.0"

resolvers += 
  "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/"

libraryDependencies ++= Seq(
  "org.apache.camel" % "camel-jetty" % "2.9.0",
  "se.scalablesolutions.akka" % "akka-camel" % "1.3.1"
  )

Hope this helps!

希望这可以帮助!

回答by futurechimp

For completeness, it seems useful to have a Scalatra example (since the question asked about Scalatra). Here's some example code from the Scalatra Akka Guide:

为了完整性,有一个 Scalatra 示例似乎很有用(因为问题是关于 Scalatra 的)。这是Scalatra Akka 指南中的一些示例代码:

package com.example.app

import akka.actor.{ActorRef, Actor, Props, ActorSystem}
import akka.dispatch.ExecutionContext
import akka.util.Timeout
import org.scalatra.FutureSupport
import org.scalatra.{Accepted, ScalatraServlet}

class MyActorApp(system:ActorSystem, myActor:ActorRef) extends ScalatraServlet with     FutureSupport {

  protected implicit def executor: ExecutionContext = system.dispatcher

  import _root_.akka.pattern.ask
  implicit val timeout = Timeout(10)

  get("/async") {
    new AsyncResult { def is = myActor ? "Do stuff and give me an answer" }
  }

  get("/fire-forget") {
    myActor ! "Hey, you know what?"
    Accepted()
  }
}

class MyActor extends Actor {
  def receive = {
    case "Do stuff and give me an answer" => sender ! "The answer is 42"
    case "Hey, you know what?" => println("Yeah I know... oh boy do I know")
  }

}

回答by Windor C

using akka-http(people also call it spray-2.0) which is based on akka-streams.

使用基于 akka-streams 的akka-http(人们也称它为spray-2.0)。

回答by opyate

HTTP servlets and the many containers out there is a tried and trusted technology. Akka offers you the choice of its own embedded servlet container, or you can use it with your own.

HTTP servlet 和许多容器是一种久经考验且值得信赖的技术。Akka 为您提供了自己的嵌入式 servlet 容器的选择,或者您可以将它与您自己的.

You can, of course, roll your own web server with Netty, and there's an interesting write-up about this on the web.

当然,您可以使用 Netty 启动您自己的 Web 服务器,并且在 Web 上有一篇关于此的有趣文章。

回答by StaxMan

I don't know why you are suspicious of using a Servlet container as the base -- it does not really limit your choice in any possible way, it just handles basic HTTP server plumbing. As such, most java service frameworks use servlet API as the basic foundation even if they don't expose that level.

我不知道您为什么怀疑使用 Servlet 容器作为基础——它并没有真正以任何可能的方式限制您的选择,它只是处理基本的 HTTP 服务器管道。因此,大多数 Java 服务框架使用 servlet API 作为基本基础,即使它们不公开该级别。

I think DropWizard is a great choice for all kinds of JVM rest services, including ones that use Akka for actual lifting. Besides the obvious reason of its goodness (based on collection of libraries proven to be "JVM's best"), I like it because it helps in areas that many other libs/frameworks leave out: production of metrics, declarative validation; all the while keeping things simple, explicit and understandable.

我认为 DropWizard 是各种 JVM 休息服务的绝佳选择,包括使用 Akka 进行实际提升的服务。除了其优点的明显原因(基于被证明是“JVM 最好的”库的集合),我喜欢它,因为它在许多其他库/框架遗漏的领域有所帮助:指标的生成、声明性验证;同时保持事情简单、明确和易于理解。