Newton Zou
Newton Zou

Reputation: 588

How can I define seed-nodes in runtime

I have a akka cluster with application.conf like below:

 remote { // Remote configuration for this seed node
  enabled-transports = ["akka.remote.netty.tcp"]
  netty.tcp {
    hostname = ""
    port = 2555
  }
}
cluster {
  seed-nodes = [
    "akka.tcp://[email protected]:2555",
    "akka.tcp://[email protected]:2555"
  ] // Seed nodes of the cluster
  auto-down-unreachable-after = 10s
}

Right now, the seed-nodes are hard-code. I want to configure a parameter here like this:

cluster {
  seed-nodes = [
    "akka.tcp://automation-akka-http@${?HOST1}:2555",
    "akka.tcp://automation-akka-http@${?HOST2}:2555"
  ] // Seed nodes of the cluster
  auto-down-unreachable-after = 10s
}

I know that I can define these parameters in sbt command for compile. But that will not resolve my problem, because I can only have seed nodes' IP in deployment phase. Is there a way to define these parameters on start.

Upvotes: 0

Views: 598

Answers (1)

thwiegan
thwiegan

Reputation: 2173

As explained in Akka Cluster Documentation

You can join seed nodes programmatically with Cluster(system).joinSeedNodes.

According to Akka API docs it takes a Sequence of akka.actor.Address, which would then be your seed node addresses.

So something like this should work:

val seeds = Seq(Address("akka.tcp", "RemoteSystem1", "host1", 1234),
                Address("akka.tcp", "RemoteSystem2", "host2", 1234)

Cluster(system).joinSeedNodes(seeds)

Where according to Address API docs

  • "akka.tcp" = protocol
  • "RemoteSystem1" = Name of remote actor system
  • "host1" = Host of remote actor system
  • 1234 = Port of remote actor system

EDIT

Created a minimal example on GitHub

val cluster = Cluster(context.system)

// Join the seed node with the given address
seedPort match {
  case Some(port) =>
    cluster.joinSeedNodes(immutable.Seq(Address("akka.tcp", "ClusterSystem", "127.0.0.1", port)))
  case _ =>
}

Can be run with sbt "run akkaPort [clusterPort]". So to initially start the cluster run with two identical ports:

sbt "run 1337 1337"

and then run an additional node with a different akkaPort:

sbt "run 1338 1337"

This node will then join.

If you omit the clusterPort parameter, it will not execute this line:

cluster.joinSeedNodes(immutable.Seq(Address("akka.tcp", "ClusterSystem", "127.0.0.1", port))) 

And therefore just using the seed node from the application.conf file.

Upvotes: 1

Related Questions