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

Resilience4j RetryConfig.custom()配置失效,重试与降级未触发排查

问题分析与修复方案

核心问题点

  1. Retry配置名称不匹配
    生产代码中@Retry注解指定的name是"throwingFeignException",但TestConfig里创建的Retry实例名称是"feignException",两者名称不一致导致自定义重试规则无法绑定到目标方法上。

  2. 断言与降级返回值不匹配
    生产代码中降级方法logCommunicationFailure返回的是"fail",但测试用例中断言结果包含"was not send successfully",这会直接导致断言失败。

  3. 缺少Resilience4j注解启用配置
    测试上下文未显式启用Resilience4j的重试注解支持,@Retry注解无法生效。

  4. 方法调用存在笔误
    生产代码中被@Retry标记的方法是sendPartner,但测试用例里调用的是myAdapter.sendPerson(myRequest),重试逻辑自然不会触发。


修复步骤

1. 对齐Retry配置名称

修改TestConfig中Retry实例的名称,与@Retry注解的name保持一致:

@Bean("FeignRetry")
public Retry retryWithCustomConfig() {
    RetryConfig customConfig = RetryConfig.custom()
        .maxAttempts(3)
        .waitDuration(Duration.ofSeconds(1))
        .retryExceptions(FeignException.class)
        .build();
    // 名称改为与@Retry注解一致的"throwingFeignException"
    return retryRegistry.retry("throwingFeignException", customConfig);
}

2. 修正断言逻辑

调整测试断言与降级方法返回值匹配:

String result = myAdapter.sendPartner(myRequest);
assertThat(result).contains("fail");

或者修改降级方法返回值为测试预期的内容:

private String logCommunicationFailure(MyRequest myRequest, Exception exception) { 
    return "was not send successfully";
}

3. 启用Resilience4j注解支持

在TestConfig或测试类上添加Resilience4j的@EnableRetry注解:

import io.github.resilience4j.retry.annotation.EnableRetry;

@EnableRetry
@TestConfiguration
public class TestRetryConfig {
    // 现有代码
}

4. 修正方法调用

测试中调用被@Retry标记的sendPartner方法:

String result = myAdapter.sendPartner(myRequest);

内容的提问来源于stack exchange,提问作者Roma Kap

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 10:07:47