再平衡监听器与静态成员资格
了解消费者组再平衡如何中断处理,以及再平衡监听器和静态成员资格如何减少停机时间与状态丢失。
再平衡监听器与静态成员资格 是 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 节课。
本课时的部分内容尚未翻译,以英文显示。
What Triggers a Rebalance
A rebalance reassigns partitions among consumers in a group. It happens when a consumer joins, leaves, crashes, or its session times out.
During a rebalance, processing pauses across the group.
The Cost of Rebalancing
Frequent rebalances hurt throughput: partitions are revoked and reassigned, in-flight work may be reprocessed, and local caches are invalidated.
Advanced consumers actively minimize and react to rebalances.
Rebalance Listener Hooks
A ConsumerRebalanceListener gives you two callbacks:
onPartitionsRevoked— called before partitions are taken away.onPartitionsAssigned— called after new partitions are granted.
Committing on Revoke
Use onPartitionsRevoked to commit offsets for work completed so far, avoiding duplicate processing after reassignment.
public void onPartitionsRevoked(Collection<TopicPartition> parts) {
consumer.commitSync(currentOffsets);
}Wiring It in Spring
In Spring Kafka, implement ConsumerAwareRebalanceListener and set it on the container properties.
factory.getContainerProperties()
.setConsumerRebalanceListener(myRebalanceListener);Static Membership
Static membership assigns each consumer a stable group.instance.id. On a brief restart, the broker keeps its partitions instead of triggering a rebalance.
spring:
kafka:
consumer:
properties:
group.instance.id: consumer-pod-1Session and Heartbeat Timeouts
Static membership relies on session.timeout.ms. If a consumer rejoins within the session window using the same instance id, no rebalance occurs.
session.timeout.ms: 45000
heartbeat.interval.ms: 3000Cooperative Rebalancing
The CooperativeStickyAssignor reassigns only the partitions that must move, instead of revoking everything (stop-the-world). This dramatically reduces rebalance impact.
partition.assignment.strategy: org.apache.kafka.clients.consumer.CooperativeStickyAssignorAvoiding Poll Timeouts
If processing a batch exceeds max.poll.interval.ms, the broker assumes the consumer is dead and rebalances. Keep per-poll work bounded or raise the interval.
Combining Strategies
For resilient consumers, combine static membership (avoid restart churn), cooperative rebalancing (smaller moves), and a rebalance listener (clean offset commits).
Putting It Together
Rebalances are unavoidable but controllable. Listeners let you commit cleanly, static membership avoids needless rebalances on restart, and cooperative assignment limits disruption.
Quick Check
Test your understanding of rebalancing.
Recap
You learned advanced rebalance handling.
- Rebalances pause processing and can reprocess work.
- Rebalance listeners let you commit offsets on revoke.
- Static membership avoids rebalances on brief restarts.
- Cooperative assignment reduces stop-the-world disruption.
用 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 节课。
「再平衡监听器与静态成员资格」这节课中我会学到什么?
了解消费者组再平衡如何中断处理,以及再平衡监听器和静态成员资格如何减少停机时间与状态丢失。 你通过在浏览器中直接运行的动手代码来练习 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 反馈 — 无需本地设置。