下面按在 Ubuntu 上配置 Kafka的常见步骤和关键配置点来说明,适合单机/伪分布式起步,也包含生产环境常用项。
# 安装 Java(Kafka 需要)
sudo apt update
sudo apt install openjdk-11-jdk -y
java -version
下载 Kafka(以 3.x 为例):
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 主要配置文件在:
config/
├── server.properties # Broker 配置(最重要)
├── zookeeper.properties # Zookeeper(老版本需要)
└── producer.properties / consumer.properties
config/server.properties# Broker 唯一 ID
broker.id=0
# 监听地址(本机)
listeners=PLAINTEXT://:9092
advertised.listeners=PLAINTEXT://localhost:9092
# 日志(消息存储)目录
log.dirs=/tmp/kafka-logs
# Zookeeper(Kafka 3.x 仍可用)
zookeeper.connect=localhost:2181
# 自动创建 topic(方便测试)
auto.create.topics.enable=true
bin/zookeeper-server-start.sh config/zookeeper.properties
bin/kafka-server-start.sh config/server.properties
(后台运行加 -daemon)
listeners=PLAINTEXT://0.0.0.0:9092
advertised.listeners=PLAINTEXT://你的IP:9092
num.partitions=3
default.replication.factor=1
log.retention.hours=168
log.segment.bytes=1073741824
num.network.threads=3
num.io.threads=8
config/kraft/server.propertiesprocess.roles=broker,controller
node.id=1
controller.quorum.voters=1@localhost:9093
listeners=PLAINTEXT://:9092,CONTROLLER://:9093
advertised.listeners=PLAINTEXT://localhost:9092
初始化:
bin/kafka-storage.sh format -t <cluster-id> -c config/kraft/server.properties
bin/kafka-server-start.sh config/kraft/server.properties
✅ 端口被占用
netstat -tunlp | grep 9092
✅ 无法连接
advertised.listenerssudo ufw allow 9092如果你愿意,我可以:
你现在是 学习 / 测试 / 生产环境?