创建主题:分区数决定并行度,副本数不超过 broker 数量
kafka-topics.sh --bootstrap-server localhost:9092 --create --topic orders --partitions 3 --replication-factor 2Kafka 3.x 常用运维命令:主题创建与分区扩容、控制台生产与消费、消费组积压查看与偏移重置、生产消费压测,以及 bootstrap-server 与日志目录等基础排查项。
创建主题:分区数决定并行度,副本数不超过 broker 数量
kafka-topics.sh --bootstrap-server localhost:9092 --create --topic orders --partitions 3 --replication-factor 2列出全部主题,排查时先确认主题名是否拼写正确
kafka-topics.sh --bootstrap-server localhost:9092 --list查看分区、副本分布与 ISR,ISR 少于副本数说明有 follower 掉队
kafka-topics.sh --bootstrap-server localhost:9092 --describe --topic orders分区只能增加不能减少,增加后按 key 的哈希分布会变化
kafka-topics.sh --bootstrap-server localhost:9092 --alter --topic orders --partitions 6删除主题及其数据,需同时开启 delete.topic.enable
kafka-topics.sh --bootstrap-server localhost:9092 --delete --topic orders交互式生产消息,每行一条,Ctrl+C 结束
kafka-console-producer.sh --bootstrap-server localhost:9092 --topic orders带 key 生产,形如 order-1:{"id":1},便于按 key 保证分区顺序
kafka-console-producer.sh --bootstrap-server localhost:9092 --topic orders --property parse.key=true --property key.separator=:从头消费全部历史消息,只适合调试,大主题会拉取很久
kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic orders --from-beginning指定消费组并打印 key 与分区,用于确认消息落在哪个分区
kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic orders --group debug-group --property print.key=true --property print.partition=true带 --group 会提交位点,可能影响同名线上消费组的进度
列出所有消费组,确认业务组名后再查询积压
kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list查看 LAG 列可判断积压程度,CONSUMER-ID 为空说明没有活跃消费者
kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group order-service把位点重置到最早,必须停掉消费组再执行,否则会与运行中消费者冲突
kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group order-service --topic orders --reset-offsets --to-earliest --execute删除消费组,仅对无活跃消费者的组生效
kafka-consumer-groups.sh --bootstrap-server localhost:9092 --delete --group debug-group生产端压测,throughput -1 表示不限速,输出吞吐与延迟分位
kafka-producer-perf-test.sh --topic orders --num-records 100000 --record-size 1024 --throughput -1 --producer-props bootstrap.servers=localhost:9092 acks=1消费端压测,测量拉取吞吐,结果含 MB/sec 与消息条数
kafka-consumer-perf-test.sh --topic orders --bootstrap-server localhost:9092 --messages 100000 --group perf-group不带 --topic 时汇总全部主题的分区与副本状态,快速定位异常
kafka-topics.sh --bootstrap-server localhost:9092 --describe查看主题级配置,如 retention.ms、cleanup.policy 是否被覆盖
kafka-configs.sh --bootstrap-server localhost:9092 --describe --entity-type topics --entity-name orders查看各 broker 上主题的日志目录与占用,磁盘告警时先看这里
kafka-log-dirs.sh --bootstrap-server localhost:9092 --describe --topic-list orders本网站使用 Cookie 以确保您在我们的网站上获得最佳体验。 隐私政策.