3

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).

Ehud Kaldor
  • 753
  • 1
  • 7
  • 20

1 Answers1

-2

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
  • I think you missed my question. I am looking to run it as part of an Akka cluster, as a singleton (it anything similar). – Ehud Kaldor Jan 07 '17 at 00:23
  • 2
    Start the http on each node, and write the logic of the endpoint inside an actor, (having the actorRef, previously created, locally or remotely in the cluster) and access to one of then (in the cluster), with actorSelection, or clusterClient . If you use routerPools, the **allow-local-routee** can help to distribute the messages in the cluster – gaston Jan 13 '17 at 18:42
  • Have you looked at http://doc.akka.io/docs/akka/2.4/scala/cluster-singleton.html ? – Dragisa Krsmanovic Jan 14 '17 at 22:29