【问题标题】:Spring Boot Manual Acknowledgement of kafka messages is not workingSpring Boot 手动确认 kafka 消息不起作用
【发布时间】:2019-08-15 21:55:04
【问题描述】:

我有一个 spring boot kafka 消费者,它使用来自主题的数据并将其存储在数据库中,并在存储后确认它。 它工作正常,但是如果应用程序在使用记录后未能获得数据库连接,就会出现问题,在这种情况下,我们不会发送确认,但消息仍然不会被消费,直到或除非我们更改组 ID 并重新启动消费者

我的消费者如下所示

  @KafkaListener(id = "${group.id}", topics = {"${kafka.edi.topic}"})
  public void onMessage(ConsumerRecord record, Acknowledgment acknowledgment) {
      boolean shouldAcknowledge = false;
      try {
          String tNo = getTrackingNumber((String) record.key());

          log.info("Check Duplicate By Comparing With DB records");

          if (!ediRecordService.isDuplicate(tNo)) {---this checks the record in my DB
              shouldAcknowledge = insertEDIRecord(record, tNo); --this return true
          } else {
              log.warn("Duplicate record found.");
              shouldAcknowledge = true;
          }
          if (shouldAcknowledge) {
              acknowledgment.acknowledge();
          }```



So if you see the above snippet we did not sent acknowledgment.

【问题讨论】:

标签: spring-boot apache-kafka kafka-consumer-api


【解决方案1】:

这不是 kafka 偏移的工作方式here

每个分区中的记录都分配有一个称为偏移量的顺序 ID 号,它唯一地标识分区内的每个记录。

从上面的语句例如,从第一个轮询消费者获取偏移量300的消息,如果由于某些问题未能持久保存到数据库中,它将不会提交偏移量。

所以在下一次轮询中,它将获得偏移量为301 的下一条记录,如果它成功地将数据持久化到数据库中,那么它将提交偏移量301 (这意味着该分区中的所有记录都已处理直到那个偏移量,在上面的例子中它是 301)

解决方案:使用重试机制,直到它成功地将数据存储到数据库中,重试次数有限,或者只是将失败的数据保存到error topic 并稍后重新处理,或者将失败记录的偏移量保存在某处以便稍后重新处理它们。

【讨论】:

  • 我有一个问题 - 如果 300 没有“提交”,下一次投票将如何获取 301 偏移量
猜你喜欢
  • 1970-01-01
  • 2021-03-21
  • 2020-06-29
  • 2019-08-22
  • 2021-03-21
  • 2015-12-20
  • 1970-01-01
  • 2018-09-07
  • 2020-02-26
相关资源
最近更新 更多