Tarun Khaneja
Tarun Khaneja

Reputation: 451

Clustering in Akka

I am trying to understand Akka Clustering for parallel computation using nodes. So, I wrote one factorial program and want to run that on a cluster of 3 nodes (inclusive master).

I am using a configuration file to provide seed nodes and cluster provider. And reading file in my code.

cluster {
  akka {
    actor {
      provider = "cluster"
    }
    remote {
      log-remote-lifecycle-events = off
      netty.tcp {
        hostname = "127.0.0.1"
        port = 0
      }
    }

    cluster {
      seed-nodes = [
        "akka.tcp://[email protected]:9876",
        "akka.tcp://[email protected]:6789"]

      # auto downing is NOT safe for production deployments.
      # you may want to use it during development, read more about it in the docs.
      #
      # auto-down-unreachable-after = 10s
    }
  }
}

Following is the java code:

package test

import java.io.File

import akka.actor.{Actor,ActorSystem, Props}
import akka.stream.ActorMaterializer
import com.typesafe.config.ConfigFactory

import scala.concurrent.ExecutionContextExecutor


class Factorial extends Actor {
  override def receive = {
    case (n: Int) => fact(n)
  }

  def fact(n:Int): Int ={
    if (n<=1){
      return 1
    }
    else {
      return n * fact(n - 1)
    }
  }
}

object ClusterActor {
  def main(args: Array[String]): Unit = {

    val configFile = "E:/Scala/StatsRuleEngine/Resources/local_configuration.conf"
    val config = ConfigFactory.parseFile(new File(configFile))

    implicit val system:ActorSystem = ActorSystem("ClusterSystem" ,config.getConfig("cluster"))
    implicit val materializer:ActorMaterializer = ActorMaterializer()
    implicit val executionContext: ExecutionContextExecutor = system.dispatcher

    val FacActor = system.actorOf(Props[Factorial],"Factorial")

    FacActor ! (5)
  }
}

On running the program, I am getting below error

Remote connection to [null] failed with java.net.ConnectException: Connection refused: no further information: /127.0.0.1:6789 [WARN] [01/21/2019 16:31:15.979] [New I/O boss #3] [NettyTransport(akka://ClusterSystem)] Remote connection to [null] failed with java.net.ConnectException: Connection refused: no further information: /127.0.0.1:9876

I tried to search, but I don't why this error is coming.

Upvotes: 0

Views: 111

Answers (1)

Ivan Stanislavciuc
Ivan Stanislavciuc

Reputation: 7275

When you boot your nodes, you need to specify the exact ports that will be open in config

  netty.tcp {
    hostname = "127.0.0.1"
    port = 0 // THE EXACT PORT
  }

So, if your seed nodes say 9876 and 6789. Two of nodes have to specify

  netty.tcp {
    hostname = "127.0.0.1"
    port = 9876
  }

and

  netty.tcp {
    hostname = "127.0.0.1"
    port = 6789
  }

Note, that the node that is listed first in seed nodes list must start first.

Upvotes: 1

Related Questions