在 Ubuntu 上修改 Kafka 配置,通常分为修改配置文件和重启 Kafka 服务两步。下面按常见场景说明。
常见路径:
/etc/kafka/server.properties
/etc/kafka/server.properties
/usr/share/kafka/config/server.properties
一般在解压目录:
/opt/kafka/config/server.properties
~/kafka/config/server.properties
sudo vim /etc/kafka/server.properties
# 或
sudo nano /etc/kafka/server.properties
# broker 唯一 ID
broker.id=0
# 监听地址(改成本机 IP 或 0.0.0.0)
listeners=PLAINTEXT://0.0.0.0:9092
advertised.listeners=PLAINTEXT://your_ip:9092
# 日志存储目录
log.dirs=/var/lib/kafka/logs
# Zookeeper 地址(Kafka 2.x)
zookeeper.connect=localhost:2181
# Kafka 3.x(KRaft 模式)
process.roles=broker,controller
controller.quorum.voters=0@localhost:9093
修改后保存退出。
sudo systemctl restart kafka
sudo systemctl status kafka
如果是 zookeeper:
sudo systemctl restart zookeeper
# 先停
sudo systemctl stop kafka
# 或
ps -ef | grep kafka
kill -9 <pid>
# 启动
/opt/kafka/bin/kafka-server-start.sh -daemon /opt/kafka/config/server.properties
# 查看 topic 列表
kafka-topics.sh --list --bootstrap-server localhost:9092
# 测试生产/消费
kafka-console-producer.sh --topic test --bootstrap-server localhost:9092
kafka-console-consumer.sh --topic test --from-beginning --bootstrap-server localhost:9092
server.properties 必须重启 Kafkaadvertised.listeners 必须能被客户端访问broker.id 不能重复如果你能告诉我:
我可以给你更精确的配置示例。