我有两个 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 回答
如果我理解正确,您想知道为什么数据不会复制到其他分区。我想你可能会误解 Kafka 如何进行复制。
根据卡夫卡文档,只写入主题中的一个分区:
因此,复制不会将数据从一个分区镜像到另一个分区,而是将每个分区的副本保存到另一个服务器。
因此,最后所有数据都写入一个分区,但两个服务器上都有该分区的副本。