Skip to content
鼓励作者:欢迎打赏犒劳

kafka

安装教程:https://software.share888.top/note/devtool/detail/kafka.html

如果你执行命令的时候发现总出现错误,但是又没什么影响,我建议你,忽略它!!!!这个错误是 Kafka 命令行工具在初始化日志系统时的一个常见问题,尤其是在 Windows 环境下使用 Kafka 3.3+ 或 4.x 版本时。

shell
D:\devtool\kafka_2.13-4.1.0>bin\windows\kafka-topics.bat --list --bootstrap-server localhost:9092
2025-10-29T15:29:40.122108600Z main ERROR Reconfiguration failed: No configuration found for '24d46ca6' at 'null' in 'null'
my-topic

创建 Topic

shell
bin\windows\kafka-topics.bat --create --topic my-topic --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1

列出所有 Topic

shell
bin\windows\kafka-topics.bat --list --bootstrap-server localhost:9092

查看 Topic 详情

shell
bin\windows\kafka-topics.bat --describe --topic my-topic --bootstrap-server localhost:9092

你应该看到类似:

shell
Topic: my-topic PartitionCount: 1 ReplicationFactor: 1
    Partition: 0 Leader: 1 Replicas: 1 Isr: 1
  • Leader: 1 表示节点 ID 为 1 的 Broker 是 Leader。
  • Isr: 1 表示它也在同步副本中。

如果 Leader 显示为 -1 或为空,说明分区没有 Leader,Broker 未正常工作。

启动控制台生产者(发送消息)

shell
bin\windows\kafka-console-producer.bat --topic my-topic --bootstrap-server localhost:9092

启动控制台消费者(接收消息)

注意:如果你启动消费者,没有携带group参数的话,则默认会给你生成一个group名字。Kafka 会 自动生成一个唯一的消费者组 ID。

这个默认的组名通常是:console-consumer- + 一串随机数字

shell
## 注意 没有携带group
bin\windows\kafka-console-consumer.bat --topic my-topic --from-beginning --bootstrap-server localhost:9092

测试同一个组

shell
# 窗口1
bin\windows\kafka-console-consumer.bat --topic my-topic --from-beginning --bootstrap-server localhost:9092 --group my-group

# 窗口2
bin\windows\kafka-console-consumer.bat --topic my-topic --from-beginning --bootstrap-server localhost:9092 --group my-group

你会发现:消息被两个消费者“瓜分”,每条消息只被其中一个收到 → 负载均衡。

⚠️ 注意:--from-beginning 的作用

  • --from-beginning 只对 新消费者组 有效。
  • 如果你使用同一个 --group 多次启动,Kafka 会从上次消费的位置(offset)继续读,不会重复读旧消息。
  • 但如果你不指定 --group,每次都是“新组”,所以每次都从头开始。

如有转载或 CV 的请标注本站原文地址