组中的 Kafka 消费者跳过分区
Kafka consumer in group skips the partitions
我有一个消费主题的消费者。主题有 6 个分区。分配给组的单个消费者。
我像下面这样投票
Consumer.poll(10000)
当没有记录 return.
时,我退出消费者获取
根据文档,我认为当没有记录可供使用且持续时间 10000 足以重新平衡和获取记录时,轮询 return 为空。
大多数时候轮询会消耗所有分区的记录,但有时轮询会从 3 个分区获取记录和 return 空记录而不消耗其他 3 个分区。
顺便说一句,我使用的是 2.0.1 Kafka 客户端和 Kafka 服务器版本是 2.11 - 2.2.0。
任何人都知道为什么我的消费者跳过其他分区并且 return 空 records.what 我应该使用所有分区。
max.poll.records
参数默认为500。所以有时可能无法通过一次 poll() 获取主题中所有分区的所有消息。
max.poll.records: The maximum number of records returned in a single
call to poll().
顺便说一下,组中只有一个消费者不是使用分区主题的合适方式。您在消费者组中的消费者数量应等于最佳实践中订阅的主题中的分区数量。 (Kafka默认是均匀分配分区给消费者的)否则你不能横向扩展负载,那么分区就没有那么有意义了。
Kafka 总是将分区分配给消费者。不可能有未分配给消费者的分区。 (如果订阅了该主题)
但在您的情况下,因为您退出了消费者,所以需要一些时间 (session.timeout.ms
) 才能将此消费者视为 Kafka 已死。如果不等session.timeout.ms
通过就再次启动consumer,那么Kafka就会意识到consumer group中有两个活跃的consumer,将partition平均分配给这两个个consumer。 (比如:分区 0、1、2 到消费者 1,分区 3、4、5 到消费者 2)但是在 Kafka 意识到其中一个消费者死了之后,在消费者组中开始重新平衡,并将所有分区分配给消费者组中的一个活跃消费者。
session.timeout.ms: The timeout used to detect client failures when
using Kafka's group management facility. The client sends periodic
heartbeats to indicate its liveness to the broker. If no heartbeats
are received by the broker before the expiration of this session
timeout, then the broker will remove this client from the group and
initiate a rebalance. Note that the value must be in the allowable
range as configured in the broker configuration by
group.min.session.timeout.ms and group.max.session.timeout.ms
您可以在代理端使用此 cli 命令检查您的消费者组的当前分区分配:
./kafka/bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group yourConsumerGroup
我有一个消费主题的消费者。主题有 6 个分区。分配给组的单个消费者。
我像下面这样投票
Consumer.poll(10000)
当没有记录 return.
根据文档,我认为当没有记录可供使用且持续时间 10000 足以重新平衡和获取记录时,轮询 return 为空。 大多数时候轮询会消耗所有分区的记录,但有时轮询会从 3 个分区获取记录和 return 空记录而不消耗其他 3 个分区。
顺便说一句,我使用的是 2.0.1 Kafka 客户端和 Kafka 服务器版本是 2.11 - 2.2.0。
任何人都知道为什么我的消费者跳过其他分区并且 return 空 records.what 我应该使用所有分区。
max.poll.records
参数默认为500。所以有时可能无法通过一次 poll() 获取主题中所有分区的所有消息。
max.poll.records: The maximum number of records returned in a single call to poll().
顺便说一下,组中只有一个消费者不是使用分区主题的合适方式。您在消费者组中的消费者数量应等于最佳实践中订阅的主题中的分区数量。 (Kafka默认是均匀分配分区给消费者的)否则你不能横向扩展负载,那么分区就没有那么有意义了。
Kafka 总是将分区分配给消费者。不可能有未分配给消费者的分区。 (如果订阅了该主题)
但在您的情况下,因为您退出了消费者,所以需要一些时间 (session.timeout.ms
) 才能将此消费者视为 Kafka 已死。如果不等session.timeout.ms
通过就再次启动consumer,那么Kafka就会意识到consumer group中有两个活跃的consumer,将partition平均分配给这两个个consumer。 (比如:分区 0、1、2 到消费者 1,分区 3、4、5 到消费者 2)但是在 Kafka 意识到其中一个消费者死了之后,在消费者组中开始重新平衡,并将所有分区分配给消费者组中的一个活跃消费者。
session.timeout.ms: The timeout used to detect client failures when using Kafka's group management facility. The client sends periodic heartbeats to indicate its liveness to the broker. If no heartbeats are received by the broker before the expiration of this session timeout, then the broker will remove this client from the group and initiate a rebalance. Note that the value must be in the allowable range as configured in the broker configuration by group.min.session.timeout.ms and group.max.session.timeout.ms
您可以在代理端使用此 cli 命令检查您的消费者组的当前分区分配:
./kafka/bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group yourConsumerGroup