在同一台 Windows 11 机器上模拟 3 节点 Kafka 集群 ,通过 不同端口 和 不同配置文件 来实现。
1. 准备工作
1.1 确保 Kafka 已安装
- 已安装 Java(本记录使用jdk21)
- 已安装 Kafka(本记录使用3.6.1版本)
1.2 创建 Kafka 数据目录
在 Kafka 安装目录或者其他地方(本记录参考:D:\work\kafka\kafka-cluster
)创建 3 个数据目录:
kotlin
D:\work\kafka\kafka-cluster\
├── broker1\
│ ├── data\ # Kafka 数据目录
│ └── logs\ # Kafka 日志目录
├── broker2\
│ ├── data\
│ └── logs\
└── broker3\
├── data\
└── logs\
2. 配置 ZooKeeper
先看效果图,有个预期
2.1 修改 zookeeper.properties
从kafka安装目录下的config下(本记录参照:D:\work\kafka\kafka_2.13\config
)复制 3 份 zookeeper.properties
,分别放在 broker1
、broker2
、broker3
目录下修改内容:
ini
# broker1/zookeeper.properties
# 2 秒(ZooKeeper 心跳时间单位)
tickTime=2000
# 允许 Follower 在 10 * 2000ms = 20s 内完成初始化同步
initLimit=10
# 如果 Leader 和 Follower 之间 5 * 2000ms = 10s 无心跳,则认为连接超时。
syncLimit=5
# # ZooKeeper 数据目录(需手动创建文件夹)
dataDir=D:/work/kafka/kafka-cluster/broker1/data
# the port at which the clients will connect
clientPort=2181
# disable the per-ip limit on the number of connections since this is a non-production config
maxClientCnxns=0
# Disable the adminserver by default to avoid port conflicts.
# Set the port to something non-conflicting if choosing to enable this
admin.enableServer=false
# admin.serverPort=8080
#
server.1=localhost:2888:3888
server.2=localhost:2889:3889
server.3=localhost:2890:3890
ini
# broker2/zookeeper.properties
# 2 秒(ZooKeeper 心跳时间单位)
tickTime=2000
# 允许 Follower 在 10 * 2000ms = 20s 内完成初始化同步
initLimit=10
# 如果 Leader 和 Follower 之间 5 * 2000ms = 10s 无心跳,则认为连接超时。
syncLimit=5
# # ZooKeeper 数据目录(需手动创建文件夹)
dataDir=D:/work/kafka/kafka-cluster/broker2/data
# the port at which the clients will connect
clientPort=2182
# disable the per-ip limit on the number of connections since this is a non-production config
maxClientCnxns=0
# Disable the adminserver by default to avoid port conflicts.
# Set the port to something non-conflicting if choosing to enable this
admin.enableServer=false
# admin.serverPort=8080
#
server.1=localhost:2888:3888
server.2=localhost:2889:3889
server.3=localhost:2890:3890
ini
# broker3/zookeeper.properties
# 2 秒(ZooKeeper 心跳时间单位)
tickTime=2000
# 允许 Follower 在 10 * 2000ms = 20s 内完成初始化同步
initLimit=10
# 如果 Leader 和 Follower 之间 5 * 2000ms = 10s 无心跳,则认为连接超时。
syncLimit=5
# # ZooKeeper 数据目录(需手动创建文件夹)
dataDir=D:/work/kafka/kafka-cluster/broker3/data
# the port at which the clients will connect
clientPort=2183
# disable the per-ip limit on the number of connections since this is a non-production config
maxClientCnxns=0
# Disable the adminserver by default to avoid port conflicts.
# Set the port to something non-conflicting if choosing to enable this
admin.enableServer=false
# admin.serverPort=8080
#
server.1=localhost:2888:3888
server.2=localhost:2889:3889
server.3=localhost:2890:3890
2.2 创建 myid
文件
在每个 dataDir
下(也就是data目录下,本记录参照:D:\work\kafka\kafka-cluster\broker1\data
)创建 myid
文件:

broker1/data/myid
→1
broker2/data/myid
→2
broker3/data/myid
→3
提醒:myid
文件的规则
在搭建 Kafka 集群(依赖 ZooKeeper 时)时,myid
文件是 ZooKeeper 用来标识集群中每个节点身份的关键文件。
-
文件名 :
必须命名为
myid
(无后缀名 ),不能是myid.txt
、myid.dat
等。 -
文件内容 :
仅包含一个数字(无空格、换行等),对应
zookeeper.properties
中server.x
的x
值。例如:
- 若配置
server.1=...
,则myid
文件内容为1
。 - 若配置
server.2=...
,则myid
文件内容为2
。
- 若配置
-
文件位置 :
再次强调必须放在
zookeeper.properties
中dataDir
指定的目录下(本记录参考D:\work\kafka\kafka-cluster\broker1\data
)
2.3 启动 ZooKeeper 集群
打开 3 个 CMD 窗口,分别运行:
makefile
# 窗口1(ZooKeeper 1)
D:\work\kafka\kafka_2.13\bin\windows\zookeeper-server-start.bat D:\work\kafka\kafka-cluster\broker1\zookeeper.properties
# 窗口2(ZooKeeper 2)
D:\work\kafka\kafka_2.13\bin\windows\zookeeper-server-start.bat D:\work\kafka\kafka-cluster\broker2\zookeeper.properties
# 窗口3(ZooKeeper 3)
D:\work\kafka\kafka_2.13\bin\windows\zookeeper-server-start.bat D:\work\kafka\kafka-cluster\broker3\zookeeper.properties
3. 配置 Kafka 集群
3.1 修改 server.properties
从kafka安装目录config下(本记录参照:D:\work\kafka\kafka_2.13\config
)复制 3 份 server.properties
,分别放在 broker1
、broker2
、broker3
目录下修改:
ini
# broker1/server.properties
broker.id=1
listeners=PLAINTEXT://localhost:9092
advertised.listeners=PLAINTEXT://localhost:9092
log.dirs=D:/work/kafka/kafka-cluster/broker1/logs
zookeeper.connect=localhost:2181,localhost:2182,localhost:2183
ini
# broker2/server.properties
broker.id=2
listeners=PLAINTEXT://localhost:9093
advertised.listeners=PLAINTEXT://localhost:9093
log.dirs=D:/work/kafka/kafka-cluster/broker2/logs
zookeeper.connect=localhost:2181,localhost:2182,localhost:2183
ini
# broker3/server.properties
broker.id=3
listeners=PLAINTEXT://localhost:9094
advertised.listeners=PLAINTEXT://localhost:9094
log.dirs=D:/work/kafka/kafka-cluster/broker3/logs
zookeeper.connect=localhost:2181,localhost:2182,localhost:2183
3.2 启动 Kafka 集群
再打开 3 个 CMD 窗口,分别运行:
makefile
# 窗口4(Kafka Broker 1)
D:\work\kafka\kafka_2.13\bin\windows\kafka-server-start.bat D:\work\kafka\kafka-cluster\broker1\server.properties
# 窗口5(Kafka Broker 2)
D:\work\kafka\kafka_2.13\bin\windows\kafka-server-start.bat D:\work\kafka\kafka-cluster\broker2\server.properties
# 窗口6(Kafka Broker 3)
D:\work\kafka\kafka_2.13\bin\windows\kafka-server-start.bat D:\work\kafka\kafka-cluster\broker3\server.properties
4. 验证集群
4.1 查看 Broker 列表
在D:\work\kafka\kafka_2.13
目录下新启一个命令窗口--窗口7,运行
css
bin\windows\kafka-broker-api-versions.bat --bootstrap-server localhost:9092
应该看到 3 个 Broker(broker.id=1,2,3
)。
4.2 创建 Topic
窗口7,运行
css
bin\windows\kafka-topics.bat --create --topic test --partitions 3 --replication-factor 3 --bootstrap-server localhost:9092
4.3 查看 Topic 详情
窗口7,运行
css
bin\windows\kafka-topics.bat --describe --topic test --bootstrap-server localhost:9092
输出示例:
yaml
Topic: test PartitionCount: 3 ReplicationFactor: 3 Configs:
Topic: test Partition: 0 Leader: 1 Replicas: 1,2,3 Isr: 1,2,3
Topic: test Partition: 1 Leader: 2 Replicas: 2,3,1 Isr: 2,3,1
Topic: test Partition: 2 Leader: 3 Replicas: 3,1,2 Isr: 3,1,2
说明分区均匀分布在 3 个 Broker 上。
5. 测试生产 & 消费
5.1 启动生产者
窗口7,运行
css
bin\windows\kafka-console-producer.bat --topic test --bootstrap-server localhost:9092
5.2 启动消费者
在D:\work\kafka\kafka_2.13
目录下新启一个命令窗口--窗口8,运行
css
bin\windows\kafka-console-consumer.bat --topic test --from-beginning --bootstrap-server localhost:9092
输入消息后,消费者应该能收到。
6. 关闭集群
按 Ctrl + C
依次关闭:
- 3 个 Kafka Broker
- 3 个 ZooKeeper
7. 数据管理工具总览
本记录使用kafka-console-ui。可以链接地址下载
启动完成,访问:http://127.0.0.1:7766
还有一些其他使用信息,直接参考kafka-console-ui项目首页提示信息
8. 启动脚本优化
每次启动带ui要7个脚本。要开7个窗口,贼麻烦。
本记录直接合并为一个脚本,一键启动。
目标是打开一个窗口,通过7个选项卡起别名区分颜色来管理

脚本命令参考:
bash
@echo off
wt -p "Command Prompt" --title "2181-zb1" --tabColor "#54FF3C" cmd /k "D:\work\kafka\kafka_2.13\bin\windows\zookeeper-server-start.bat D:\work\kafka\kafka-cluster\broker1\zookeeper.properties & pause" ; ^
-p "Command Prompt" --title "2182-zb1" --tabColor "#54FF3C" cmd /k "D:\work\kafka\kafka_2.13\bin\windows\zookeeper-server-start.bat D:\work\kafka\kafka-cluster\broker2\zookeeper.properties & pause" ; ^
-p "Command Prompt" --title "2183-zb1" --tabColor "#54FF3C" cmd /k "D:\work\kafka\kafka_2.13\bin\windows\zookeeper-server-start.bat D:\work\kafka\kafka-cluster\broker3\zookeeper.properties & pause" ; ^
-p "Command Prompt" --title "9092-kafka-b1" --tabColor "#4584FF" cmd /k "timeout /t 7 /nobreak >nul & D:\work\kafka\kafka_2.13\bin\windows\kafka-server-start.bat D:\work\kafka\kafka-cluster\broker1\server.properties & pause" ; ^
-p "Command Prompt" --title "9093-kafka-b2" --tabColor "#4584FF" cmd /k "timeout /t 7 /nobreak >nul & D:\work\kafka\kafka_2.13\bin\windows\kafka-server-start.bat D:\work\kafka\kafka-cluster\broker2\server.properties & pause" ; ^
-p "Command Prompt" --title "9094-kafka-b3" --tabColor "#4584FF" cmd /k "timeout /t 7 /nobreak >nul & D:\work\kafka\kafka_2.13\bin\windows\kafka-server-start.bat D:\work\kafka\kafka-cluster\broker3\server.properties & pause" ; ^
-p "Command Prompt" --title "kafka-ui" --tabColor "#A24BFF" cmd /k "timeaout /t 7 /nobreak >nul & cd /d D:\work\kafka\kafka-ui\kafka-console-ui\bin & start.bat & pause"
脚本建立放在kafka安装目录下,用的时候方便找。