背景:因下游服务还没做好,所以下游服务暂时停机了,上游一直往kafka发送消息,打算下游上线时能直接接上停机前的位置,于是把topic的retention.ms
设置成了2周,保证log不过期,过了两天想查看下topic堆积情况(怎么查看可看另一篇文章),结果根据group查不到分区与消费者绑定关系了,offset信息就也不存在了
[xuhaixing@xhx151 kafka_2.13-2.7.0]$ kafka-consumer-groups.sh --bootstrap-server http://192.168.94.151:9092 --describe --group goods-sync-group
Error: Consumer group 'goods-sync-group' does not exist.
抱着试一试的态度,把消费者启动了下,真的是从最新位置开始消费的。
问题分析
当时只把日志过期时间调大了,以为消费者的offset信息会一直保存直到日志过期,后来经查看资料,offset过期时间也是单独配置的offsets.retention.minutes
。
官网有如下描述,2.0.0版本后,offset过期时间由1天变成了7天。
Notable changes in 2.0.0
KIP-186 increases the default offset retention time from 1 day to 7 days. This makes it less likely to “lose” offsets in an application that commits infrequently. It also increases the active set of offsets and therefore can increase memory usage on the broker. Note that the console consumer currently enables offset commit by default and can be the source of a large number of offsets which this change will now preserve for 7 days instead of 1. You can preserve the existing behavior by setting the broker config offsets.retention.minutes to 1440.
所以,日志不过期并不能保证消费者停止后,还能从以前的offset继续消费。
解决
- 消费者启动时,指定从某个offset开始消费,或者从某个时间戳开始消费。
- 消费者pause住,然后再resume,此种情况下消费者不消费消息,但是一直和broker间有心跳。
- 调大broker端参数
offsets.retention.minutes
,可以保持和log过期时间一致
当时想投机取巧一下不想改代码,没想到还是改代码了。
更多优质内容,请关注公众号:程序员星星toC