作者:じ☆ve不哭
发布时间:2025-09-19T16:38:20
wget https://downloads.apache.org/kafka/4.0.0/kafka_2.13-4.0.0.tgz
tar -xzf kafka_2.13-4.0.0.tgz
cd kafka_2.13-4.0.0
1、生成集群唯一标识符(Cluster ID) Kafka 4.0 完全移除了对 ZooKeeper 的依赖,因此我们只需要配置 KRaft。
./bin/kafka-storage.sh random-uuid
# 输出类似:cELBcqLNQAuEvTfQgdX4_A
# 复制这个 UUID,下一步会用到。
2、修改config/server.properties 最终配置如下
broker.id=1
process.roles=broker,controller
node.id=1
controller.quorum.voters=1@localhost:9093
listeners=PLAINTEXT://0.0.0.0:9092,CONTROLLER://0.0.0.0:9093
inter.broker.listener.name=PLAINTEXT
advertised.listeners=PLAINTEXT://123.56.190.57:9092
controller.listener.names=CONTROLLER
listener.security.protocol.map=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT,SSL:SSL,SASL_PLAINTEXT:SASL_PLAINTEXT,SASL_SSL:SASL_SSL
num.network.threads=3
num.io.threads=8
socket.send.buffer.bytes=102400
socket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
log.dirs=/tmp/kraft-combined-logs
num.partitions=1
num.recovery.threads.per.data.dir=1
offsets.topic.replication.factor=1
share.coordinator.state.topic.replication.factor=1
share.coordinator.state.topic.min.isr=1
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=1
log.retention.hours=168
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
3、使用上一步生成的 UUID 来初始化存储元数据的日志目录。
./bin/kafka-storage.sh format -t <uuid> -c ./config/server.properties
# 例如:
./bin/kafka-storage.sh format -t QDK7gwhnQT-w_iputukn6w -c ./config/server.properties
4、启动
# 后台启动
bin/kafka-server-start.sh -daemon config/server.properties
# 或者前台启动(查看日志)
bin/kafka-server-start.sh config/server.properties
1. 创建一个 Topic 打开另一个终端,执行:
./bin/kafka-topics.sh --create --topic test-topic --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1
2. 生产一些消息
./bin/kafka-console-producer.sh --topic test-topic --bootstrap-server localhost:9092
> Hello Kafka 4.0!
> This is KRaft mode.
3. 消费这些消息
./bin/kafka-console-consumer.sh --topic test-topic --from-beginning --bootstrap-server localhost:9092