Local using config

Configuration based discovery can be used to see the Cluster Bootstrap process run locally within an IDE or from the command line.

To use config service discovery set the following configuration:

  • akka.management.cluster.bootstrap.contact-point-discovery.discovery-method to config
  • akka.discovery.config.services.[cluster-name] to the endpoinds of the Akka nodes

For example:

akka.discovery {
  config.services = {
    local-cluster = {
      endpoints = [
        {
          host = "127.0.0.1"
          port = 8558
        },
        {
          host = "127.0.0.2"
          port = 8558
        },
        {
          host = "127.0.0.3"
          port = 8558
        }
      ]
    }
  }
}
Full source at GitHub

This configuration will return three endpoints for a service called local-cluster.

Akka bootstrap is then configured to lookup local-cluster in the config:

akka.management {
  cluster.bootstrap {
    contact-point-discovery {
      service-name = "local-cluster"
      discovery-method = akka.discovery.config
    }
  }
}
Full source at GitHub

Three main methods can be run, only overriding the host so the ActorSystem’s can all bind to the same port:

object Node1 extends App {
  new Main(1)
}

object Node2 extends App {
  new Main(2)
}

object Node3 extends App {
  new Main(3)
}

class Main(nr: Int) {

  val config = ConfigFactory.parseString(s"""
      akka.remote.artery.canonical.hostname = "127.0.0.$nr"
      akka.management.http.hostname = "127.0.0.$nr"
    """).withFallback(ConfigFactory.load())
  val system = ActorSystem("local-cluster", config)

  AkkaManagement(system).start()

  ClusterBootstrap(system).start()

  Cluster(system).registerOnMemberUp({
    system.log.info("Cluster is up!")
  })
}
Full source at GitHub

The example uses three loopback addresses: 127.0.0.2-4. On Mac you’ll need to set these up:

sudo ifconfig lo0 alias 127.0.0.2 up
sudo ifconfig lo0 alias 127.0.0.3 up
sudo ifconfig lo0 alias 127.0.0.4 up

On Linux this should not be required.

Run the three mains: Node1, Node2 and Node3 and they will form a cluster either in your IDE or from the command line:

sbt "bootstrap-demo-local/runMain akka.cluster.bootstrap.Node1"
sbt "bootstrap-demo-local/runMain akka.cluster.bootstrap.Node2"
sbt "bootstrap-demo-local/runMain akka.cluster.bootstrap.Node3"

The first time one of the Nodes will form a new cluster and the others will join. Any subsequent restarts then the node will discover a cluster already exists and join.

The source code for this page can be found here.