Exactly Once Semantics
您可以为侦听器容器提供“KafkaAwareTransactionManager
”实例。在如此配置后,容器会在调用侦听器前启动事务。侦听器执行的任何“KafkaTemplate
”操作都会参与事务。如果侦听器成功处理记录(或多个记录,当使用“BatchMessageListener
”时),则容器将在事务管理器提交事务之前使用“producer.sendOffsetsToTransaction()
)”将偏移量发送给事务。如果侦听器抛出异常,则该事务回滚,使用者重新定位,以便在下一次轮询时检索回滚的记录。有关详情以及如何处理反复失败的记录,请参阅“After-rollback Processor”。
You can provide a listener container with a KafkaAwareTransactionManager
instance.
When so configured, the container starts a transaction before invoking the listener.
Any KafkaTemplate
operations performed by the listener participate in the transaction.
If the listener successfully processes the record (or multiple records, when using a BatchMessageListener
), the container sends the offset(s) to the transaction by using producer.sendOffsetsToTransaction()
), before the transaction manager commits the transaction.
If the listener throws an exception, the transaction is rolled back and the consumer is repositioned so that the rolled-back record(s) can be retrieved on the next poll.
See After-rollback Processor for more information and for handling records that repeatedly fail.
使用事务可启用 Exactly Once Semantics (EOS)。
Using transactions enables Exactly Once Semantics (EOS).
这意味着,对于 read → process → write
序列,可以保证 序列 精确完成一次。(读取和处理至少有一次语义)。
This means that, for a read → process → write
sequence, it is guaranteed that the sequence is completed exactly once.
(The read and process have at least once semantics).
适用于 Apache Kafka 3.0 及更高版本的 Spring 仅支持 EOSMode.V2
:
Spring for Apache Kafka version 3.0 and later only supports EOSMode.V2
:
-
V2
- aka fetch-offset-request fencing (since version 2.5)
这要求代理是版本 2.5 或更高版本。
This requires the brokers to be version 2.5 or later.
在模式 V2
中,不需要每个 group.id/topic/partition
都拥有一个生产者,因为消费者元数据会连同偏移量一起发送到事务,而代理可以根据该信息确定生产者是否被隔离。
With mode V2
, it is not necessary to have a producer for each group.id/topic/partition
because consumer metadata is sent along with the offsets to the transaction and the broker can determine if the producer is fenced using that information instead.
有关详情,请参阅“ KIP-447”。
Refer to KIP-447 for more information.
“V2
”以前是“BETA
”;“EOSMode
”已更改为将该框架与“ KIP-732”对齐。
V2
was previously BETA
; the EOSMode
has been changed to align the framework with KIP-732.