ack 如何在 Kafka 中为 pub/sub 工作?

How ack works for pub/sub in Kafka?

使用publish/subscribe如何确认消息?

当消息发送给唯一组中的某些消费者时。是所有消费者都确认了,还是所有消费者都确认了消息?

制作人

在生产者端,您可以选择等待代理确认消息已成功存储在主题中。您可以使用名为 acks 的 Producer 配置并将其设置为值 1all:

acks=1: This will mean the leader will write the record to its local log but will respond without awaiting full acknowledgement from all followers. In this case should the leader fail immediately after acknowledging the record but before the followers have replicated it then the record will be lost.

acks=all: This means the leader will wait for the full set of in-sync replicas to acknowledge the record. This guarantees that the record will not be lost as long as at least one in-sync replica remains alive. This is the strongest available guarantee. This is equivalent to the acks=-1 setting.

消费者

在消费者方面,您有一个 Consumer Group 的概念,它正在将偏移量提交回代理,以确认消费者已经处理了哪些消息。每个消费者组将提交自己的偏移量以确认它消费了它们。这独立于其他消费者群体。

如果您有两个消费者组使用相同的主题,每个消费者组将根据其个人配置独立提交其偏移量。

默认情况下,消费者通过消费者配置 enable.auto.commitauto.commit.interval.ms 自动将偏移量提交回 Kafka,默认为 5 秒。或者您可以根据您在消费者中的处理逻辑手动提交偏移量。