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

跨主机部署的Kafka生产者与消费者无法通信如何解决?

Kafka双节点集群无法收发消息问题排查与解决

环境与配置信息

我使用的是kafka_2.11-1.1.0版本,基于两台Windows主机搭建集群:

  • 第一台主机server.properties核心配置:
broker.id=1
num.network.threads=3
num.io.threads=8
socket.send.buffer.bytes=102400
socket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
log.dirs=/tmp/kafka-logs
num.partitions=1
num.recovery.threads.per.data.dir=1
offsets.topic.replication.factor=1
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=1
log.retention.hours=168
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
zookeeper.connect=192.168.1.110:2181,192.168.1.64:2181
zookeeper.connection.timeout.ms=6000
group.initial.rebalance.delay.ms=0
  • 第二台主机broker.id=2,zookeeper.connect配置使用两台主机的以太网+无线网卡IPv4地址。

操作步骤

两台主机依次启动服务:

bin\windows\zookeeper-server-start.bat config\zookeeper.properties
bin\windows\kafka-server-start.bat config\server.properties

在第一台主机创建Topic并启动生产者:

bin\windows\kafka-topics.bat --create --zookeeper 192.168.1.110:2181 --replication-factor 2 --partitions 1 --topic test
bin\windows\kafka-console-producer.bat --broker-list 192.168.1.110:2181 --topic test

在第二台主机启动消费者:

bin\windows\kafka-console-consumer.bat --bootstrap-server 192.168.1.64:2181 --topic test

问题现象

  • 发送消息时消费者完全无法接收
  • ZooKeeper控制台循环输出如下日志:
INFO Accepted socket connection from /192.168.1.110:55371 (org.apache.zookeeper.server.NIOServerCnxnFactory)
WARN Exception causing close of session 0x0 due to java.io.EOFException (org.apache.zookeeper.server.NIOServerCnxn)
INFO Closed socket connection for client /192.168.1.110:55371 (no session established for client) (org.apache.zookeeper.server.NIOServerCnxn)
  • 一分钟后生产者控制台抛出超时错误:
ERROR Error when sending message to topic test with key: null, value: 6 bytes with error: (org.apache.kafka.clients.producer.internals.ErrorLoggingCallback)
org.apache.kafka.common.errors.TimeoutException: Failed to update metadata after 60000 ms.

解决过程与方案

临时修复(已验证有效)

感谢Victor的提示,我发现核心问题是生产者和消费者命令里错误地使用了ZooKeeper的端口2181,而不是Kafka Broker的默认端口9092。

修改后的生产者命令:

bin\windows\kafka-console-producer.bat --broker-list 192.168.1.110:9092 --topic test

修改后的消费者命令:

bin\windows\kafka-console-consumer.bat --bootstrap-server 192.168.1.64:9092 --topic test

修改后消息可以正常收发。

永久优化方案

不过上述方法在某些场景下可能不稳定,我找到更可靠的永久解决方案:两台主机的生产者/消费者命令统一使用生产者所在主机的以太网IP地址(搭配9092端口),避免因多网卡IP导致的连接问题。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 06:39:30