/integration-test/kubernetes-dns/src/main/scala/akka/cluster/bootstrap/ClusterApp.scala

https://github.com/akka/akka-management · Scala · 72 lines · 55 code · 13 blank · 4 comment · 0 complexity · 648cecf651b4f47bb2f4eb6ede6e1681 MD5 · raw file

  1. /*
  2. * Copyright (C) 2017-2020 Lightbend Inc. <https://www.lightbend.com>
  3. */
  4. package akka.cluster.bootstrap
  5. import akka.actor.{ Actor, ActorLogging, ActorSystem, PoisonPill, Props }
  6. import akka.cluster.ClusterEvent.ClusterDomainEvent
  7. import akka.cluster.singleton.{ ClusterSingletonManager, ClusterSingletonManagerSettings }
  8. import akka.cluster.{ Cluster, ClusterEvent }
  9. import akka.http.scaladsl.Http
  10. import akka.http.scaladsl.model._
  11. import akka.http.scaladsl.server.Directives._
  12. import akka.management.cluster.bootstrap.ClusterBootstrap
  13. import akka.management.scaladsl.AkkaManagement
  14. import akka.stream.ActorMaterializer
  15. object ClusterApp {
  16. def main(args: Array[String]): Unit = {
  17. implicit val system = ActorSystem()
  18. implicit val materializer = ActorMaterializer()
  19. implicit val executionContext = system.dispatcher
  20. val cluster = Cluster(system)
  21. system.log.info("Starting Akka Management")
  22. AkkaManagement(system).start()
  23. ClusterBootstrap(system).start()
  24. system.actorOf(
  25. ClusterSingletonManager.props(
  26. Props[NoisySingleton],
  27. PoisonPill,
  28. ClusterSingletonManagerSettings(system)
  29. )
  30. )
  31. Cluster(system).subscribe(
  32. system.actorOf(Props[ClusterWatcher]),
  33. ClusterEvent.InitialStateAsEvents,
  34. classOf[ClusterDomainEvent]
  35. )
  36. // add real app routes here
  37. val routes =
  38. path("hello") {
  39. get {
  40. complete(
  41. HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>Hello</h1>")
  42. )
  43. }
  44. }
  45. Http().bindAndHandle(routes, "0.0.0.0", 8080)
  46. system.log.info(
  47. s"Server online at http://localhost:8080/\nPress RETURN to stop..."
  48. )
  49. cluster.registerOnMemberUp(() => {
  50. system.log.info("Cluster member is up!")
  51. })
  52. }
  53. class ClusterWatcher extends Actor with ActorLogging {
  54. val cluster = Cluster(context.system)
  55. override def receive = {
  56. case msg => log.info(s"Cluster ${cluster.selfAddress} >>> " + msg)
  57. }
  58. }
  59. }