Apache Kafka 是一个分布式流处理平台,广泛应用于日志收集、消息系统、事件溯源等场景。本文将手把手教你如何在 Windows 和 Linux 环境下搭建 Kafka 集群,并附上常见问题与解决方案,助你避开新手常踩的“坑”。
一、准备工作
在开始之前,请确保你的机器满足以下条件:
- 已安装 Java 8 或 Java 11(Kafka 依赖 JVM)
- 至少 2GB 内存(建议 4GB+)
- 下载 Kafka 安装包:官方下载地址
二、单机伪集群搭建(适用于学习测试)
我们将在一台机器上模拟 3 个 Kafka 节点 + 1 个 ZooKeeper(Kafka 2.8+ 支持 KRaft 模式,但本教程仍使用经典 ZooKeeper 模式以兼容更多版本)。
1. 解压 Kafka
# Linux / macOS$ tar -xzf kafka_2.13-3.6.0.tgzcd kafka_2.13-3.6.0# Windows(使用 PowerShell 或解压工具)Expand-Archive kafka_2.13-3.6.0.tgz -DestinationPath .\kafka 2. 启动 ZooKeeper
Kafka 依赖 ZooKeeper 管理集群元数据(3.3+ 版本可选 KRaft,但初学者建议先用 ZooKeeper)。
# Linux / macOS$ bin/zookeeper-server-start.sh config/zookeeper.properties# Windows> .\bin\windows\zookeeper-server-start.bat .\config\zookeeper.properties 3. 配置多个 Kafka 实例
复制配置文件,创建 3 个 broker 配置:
# Linux / macOS$ cp config/server.properties config/server-1.properties$ cp config/server.properties config/server-2.properties$ cp config/server.properties config/server-3.properties# Windows(PowerShell)Copy-Item config\server.properties config\server-1.propertiesCopy-Item config\server.properties config\server-2.propertiesCopy-Item config\server.properties config\server-3.properties 分别编辑这三个文件,修改以下关键参数(以 server-1.properties 为例):
# server-1.propertiesbroker.id=1listeners=PLAINTEXT://:9092log.dirs=/tmp/kafka-logs-1# server-2.propertiesbroker.id=2listeners=PLAINTEXT://:9093log.dirs=/tmp/kafka-logs-2# server-3.propertiesbroker.id=3listeners=PLAINTEXT://:9094log.dirs=/tmp/kafka-logs-3 ⚠️ Kafka避坑指南:Windows 用户注意路径分隔符!log.dirs 应使用正斜杠或双反斜杠,例如:C:\\kafka\\logs-1或C:/kafka/logs-1。
4. 启动三个 Kafka Broker
# Linux / macOS(每个命令在新终端运行)$ bin/kafka-server-start.sh config/server-1.properties$ bin/kafka-server-start.sh config/server-2.properties$ bin/kafka-server-start.sh config/server-3.properties# Windows(每个命令在新 CMD/PowerShell 窗口运行)> .\bin\windows\kafka-server-start.bat .\config\server-1.properties> .\bin\windows\kafka-server-start.bat .\config\server-2.properties> .\bin\windows\kafka-server-start.bat .\config\server-3.properties 三、验证集群是否正常
创建一个测试 Topic 并查看其分区分布:
# 创建 topic(3 分区,2 副本)$ bin/kafka-topics.sh --create --topic test-cluster \ --bootstrap-server localhost:9092,localhost:9093,localhost:9094 \ --partitions 3 --replication-factor 2# 查看 topic 详情$ bin/kafka-topics.sh --describe --topic test-cluster \ --bootstrap-server localhost:9092 如果看到类似以下输出,说明集群工作正常:
Topic: test-cluster PartitionCount: 3 ReplicationFactor: 2 Topic: test-cluster Partition: 0 Leader: 1 Replicas: 1,2 Isr: 1,2 Topic: test-cluster Partition: 1 Leader: 2 Replicas: 2,3 Isr: 2,3 Topic: test-cluster Partition: 2 Leader: 3 Replicas: 3,1 Isr: 3,1 四、常见问题与避坑手册
❌ 问题1:启动 Kafka 报错 “Address already in use”
原因:端口被占用(如 9092 已被其他服务使用)。
解决:修改 listeners 端口,或关闭占用端口的进程。
❌ 问题2:Windows 下路径错误导致无法写入日志
原因:Kafka 配置中的 log.dirs 使用了非法路径格式。
解决:使用绝对路径并确保目录存在,例如:log.dirs=C:/kafka/data/logs-1,并在启动前手动创建该目录。
❌ 问题3:ZooKeeper 连接超时
原因:防火墙阻止、ZooKeeper 未启动、或 Kafka 配置中 zookeeper.connect 地址错误。
解决:检查 ZooKeeper 是否运行(默认端口 2181),确保 zookeeper.connect=localhost:2181 配置正确。
❌ 问题4:副本数(replication-factor)大于 Broker 数量
原因:创建 Topic 时 replication-factor=3,但只有 2 个 Broker 在线。
解决:确保 replication-factor ≤ 在线 Broker 数量。本教程有 3 个 Broker,所以最大可设为 3。
五、总结
通过本文,你已经掌握了在 Windows 和 Linux 环境下搭建 Kafka 伪集群的核心步骤,并了解了常见的 Kafka避坑指南。虽然这是单机模拟,但配置逻辑与生产环境一致。后续可扩展到多台物理机,只需调整 IP 和端口即可。
记住:Kafka 的稳定运行依赖于良好的网络、磁盘 I/O 和 JVM 调优。建议在生产环境中使用专用服务器,并监控 Kafka 集群状态。
祝你搭建顺利!如有疑问,欢迎留言交流。
