You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置Spring Modulith事件发布仓库失败事件的运行时重试?

Spring Modulith 失败事件运行时自动重试配置

结合你已实现的事件失败存储逻辑,要在应用运行期间自动重试失败事件、完整落地Transactional Outbox模式,可按以下步骤配置:

1. 引入依赖

若项目未集成Spring Retry,需添加相关依赖:

Maven

<dependency>
    <groupId>org.springframework.retry</groupId>
    <artifactId>spring-retry</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

Gradle

implementation 'org.springframework.retry:spring-retry'
implementation 'org.springframework.boot:spring-boot-starter-aop'

2. 启用并配置内置重试机制

通过配置文件开启Spring Modulith的事件重试支持,可直接控制重试次数、间隔等参数:

# 开启运行时事件重试
spring.modulith.events.retry.enabled=true
# 最大重试次数(默认3次)
spring.modulith.events.retry.max-attempts=5
# 首次重试延迟(默认1000ms)
spring.modulith.events.retry.initial-delay=2000
# 延迟时间乘数(默认2,即每次间隔翻倍)
spring.modulith.events.retry.multiplier=1.5
# 最大延迟时间(默认10000ms)
spring.modulith.events.retry.max-delay=10000

3. 自定义重试逻辑(可选)

如果需要针对特定异常类型、更复杂的重试规则,可自定义RetryTemplate并注册为Bean,Spring Modulith会自动复用该模板:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.retry.backoff.ExponentialBackOffPolicy;
import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.retry.annotation.EnableRetry;

import java.util.HashMap;
import java.util.Map;

@Configuration
@EnableRetry
public class EventRetryConfig {

    @Bean
    public RetryTemplate eventRetryTemplate() {
        RetryTemplate template = new RetryTemplate();

        // 配置重试策略:指定可重试的异常类型
        Map<Class<? extends Throwable>, Boolean> retryableExceptions = new HashMap<>();
        retryableExceptions.put(RuntimeException.class, true);
        retryableExceptions.put(CustomProcessingException.class, true);

        SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
        retryPolicy.setMaxAttempts(5);
        retryPolicy.setRetryableExceptions(retryableExceptions);
        template.setRetryPolicy(retryPolicy);

        // 配置指数退避策略
        ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
        backOffPolicy.setInitialInterval(2000);
        backOffPolicy.setMultiplier(1.5);
        backOffPolicy.setMaxInterval(10000);
        template.setBackOffPolicy(backOffPolicy);

        return template;
    }
}

4. 为监听器添加重试标记

在事件监听器方法上添加@Retryable注解,明确指定需要重试的异常类型:

import org.springframework.modulith.events.ApplicationModuleListener;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Component;

@Component
public class SampleFailedEventListener {

    @ApplicationModuleListener
    @Retryable(retryFor = {RuntimeException.class, CustomProcessingException.class})
    public void handleSampleEvent(SampleEvent event) {
        // 可能抛出异常的业务逻辑
        if (event.shouldFail()) {
            throw new RuntimeException("Event processing failed");
        }
    }
}

5. 完整验证Transactional Outbox模式

结合你已配置的spring.modulith.republish-outstanding-events-on-restart=true,此时系统会:

  • 事务提交后将事件存入Outbox(Event Publication Registry)
  • 监听器失败时,事件标记为未完成并触发运行时自动重试
  • 应用重启后,未完成的事件会重新发布执行

内容的提问来源于stack exchange,提问作者Wim Deblauwe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 13:02:23