如何在Akka集群中运行Akka-HTTP服务器?

时间:2017-01-06 20:18:01

标签: scala akka-http akka-cluster

我正在构建一个Akka集群,并希望将Akka-HTTP服务器用作内部的API服务器。如何做到这一点?

我认为它将是一个集群单例,因为它是集群的访问点,但是使它成为一个演员似乎很奇怪,因为它需要有一个receive()方法,它什么也不做(我猜)。

1 个答案:

答案 0 :(得分:-2)

最简单的例子:

implicit val system = ... // your ActorSystem goes here
implicit val materializer = ActorMaterializer()
implicit val executionContext = system.dispatcher

val route =
  path("hello") {
    get {
      complete(HttpEntity(ContentTypes.`text/plain(UTF-8)`, "Hello World !"))
    }
  }

val bindingFuture = Http().bindAndHandle(route, "localhost", 8080)

停止:

bindingFuture
    .flatMap(_.unbind()) // trigger unbinding from the port
    .onComplete(_ => system.terminate()) // and shutdown when done