会话超时时Kafka Listener回滚事务

时间:2017-09-01 11:46:45

标签: spring apache-kafka spring-kafka

我使用spring-kafka版本1.1.3来使用来自主题的消息。消费者配置中的自动提交设置为truemax.poll.records10。与服务器协商session.timeout.ms为10秒。

收到消息后,我将部分消息保存到数据库中。我的数据库有时会很慢,导致kafka监听器会话超时:

  

组mygroup的自动偏移提交失败:无法完成提交   因为该组已经重新平衡并分配了分区   另一名成员。这意味着后续调用之间的时间   poll()比配置的session.timeout.ms长   通常意味着民意调查循环花费了太多时间的消息   处理。您可以通过增加会话来解决此问题   超时或通过减少poll()中返回的批量的最大大小   与max.poll.records。

由于我无法在服务器上增加会话超时且max.poll.records已经降低到10,我希望能够在事务中包装我的数据库调用,这将会在kafka会话超时的情况下回滚。

这可能吗?我该如何做到这一点?

不幸的是,我无法在文档中找到解决方案。

2 个答案:

答案 0 :(得分:2)

您必须考虑升级到Spring Kafka 1.2和Kafka 0.10.x。旧的阿帕奇卡夫卡有一个心跳的缺陷。因此,使用autoCommit和慢速监听器会导致意外的重新平衡,并且您可能会遇到这样的问题。您使用的Spring Kafka版本具有以下逻辑:

// if the container is set to auto-commit, then execute in the
// same thread
// otherwise send to the buffering queue
if (this.autoCommit) {
    invokeListener(records);
}
else {
    if (sendToListener(records)) {
        if (this.assignedPartitions != null) {
            // avoid group management rebalance due to a slow
            // consumer
            this.consumer.pause(this.assignedPartitions);
            this.paused = true;
            this.unsent = records;
        }
    }
}

因此,您可以考虑关闭autoCommit并依赖默认启用的内置pause功能。

答案 1 :(得分:0)

决定升级到Kafka 0.11,因为它增加了交易支持(见Release Notes)。

相关问题