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

首次安装Kafka启动报错:ZooKeeperClient连接2181端口被拒绝

问题复现

首次安装Kafka后执行启动命令:
.\bin\windows\kafka-server-start.bat .\config\server.properties
返回如下错误日志:

[2021-10-08 10:03:43,563] INFO Socket error occurred: localhost/0:0:0:0:0:0:0:1:2181: Connection refused: no further information (org.apache.zookeeper.ClientCnxn)
[2021-10-08 10:03:44,693] INFO Opening socket connection to server localhost/0:0:0:0:0:0:0:1:2181. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
[2021-10-08 10:03:46,743] INFO Socket error occurred: localhost/0:0:0:0:0:0:0:1:2181: Connection refused: no further information (org.apache.zookeeper.ClientCnxn)
[2021-10-08 10:03:47,873] INFO Opening socket connection to server localhost/0:0:0:0:0:0:0:1:2181. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
[2021-10-08 10:03:49,928] INFO Socket error occurred: localhost/0:0:0:0:0:0:0:1:2181: Connection refused: no further information (org.apache.zookeeper.ClientCnxn)
[2021-10-08 10:03:51,043] INFO Opening socket connection to server localhost/0:0:0:0:0:0:0:1:2181. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
[2021-10-08 10:03:53,093] INFO Socket error occurred: localhost/0:0:0:0:0:0:0:1:2181: Connection refused: no further information (org.apache.zookeeper.ClientCnxn)
[2021-10-08 10:03:54,233] INFO Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
[2021-10-08 10:03:56,283] INFO Socket error occurred: localhost/127.0.0.1:2181: Connection refused: no further information (org.apache.zookeeper.ClientCnxn)
[2021-10-08 10:03:57,403] INFO Opening socket connection to server localhost/0:0:0:0:0:0:0:1:2181. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
[2021-10-08 10:03:59,443] INFO Socket error occurred: localhost/0:0:0:0:0:0:0:1:2181: Connection refused: no further information (org.apache.zookeeper.ClientCnxn)
[2021-10-08 10:03:59,523] INFO [ZooKeeperClient Kafka server] Closing. (kafka.zookeeper.ZooKeeperClient)
[2021-10-08 10:03:59,683] INFO Session: 0x0 closed (org.apache.zookeeper.ZooKeeper)
[2021-10-08 10:03:59,693] INFO EventThread shut down for session: 0x0 (org.apache.zookeeper.ClientCnxn)
[2021-10-08 10:03:59,708] INFO [ZooKeeperClient Kafka server] Closed. (kafka.zookeeper.ZooKeeperClient)
[2021-10-08 10:03:59,713] ERROR Fatal error during KafkaServer startup. Prepare to shutdown (kafka.server.KafkaServer)
kafka.zookeeper.ZooKeeperClientTimeoutException: Timed out waiting for connection while in state: CONNECTING
        at kafka.zookeeper.ZooKeeperClient.waitUntilConnected(ZooKeeperClient.scala:271)
        at kafka.zookeeper.ZooKeeperClient.<init>(ZooKeeperClient.scala:125)
        at kafka.zk.KafkaZkClient$.apply(KafkaZkClient.scala:1948)
        at kafka.server.KafkaServer.createZkClient$1(KafkaServer.scala:431)
        at kafka.server.KafkaServer.initZkClient(KafkaServer.scala:456)
        at kafka.server.KafkaServer.startup(KafkaServer.scala:191)
        at kafka.Kafka$.main(Kafka.scala:109)
        at kafka.Kafka.main(Kafka.scala)
[2021-10-08 10:03:59,723] INFO shutting down (kafka.server.KafkaServer)
[2021-10-08 10:03:59,743] INFO App info kafka.server for 0 unregistered (org.apache.kafka.common.utils.AppInfoParser)
[2021-10-08 10:03:59,743] INFO shut down completed (kafka.server.KafkaServer)
[2021-10-08 10:03:59,743] ERROR Exiting Kafka. (kafka.Kafka$)
[2021-10-08 10:03:59,752] INFO shutting down (kafka.server.KafkaServer)
错误根因

日志明确显示Kafka尝试连接本地2181端口的ZooKeeper服务时被拒绝,最终超时退出。旧版本Kafka依赖ZooKeeper存储集群元数据、节点状态等核心信息,必须先启动ZooKeeper服务,才能正常启动Kafka。

解决步骤
  • 第一步:启动ZooKeeper服务。在Kafka根目录打开命令行窗口,执行启动命令:
    .\bin\windows\zookeeper-server-start.bat .\config\zookeeper.properties
    等待ZooKeeper启动完成,日志中出现绑定2181端口的提示后,不要关闭该窗口。
  • 第二步:新开一个命令行窗口,进入Kafka根目录,重新执行Kafka启动命令即可:
    .\bin\windows\kafka-server-start.bat .\config\server.properties
验证方案

两个服务都启动完成后,可执行以下命令创建测试Topic,验证服务可用性:
.\bin\windows\kafka-topics.bat --create --topic test-demo --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1
如果无报错返回,说明服务运行正常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 14:57:03