在Akka中聚类

时间:2019-01-21 11:17:23

标签: java scala akka

我正在尝试了解Akka群集,以便使用节点进行并行计算。因此,我编写了一个阶乘程序,并希望在3个节点(包括主节点)的群集上运行它。

我正在使用配置文件来提供种子节点和群集提供程序。并在我的代码中读取文件。

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://ClusterSystem@127.0.0.1:9876",
        "akka.tcp://ClusterSystem@127.0.0.1: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
    }
  }
}

以下是 java 代码:

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)
  }
}

在运行程序时,我遇到了错误

  

与[null]的远程连接失败,并出现java.net.ConnectException:连接被拒绝:没有更多信息:/127.0.0.1:6789   [WARN] [01/21/2019 16:31:15.979] [New I / O boss#3] [NettyTransport(akka:// ClusterSystem)]与[null]的远程连接失败,并出现java.net.ConnectException:连接被拒绝:没有更多信息:/127.0.0.1:9876

我尝试搜索,但是我不知道为什么会出现此错误。

1 个答案:

答案 0 :(得分:0)

启动节点时,需要指定将在config中打开的确切端口

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

因此,如果您的种子节点说98766789。两个节点必须指定

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

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

请注意,种子节点列表中首先列出的节点必须首先开始。