Advanced Spring Boot 4: Event-Driven Architecture (Kafka) · 课时

消费者组与再平衡

了解 Kafka 消费者组如何扩展消费能力、分区如何分配,以及再平衡期间会发生什么,并结合 Spring Kafka 进行学习。

第 4 / 4 课13 个步骤

消费者组与再平衡 是 CoddyKit 上的免费 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why Consumer Groups?

A consumer group lets multiple consumer instances share the work of reading a topic. Kafka assigns each partition to exactly one consumer in the group.

Group ID

Consumers join a group by sharing a group.id. Kafka tracks the offset position per group, so each group reads the topic independently.

spring.kafka.consumer.group-id=order-service

Partitions Drive Parallelism

The number of partitions sets the maximum parallelism. With four partitions, at most four consumers in a group can work at once.

topic orders: 4 partitions
group with 4 consumers -> 1 partition each

Extra Consumers Idle

If a group has more consumers than partitions, the extras sit idle. Scale partitions to scale consumption beyond the current limit.

6 consumers, 4 partitions -> 2 idle

What Triggers a Rebalance

A rebalance reassigns partitions when group membership changes.

  • A consumer joins or leaves
  • A consumer is considered dead
  • Partitions are added

Rebalance Cost

During a rebalance, consumers stop processing briefly while partitions are reassigned. Frequent rebalances hurt throughput.

Heartbeats and Liveness

Consumers send heartbeats. Missing them past session.timeout.ms marks a consumer dead and triggers a rebalance.

session.timeout.ms = 10000
heartbeat.interval.ms = 3000

Poll Loop Health

If processing between polls exceeds max.poll.interval.ms, the consumer is evicted. Keep per-message work bounded or increase the limit.

max.poll.interval.ms = 300000

Committing Offsets

Offsets record progress per partition per group. Spring Kafka can auto-commit or commit manually after successful processing for at-least-once delivery.

spring.kafka.consumer.enable-auto-commit=false

Cooperative Rebalancing

The cooperative rebalance protocol reassigns only the partitions that must move, avoiding a full stop-the-world reassignment.

partition.assignment.strategy=CooperativeStickyAssignor

Spring Kafka Listeners

A @KafkaListener joins the configured group automatically; running multiple app instances scales the group with no code change.

@KafkaListener(topics = "orders", groupId = "order-service")
void onOrder(Order o) { ... }

Quick Check

Reason about parallelism.

Recap

You learned that a consumer group shares a topic by assigning each partition to one consumer, so partition count caps parallelism. Rebalances reassign partitions when membership changes and briefly pause processing; heartbeats and poll intervals govern liveness. Cooperative rebalancing and manual offset commits keep Spring Kafka consumers efficient and reliable.

免费开始

用 AI 导师学习 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「消费者组与再平衡」课时是免费的吗?

是的 — 「消费者组与再平衡」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 课程的其余内容,请升级到 CoddyKit PRO。 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 课程共包含 4 节课。

「消费者组与再平衡」这节课中我会学到什么?

了解 Kafka 消费者组如何扩展消费能力、分区如何分配,以及再平衡期间会发生什么,并结合 Spring Kafka 进行学习。 你通过在浏览器中直接运行的动手代码来练习 Advanced Spring Boot 4: Event-Driven Architecture (Kafka),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「消费者组与再平衡」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 课中编写并运行代码吗?

能。每节 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Kafka 架构概览
  2. 主题、分区与偏移量
  3. 使用 Docker 设置本地 Kafka
  4. 消费者组与再平衡
← 返回 Advanced Spring Boot 4: Event-Driven Architecture (Kafka)