Kafka 的配置可以分为单机(Standalone)和集群(Cluster)两种场景。下面我按从零开始、最常用的方式给你一个完整、清晰的 Kafka 配置步骤,适用于 Linux / macOS / Windows(WSL)。
Kafka 依赖 Java:
java -version
推荐:
官网:https://kafka.apache.org/downloads
wget https://downloads.apache.org/kafka/3.6.1/kafka_2.13-3.6.1.tgz
tar -xzf kafka_2.13-3.6.1.tgz
cd kafka_2.13-3.6.1
Kafka 主要有两个核心配置文件:
| 文件 | 作用 |
|---|---|
server.properties |
Kafka Broker 配置 |
zookeeper.properties |
Zookeeper 配置(Kafka 2.x / 3.x 仍常用) |
vim config/zookeeper.properties
关键配置:
dataDir=/tmp/zookeeper
clientPort=2181
bin/zookeeper-server-start.sh config/zookeeper.properties
✅ Zookeeper 默认端口:2181
server.propertiesvim config/server.properties
必须配置项:
broker.id=0
listeners=PLAINTEXT://localhost:9092
log.dirs=/tmp/kafka-logs
zookeeper.connect=localhost:2181
常见可选配置:
num.partitions=3
default.replication.factor=1
offsets.topic.replication.factor=1
bin/kafka-server-start.sh config/server.properties
✅ Kafka 默认端口:9092
jps
应看到:
QuorumPeerMain # Zookeeper
Kafka # Kafka
bin/kafka-topics.sh \
--create \
--topic test-topic \
--bootstrap-server localhost:9092 \
--partitions 3 \
--replication-factor 1
查看 Topic:
bin/kafka-topics.sh --list --bootstrap-server localhost:9092
bin/kafka-console-producer.sh \
--topic test-topic \
--bootstrap-server localhost:9092
输入消息:
hello kafka
bin/kafka-console-consumer.sh \
--topic test-topic \
--from-beginning \
--bootstrap-server localhost:9092
✅ 能看到刚才发送的消息
cp config/server.properties config/server-1.properties
cp config/server.properties config/server-2.properties
broker.id=1
listeners=PLAINTEXT://localhost:9093
log.dirs=/tmp/kafka-logs-1
broker.id=2
listeners=PLAINTEXT://localhost:9094
log.dirs=/tmp/kafka-logs-2
bin/kafka-server-start.sh config/server-1.properties
bin/kafka-server-start.sh config/server-2.properties
✅ 新版本推荐,但生产需谨慎
process.roles=broker,controller
node.id=1
controller.quorum.voters=1@localhost:9093
启动方式不同,需要 kafka-storage.sh 初始化。
| 配置项 | 说明 |
|---|---|
| broker.id | Broker 唯一 ID |
| listeners | 客户端连接地址 |
| log.dirs | 消息存储目录 |
| zookeeper.connect | ZK 地址 |
| num.partitions | 默认分区数 |
| replication.factor | 副本数 |
listeners 配置错误如果你愿意,我可以:
你现在是 学习 / 面试 / 生产环境 哪种场景?