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

使用重试主题实现非阻塞重试

学习使用 Spring Kafka 的 @RetryableTopic 执行非阻塞、延迟重试,让消费者在重新处理失败消息的同时保持响应。

第 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 节课。

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

The Problem with Blocking Retries

Blocking retries (Spring Retry) pause the consumer thread between attempts. A long backoff blocks the partition, stalling all later records.

Non-blocking retries solve this by moving failed records to dedicated retry topics.

How Retry Topics Work

When processing fails, the record is forwarded to a retry topic with a delay. A separate listener consumes the retry topic after the delay and tries again.

  • The main consumer keeps moving forward.
  • Each retry level can have a longer delay.

Enabling RetryableTopic

Annotate your listener with @RetryableTopic. Spring auto-creates the retry and DLT topics.

@RetryableTopic
@KafkaListener(topics = "orders")
public void handle(Order order) {
    paymentService.charge(order);
}

Configuring Attempts and Backoff

Customize the number of attempts and the delay strategy directly in the annotation.

@RetryableTopic(
    attempts = "4",
    backoff = @Backoff(delay = 1000, multiplier = 2.0))
@KafkaListener(topics = "orders")
public void handle(Order order) { /* ... */ }

Generated Topic Names

By default Spring creates topics like orders-retry-0, orders-retry-1, and finally orders-dlt.

Each retry topic corresponds to one backoff level.

Exponential vs Fixed Backoff

Set multiplier for exponential growth, or omit it for fixed delays.

  • Fixed: 1s, 1s, 1s.
  • Exponential: 1s, 2s, 4s.

Exponential is better for transient downstream outages.

Selecting Retryable Exceptions

Only retry exceptions that are transient. Use include or exclude to control which exceptions trigger retries.

@RetryableTopic(
    include = { RemoteServiceException.class },
    exclude = { ValidationException.class })
@KafkaListener(topics = "orders")
public void handle(Order order) { /* ... */ }

Handling the DLT

After all attempts fail, the record lands on the DLT. Add a @DltHandler method to react.

@DltHandler
public void handleDlt(Order order) {
    log.error("Order permanently failed: {}", order.getId());
}

Reading Retry Headers

Retry records carry headers such as the attempt count and original timestamp, letting you log how far a message progressed.

@Header(KafkaHeaders.ORIGINAL_TOPIC) String originalTopic

Blocking vs Non-Blocking

Use blocking retries for very short, immediate transients; use non-blocking retry topics when delays are longer and partition throughput matters.

Putting It Together

Non-blocking retries keep consumers responsive while still giving failed messages multiple chances. Combine @RetryableTopic, exponential backoff, exception filtering, and a @DltHandler.

Quick Check

Test your understanding of retry topics.

Recap

You learned non-blocking retries.

  • @RetryableTopic creates retry topics automatically.
  • Configure attempts, backoff, and a multiplier.
  • Filter retryable exceptions with include/exclude.
  • Use @DltHandler for permanently failed records.
免费开始

用 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 节课。

「使用重试主题实现非阻塞重试」这节课中我会学到什么?

学习使用 Spring Kafka 的 @RetryableTopic 执行非阻塞、延迟重试,让消费者在重新处理失败消息的同时保持响应。 你通过在浏览器中直接运行的动手代码来练习 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. 处理消费者异常
  2. 使用 Spring Retry 实现重试机制
  3. 实现死信主题(DLT)
  4. 使用重试主题实现非阻塞重试
← 返回 Advanced Spring Boot 4: Event-Driven Architecture (Kafka)