首页 文章

为什么我的 kafka 在一个分区中有消息?

提问于
浏览
6

我有两个 kafka 代理在本地机器上有两个分区,并使用以下工具将一个本地文件写入 kafka test2 主题。

# create topic
  ./kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 2 --partitions 2 --topic test2
  Created topic "test2".
  # write 15MB file to kafka, very fast!!
  kafka-console-producer.sh --broker-list localhost:9093,localhost:9094 --topic test2 < data.txt
  # read data from kafka
  ./kafka-console-consumer.sh --zookeeper localhost:2181 --topic test2 --from-beginning

然后我发现所有消息都在一个分区中,如何调试呢?

$ kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list localhost:9093,localhost:9094 --topic test2 --time -1
    SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
    SLF4J: Defaulting to no-operation (NOP) logger implementation
    SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
    test2:0:68263
    test2:1:0

分区的状态是:

$ kafka-topics.sh --describe --zookeeper localhost:2181 --topic test2
Topic:test2 PartitionCount:2    ReplicationFactor:2 Configs:
    Topic: test2    Partition: 0    Leader: 1   Replicas: 1,2   Isr: 1,2
    Topic: test2    Partition: 1    Leader: 2   Replicas: 2,1   Isr: 2,1

1 回答

  • -1

    如果我理解正确,您想知道为什么数据不会复制到其他分区。我想你可能会误解 Kafka 如何进行复制。

    根据卡夫卡文档,只写入主题中的一个分区:

    生产者将数据发布到他们选择的主题。生产者负责选择分配给主题中哪个分区的记录。

    因此,复制不会将数据从一个分区镜像到另一个分区,而是将每个分区的副本保存到另一个服务器。

    Kafka 在可配置数量的服务器上复制每个主题分区的日志(您可以基于 topic-by-topic 设置此复制因子)。这允许在群集中的服务器发生故障时自动故障转移到这些副本,以便在出现故障时消息仍然可用。

    因此,最后所有数据都写入一个分区,但两个服务器上都有该分区的副本。

相关问题