gpt4 book ai didi

java - Akka 中的集群

转载 作者:行者123 更新时间:2023-12-02 10:21:50 26 4
gpt4 key购买 nike

我正在尝试了解 Akka Clustering 以使用节点进行并行计算。因此,我编写了一个阶乘程序,并希望在 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)
}
}

运行程序时,出现以下错误

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

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

最佳答案

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

  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
}

请注意,种子节点列表中第一个列出的节点必须首先启动。

关于java - Akka 中的集群,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54288828/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com