Randomize
Randomize

Reputation: 9103

Akka: How to start a simple local cluster?

akka {
  actor {
    provider = "akka.cluster.ClusterActorRefProvider"
  }
  remote {
    enabled-transports = ["akka.remote.netty.tcp"]
    netty.tcp {
      hostname = "127.0.0.1"
      port = 0
    }
  }
}

akka.cluster {
  seed-nodes = [
    "akka.tcp://[email protected]:2551",
    "akka.tcp://[email protected]:2552"
  ]
}

object AndromedaApiClusterActivator extends App {
  val system = ActorSystem("MyCluster", ConfigFactory.load())
  val clusterController = system.actorOf(Props[MyCluster], name = "MyCluster")
}


class MyCluster extends Actor  {

  val log = Logging(context.system, this)
  val cluster = Cluster(context.system)

  override def preStart() {
    cluster.subscribe(self, classOf[MemberEvent], classOf[UnreachableMember])
  }

  override def postStop() {
    cluster.unsubscribe(self)
  }

  override def receive = {
    case x: MemberEvent => log.info("MemberEvent: {}", x)
    case x: UnreachableMember => log.info("UnreachableMember {}: ", x)
  }

}

When I run it I get:

Association with remote system [akka.tcp://[email protected]:2552] has failed, address is now gated for [5000] ms. Reason: [Association failed with [akka.tcp://[email protected]:2552]] Caused by: [Connection refused: /127.0.0.1:2552]
Association with remote system [akka.tcp://[email protected]:2551] has failed, address is now gated for [5000] ms. Reason: [Association failed with [akka.tcp://[email protected]:2551]] Caused by: [Connection refused: /127.0.0.1:2551]

I cannot find an explanation. Any help?

Upvotes: 1

Views: 1121

Answers (1)

Nikita
Nikita

Reputation: 4515

You should start 2 nodes first and then connect to them. To illustrate it, I will create both systems inside one App, but you can run 2 instances of the App with different configs/ports specified in command line.

object Main extends App {
  val system1 = ActorSystem("MyCluster1", ConfigFactory.load("node1.conf"))
  val system2 = ActorSystem("MyCluster2", ConfigFactory.load("node2.conf"))
  val clusterController = system1.actorOf(Props[MyCluster], name = "MyCluster1")
}

application.conf:

akka {
  actor {
    provider = "akka.cluster.ClusterActorRefProvider"
  }
  remote {
    enabled-transports = ["akka.remote.netty.tcp"]
    netty.tcp {
      hostname = "127.0.0.1"
      port = 2552
    }
  }
}

akka.cluster {
  seed-nodes = [
    "akka.tcp://[email protected]:2552",
    "akka.tcp://[email protected]:2553"
  ]
}

To start other nodes, I suggest to specify different configs with node1.conf:

include "application"

akka.remote.netty.tcp.port = 2552

node2.conf:

include "application"

akka.remote.netty.tcp.port = 2553

Upvotes: 1

Related Questions