为什么Kafka消费者需要很长时间才能开始消费?

时间:2017-02-01 09:07:07

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

我们启动Kafka消费者,收听可能尚未创建的主题(尽管已启用主题自动创建)。

此后不久,制作人就该主题发布消息。

然而,消费者注意这需要一些时间:确切地说是5分钟。此时,消费者撤销其分区并重新加入消费者组。卡夫卡重新稳定了这个群体。查看消费者与kafka日志的时间戳,此过程在消费者端实例化。

我认为这是预期的行为,但我想理解这一点。这实际上是一个重新平衡(从0到1分区)?如果我们提前创建主题,这不会发生吗?

2017-02-01 08:36:45.692  INFO 7 --- [afka-consumer-1] o.a.k.c.c.internals.ConsumerCoordinator  : Revoking previously assigned partitions [] for group tps-kafka-partitioning
2017-02-01 08:36:45.692  INFO 7 --- [afka-consumer-1] o.s.k.l.KafkaMessageListenerContainer    : partitions revoked:[]
2017-02-01 08:36:45.693  INFO 7 --- [afka-consumer-1] o.a.k.c.c.internals.AbstractCoordinator  : (Re-)joining group tps-kafka-partitioning
2017-02-01 08:36:45.738  INFO 7 --- [afka-consumer-1] o.a.k.c.c.internals.AbstractCoordinator  : Successfully joined group tps-kafka-partitioning with generation 1
2017-02-01 08:36:45.747  INFO 7 --- [afka-consumer-1] o.a.k.c.c.internals.ConsumerCoordinator  : Setting newly assigned partitions [] for group tps-kafka-partitioning
2017-02-01 08:36:45.749  INFO 7 --- [afka-consumer-1] o.s.k.l.KafkaMessageListenerContainer    : partitions assigned:[]
2017-02-01 08:41:45.540  INFO 7 --- [afka-consumer-1] o.a.k.c.c.internals.ConsumerCoordinator  : Revoking previously assigned partitions [] for group tps-kafka-partitioning
2017-02-01 08:41:45.544  INFO 7 --- [afka-consumer-1] o.s.k.l.KafkaMessageListenerContainer    : partitions revoked:[]
2017-02-01 08:41:45.544  INFO 7 --- [afka-consumer-1] o.a.k.c.c.internals.AbstractCoordinator  : (Re-)joining group tps-kafka-partitioning

kafka logs

[2017-02-01 08:41:45,546] INFO [GroupCoordinator 1001]: Preparing to restabilize group tps-kafka-partitioning with old generation 1 (kafka.coordinator.GroupCoordinator)
[2017-02-01 08:41:45,546] INFO [GroupCoordinator 1001]: Stabilized group tps-kafka-partitioning generation 2 (kafka.coordinator.GroupCoordinator)
[2017-02-01 08:41:45,551] INFO [GroupCoordinator 1001]: Assignment received from leader for group tps-kafka-partitioning for generation 2 (kafka.coordinator.GroupCoordinator)
[2017-02-01 08:42:14,636] INFO [GroupCoordinator 1001]: Preparing to restabilize group tps-kafka-group-id with old generation 1 (kafka.coordinator.GroupCoordinator)
[2017-02-01 08:42:14,636] INFO [GroupCoordinator 1001]: Stabilized group tps-kafka-group-id generation 2 (kafka.coordinator.GroupCoordinator)

1 个答案:

答案 0 :(得分:6)

这可能是由于参数 metadata.max.age.ms 的默认值,它控制了消费者强制刷新主题元数据的频率。

当您使用不存在的主题启动消费者时会发生什么情况是经纪人自动创建此主题,但这需要花费一点时间进行领导者选举等,因此当您的消费者请求该主题的元数据时,它会得到一个LEADER_NOT_AVAILABLE警告,无法获取任何消息。 在达到上述超时后,消费者刷新元数据,这次成功并开始阅读消息。这不依赖于生产者向主题写消息,它纯粹是消费者的事情。

如果您使用例如1000毫秒超时启动您的消费者,您应该会看到更短的延迟,直到消息被消耗。

此外,如果您预先创建主题,或者在消费者之前启动生产者,则根本不会发生此行为。