You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Akka Cluster出现java.net.ConnectException: Connection refused错误求助

Akka Cluster Connection Refused 问题排查

问题现象

使用Akka Cluster时持续出现Connection refused错误,无法连接配置中的种子节点。

相关文件

application.conf

akka {
  actor {
    provider = "cluster"
  }
  remote.artery {
    canonical {
      hostname = "127.0.0.1"
      port = 0
    }
  }

  cluster {
    seed-nodes = [
      "akka://ClusterSystem@127.0.0.1:2551",
      "akka://ClusterSystem@127.0.0.1:2552"]
    auto-down-unreachable-after = 10s
  }
}

ClusterListener Actor类

class ClusterListener extends Actor{

  val cluster: Cluster = Cluster(context.system)

  override def preStart(): Unit = {
//    subscribe
    cluster.subscribe(self, initialStateMode = InitialStateAsEvents, classOf[MemberEvent], classOf[UnreachableMember])
  }

  override def postStop(): Unit = {
//    unsubscribe
    cluster.unsubscribe(self)
  }
  override def receive: Receive = {
    case MemberUp(member) =>
      log.info("Member is Up: {}", member.address)

    case UnreachableMember(member) =>
      log.info("Member detected as unreachable: {}", member)

    case MemberRemoved(member, previousStatus) =>
      log.info("Member is Removed: {} after {}", member.address, previousStatus)

    case _: MemberEvent =>
      log.info("Something else")
  }
}

主类

object MainClass extends App {
  implicit val system: ActorSystem = ActorSystem("ClusterSystem")

  val actor = system.actorOf(Props(new ClusterListener),"clusterActor")
    Cluster(system).registerOnMemberUp(actor)
}

错误日志

[INFO] [04/06/2023 13:11:25.781] [main] [ArteryTcpTransport(akka://ClusterSystem)] Remoting started with transport [Artery tcp]; listening on address [akka://ClusterSystem@127.0.0.1:35911] with UID [-7093993311719804604]
[INFO] [04/06/2023 13:11:25.831] [main] [Cluster(akka://ClusterSystem)] Cluster Node [akka://ClusterSystem@127.0.0.1:35911] - Starting up, Akka version [2.6.0] ...
[INFO] [04/06/2023 13:11:26.217] [main] [Cluster(akka://ClusterSystem)] Cluster Node [akka://ClusterSystem@127.0.0.1:35911] - Registered cluster JMX MBean [akka:type=Cluster]
[INFO] [04/06/2023 13:11:26.217] [main] [Cluster(akka://ClusterSystem)] Cluster Node [akka://ClusterSystem@127.0.0.1:35911] - Started up successfully
[INFO] [04/06/2023 13:11:26.287] [ClusterSystem-akka.actor.internal-dispatcher-5] [Cluster(akka://ClusterSystem)] Cluster Node [akka://ClusterSystem@127.0.0.1:35911] - No downing-provider-class configured, manual cluster downing required, see https://doc.akka.io/docs/akka/current/typed/cluster.html#downing
[WARN] [04/06/2023 13:11:26.706] [ClusterSystem-akka.remote.default-remote-dispatcher-11] [akka.stream.Log(akka://ClusterSystem/system/Materializers/StreamSupervisor-1)] [outbound connection to [akka://ClusterSystem@127.0.0.1:2552], message stream] Upstream failed, cause: StreamTcpException: Tcp command [Connect(127.0.0.1:2552,None,List(),Some(5000 milliseconds),true)] failed because of java.net.ConnectException: Connection refused
[WARN] [04/06/2023 13:11:26.710] [ClusterSystem-akka.remote.default-remote-dispatcher-7] [akka.stream.Log(akka://ClusterSystem/system/Materializers/StreamSupervisor-1)] [outbound connection to [akka://ClusterSystem@127.0.0.1:2552], control stream] Upstream failed, cause: StreamTcpException: Tcp command [Connect(127.0.0.1:2552,None,List(),Some(5000 milliseconds),true)] failed because of java.net.ConnectException: Connection refused
[WARN] [04/06/2023 13:11:26.719] [ClusterSystem-akka.remote.default-remote-dispatcher-7] [akka.stream.Log(akka://ClusterSystem/system/Materializers/StreamSupervisor-1)] [outbound connection to [akka://ClusterSystem@127.0.0.1:2551], message stream] Upstream failed, cause: StreamTcpException: Tcp command [Connect(127.0.0.1:2551,None,List(),Some(5000 milliseconds),true)] failed because of java.net.ConnectException: Connection refused
[WARN] [04/06/2023 13:11:26.719] [ClusterSystem-akka.remote.default-remote-dispatcher-11] [akka.stream.Log(akka://ClusterSystem/system/Materializers/StreamSupervisor-1)] [outbound connection to [akka://ClusterSystem@127.0.0.1:2551], control stream] Upstream failed, cause: StreamTcpException: Tcp command [Connect(127.0.0.1:2551,None,List(),Some(5000 milliseconds),true)] failed because of java.net.ConnectException: Connection refused

问题原因及解决方法

核心问题

配置中指定的种子节点akka://ClusterSystem@127.0.0.1:2551和akka://ClusterSystem@127.0.0.1:2552并未启动,当前节点(随机端口35911)尝试连接不存在的服务,因此抛出Connection refused错误。

解决步骤

  1. 先启动种子节点

    • 启动第一个种子节点,指定端口2551:
      java -Dakka.remote.artery.canonical.port=2551 -jar your-app.jar
      
    • 启动第二个种子节点,指定端口2552:
      java -Dakka.remote.artery.canonical.port=2552 -jar your-app.jar
      

    这两个节点启动后会作为集群的种子节点,等待其他节点加入。

  2. 启动普通节点
    保持原有配置(port=0随机端口)启动其他节点,它们会自动连接到种子节点并加入集群。

  3. 修正主类中的错误用法
    registerOnMemberUp方法需要传入一个代码块(() => Unit),而非ActorRef。修改主类代码:

    object MainClass extends App {
      implicit val system: ActorSystem = ActorSystem("ClusterSystem")
      val log = Logging(system, this.getClass)
    
      val actor = system.actorOf(Props(new ClusterListener),"clusterActor")
      Cluster(system).registerOnMemberUp {
        log.info("Current node has joined the cluster successfully")
        // 这里可以添加节点加入集群后的初始化逻辑
      }
    }
    
  4. 可选:配置downing provider
    日志中提示的"No downing-provider-class configured"警告,可通过添加akka.cluster.downing-provider-class = "akka.cluster.sbr.SplitBrainResolverProvider"来配置自动脑裂解决,避免手动down节点的麻烦。

内容的提问来源于stack exchange,提问作者user21236160

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 12:42:36