概念

消费者消费完了消息是不会被立刻删除的,每个消费者把消费的数据消费到哪里了就需要做个记录,就是offset
Kafka 0.9版本之前,consumer默认将offset保存在Zookeeper中,从0.9版本开始,consumer默认将offset保存在Kafka一个内置的topic中,该topic为”__consumer_offsets” 。

由于consumer在消费过程中可能会出现断电宕机等故障,consumer恢复后,需要从故障前的位置的继续消费,所以consumer需要实时记录自己消费到了哪个offset,以便故障恢复后继续消费。

image.png

为什么Kafka后期将offset挪到”__consumer_offsets” topic了,

为什么Kafka后期将offset挪到”__consumer_offsets” topic了

因为zookeeper相对于kafka来说是一个外部框架,kafka在不断的优化发现读写瓶颈出现在offset的保存中了.再给offset保存在zookeeper中就发现Kafka吞吐量很难提升了,优化起来没法优化,因为Zookeeper是外部框架,你没法改人家源码.
为了让后期维护优化自己框架更容易,就给offset保存在自己内部的”__consumer_offsets” topic了

配置


1)修改配置文件consumer.properties
exclude.internal.topics=false
2)读取offset
0.11.0.0之前版本:
bin/kafka-console-consumer.sh —topic consumer_offsets —zookeeper hadoop102:2181 —formatter “kafka.coordinator.GroupMetadataManager\$OffsetsMessageFormatter” —consumer.config config/consumer.properties —from-beginning
0.11.0.0之后版本(含):
bin/kafka-console-consumer.sh —topic
consumer_offsets —zookeeper hadoop102:2181 —formatter “kafka.coordinator.group.GroupMetadataManager\$OffsetsMessageFormatter” —consumer.config config/consumer.properties —from-beginning