How to run an Akka-HTTP server inside an Akka cluster?

1.1k Views Asked by At

I am building an Akka cluster and want to use Akka-HTTP server as an API server inside. How does one do that?

I would imagine it will be a cluster singleton, as it is an access point to the cluster, but making it an actor seems weird, as it will need to have a receive() method, which will do nothing (i guess).

1

There are 1 best solutions below

3
On

Simplest example:

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)

To stop:

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