kafka重复消费问题

事件经过

自己本地写了一个kafka的demo,起了一个消费者

@KafkaListener(topics = KafkaConstants.SMS_TOPIC_NAME, groupId = "sms")
    public void listenGroup(ConsumerRecord<String, String> record, Acknowledgment ack){

        String value = record.value();
        System.out.println("value1:" + value);
        System.out.println("record1:" + record);

        SmsThreadPool.submit(value);
        ack.acknowledge();
    }

但是如果过两天,offset就会从0开始重新消费。

解决

kafka官方文档https://kafka.apache.org/documentation.html#upgrade

  • offsets.retention.minutes

    After a consumer group loses all its consumers (i.e. becomes empty) its offsets will be kept for this retention period before getting discarded. For standalone consumers (using manual assignment), offsets will be expired after the time of last commit plus this retention period.

    Type:int
    Default:10080
    Valid Values:[1,...]
    Importance:high
    Update Mode:read-only

当一个消费组的所有消费者断开链接后,offset的位移量会保留的时间,默认10080分钟,即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.

2.0.0版本之后,默认值是7天,之前版本是1天。

我的本地版本是1.0.1,所以保留时间是1天,推测是2天后启动,offset就被重置了。

所以安全起见,修改配置文件,将offsets.retention.minutes=10080,与log.retention.hours默认的168(7天)一致。文件路径:kafka/config/server.properties

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Kafka重复消费指的是消费者可能会在某些情况下重复消费相同的消息。Kafka 是一个分布式消息系统,它的消费者组可以有多个消费者同时消费同一个主题的消息。当某个消费消费一条消息时,Kafka 就会将该消息的 offset 值保存在消费者组的 offset 管理器中。这样,其他消费者就可以从该 offset 值开始消费后续的消息。 Kafka重复消费可能发生在以下几种情况下: 1. 消费者手动重置 offset:如果消费者手动将 offset 重置到之前已经消费过的位置,那么接下来消费者就会重新消费之前已经消费过的消息。 2. 消费者组 rebalance:当消费者组发生 rebalance 时,消费者会重新分配分区。如果一个消费者之前消费了某个分区的消息,但在 rebalance 后该分区被分配给了另一个消费者,那么该消费者就会重新消费该分区之前已经消费过的消息。 3. 消息重复发送:在某些情况下,生产者可能会重复发送相同的消息。如果消费者在消费该消息时出现了异常,那么该消息就可能被重复消费。 为了避免 Kafka重复消费,可以考虑使用以下几种策略: 1. 自动管理 offset:Kafka 提供了自动管理 offset 的功能,消费者可以将 offset 保存在 Kafka 中,这样就可以避免手动重置 offset 的问题。 2. 避免 rebalance:可以通过调整消费者组的配置来避免 rebalance 的频繁发生。 3. 消费幂等性:消费者可以实现消费幂等性,即使消息重复消费也不会对系统产生影响。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值