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:

sourceakka.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
        }
      ]
    }
  }
}

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

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

sourceakka.management {
  cluster.bootstrap {
    contact-point-discovery {
      service-name = "local-cluster"
      discovery-method = config
    }
  }
}

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

sourceobject 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: 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!")
  })
}

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 "integration-test-local/runMain akka.cluster.bootstrap.Node1"
sbt "integration-test-local/runMain akka.cluster.bootstrap.Node2"
sbt "integration-test-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.

Found an error in this documentation? The source code for this page can be found here. Please feel free to edit and contribute a pull request.