1、配置
kafka.consumer.auto.offset.reset=earliest
What to do when there is no initial offset in Kafka or if the current offset does not exist any more on the server (e.g. because that data has been deleted):
- earliest: automatically reset the offset to the earliest offset
- latest: automatically reset the offset to the latest offset
- none: throw exception to the consumer if no previous offset is found for the consumer's group
- anything else: throw exception to the consumer.
当无法获取当前offset的时候该怎么处理,这个配置项的默认值是“latest”,而当我们新建一个group对主题订阅的时候,第一次无法知道当前的offset值,这个时候就触发了“latest”这个配置值对应的操作,也就是说把当前topic里面最新的偏移作为offset,那显然,该消费者是读不到主题中的历史信息的,于是把配置的值改为“earliest”,即可消费到历史消息。
注意:
这个配置只在group第一次订阅主题的时候触发,一旦这个offset值被确定下来了,你再把这个配置改成“earliest”就没效果了,因为他已经不符合这个条件了(这个group在这个主题下已经能拿到offset值了)