集群运行的应用在消费多主题时,如何将多主题的分区均匀分配?

How to assign partitions of multi topics evenly when consuming multi topics in an application that runs in a cluster?

假设有一个应用程序在 100 个节点的集群中运行。并且应用程序消耗了20个主题,每个主题有30个分区。

理想情况下,每个应用程序实例应该有 6 个分区,来自相同或不同的主题。但最坏的情况可能只有 30 个节点实际使用分区分配。其余节点没有分配分区。

有什么方法可以使它接近理想状态,即每个应用程序实例占用 6 个分区?

这真的是一个虚拟问题...Kafka 对此有本地解决方案。配置使用 RoundRobinAssignor 或 StickyAssignor 并在同一消费者中消费多个主题。转让人将处理剩下的事情。

虽然问题的作者已经回答了partition.assignment.strategy的值,他应该使用它来获得多个主题消费时分区的均匀分布。我想强调此配置的含义以及我们在设置此配置时可以牢记的经验法则

partition.assignment.strategy 简单地决定 分区在给定消费者组中的消费者实例之间分配的顺序

此配置的可能选项是 Range、RoundRobin、StickyAssignor 和 YourOwnCustomPartitioner

经验法则:

The take-away lesson is to subscribe to multiple topics, better to use RoundRobinAssignor if the number of partitions for each topic is not even. But If partitions of each topic are even or just one topic. You could use either RoundRobinAssignor or RangeAssignor .

我为上述经验法则引用了此文档Partition Assignment strategy

另一个有用的读物​​是 understanding-kafka-partition-assignment-strategies