如何检查 Kafka Consumer 是否准备就绪

如何检查 Kafka Consumer 是否准备就绪

问题描述:

我已将 Kafka 提交策略设置为最新且缺少前几条消息.如果我在开始将消息发送到输入主题之前休眠 20 秒,则一切都按预期工作.我不确定问题是否与消费者花费很长时间进行分区重新平衡有关.有没有办法在开始轮询之前知道消费者是否准备好?

I have Kafka commit policy set to latest and missing first few messages. If I give a sleep of 20 seconds before starting to send the messages to the input topic, everything is working as desired. I am not sure if the problem is with consumer taking long time for partition rebalancing. Is there a way to know if the consumer is ready before starting to poll ?

  • 你可以使用consumer.assignment(),它会返回一组分区并验证是否所有的分区都被分配到该主题可用.

    • You can use consumer.assignment(), it will return set of partitions and verify whether all of the partitions are assigned which are available for that topic.

      如果您使用的是 spring-kafka 项目,您可以包含 spring-kafka-test 依赖项并使用以下方法等待主题分配,但您需要有容器.ContainerTestUtils.waitForAssignment(Object container, int partitions);

      If you are using spring-kafka project, you can include spring-kafka-test dependancy and use below method to wait for topic assignment , but you need to have container. ContainerTestUtils.waitForAssignment(Object container, int partitions);