Spring boot Spring Retry没有再次调用maxAttempts 试试看{ reponseType=retryTemplate.execute((RetryCallback)上下文->{ 试一试{ log.error(“调用api尝试#”+context.getRetryCount()); HttpEntity xResponse=HttpRequestTemplate.exchange(requestUrl,HttpMethod.POST,entity,x.class); 返回xResponse.getBody(); }捕获(HttpStatusCodeException){ 如果(如getStatusCode().is5xxServerError()){ 抛出新的RetryException(“api返回服务器错误”,e); } 返回null; } }); }捕获(重试异常){ 投掷e;

Spring boot Spring Retry没有再次调用maxAttempts 试试看{ reponseType=retryTemplate.execute((RetryCallback)上下文->{ 试一试{ log.error(“调用api尝试#”+context.getRetryCount()); HttpEntity xResponse=HttpRequestTemplate.exchange(requestUrl,HttpMethod.POST,entity,x.class); 返回xResponse.getBody(); }捕获(HttpStatusCodeException){ 如果(如getStatusCode().is5xxServerError()){ 抛出新的RetryException(“api返回服务器错误”,e); } 返回null; } }); }捕获(重试异常){ 投掷e;,spring-boot,spring-retry,Spring Boot,Spring Retry,在配置文件中定义了retryTemplate策略,httpRestTemplate是普通模板 @Bean RetryTemplate RetryTemplate(){ RetryTemplate RetryTemplate=新RetryTemplate(); FixedBackOffPolicy FixedBackOffPolicy=新的FixedBackOffPolicy(); 固定退保政策。退保期(30000); retryTemplate.setBackOffPolicy(fixedBac

在配置文件中定义了retryTemplate策略,httpRestTemplate是普通模板

@Bean
RetryTemplate RetryTemplate(){
RetryTemplate RetryTemplate=新RetryTemplate();
FixedBackOffPolicy FixedBackOffPolicy=新的FixedBackOffPolicy();
固定退保政策。退保期(30000);
retryTemplate.setBackOffPolicy(fixedBackOffPolicy);
setRetryPolicy(新的CustomRetryPolicy(3));
返回retryTemplate;
}
但在给定时间后无法再次调用retryTemplate。
有人能帮我吗,我想我在异常游戏中被搞糊涂了。

这只会在
e.getStatusCode().is5xxServerError()
的情况下重试。否则您将返回
null
,从重试模板的角度来看,这是“成功的”。模板只会在引发异常时重试


您可以在重试策略中对可重试的异常进行分类。

me从restTemplate调用中获取503 ServiceUnavailableException,但retryTemplate仍不工作,这毫无意义;请尝试在调试器中运行,以查看lambda返回template.execute方法时发生的情况。我尝试过调试以查看行为,因此这是错误的ke restTemplate.exchange调用api,返回HttpStatusCodeException和ServiceUnavailableException,即503,然后抛出RetryException。在该调试器进入捕捉RetryException的catch块之后,从那里抛出它进入父catch块。这是不可能的;您必须在
RetryTemplate.doExecut中结束e()
跳出lambda时的catch块。我发现需要添加spring特性作为spring重试工作的依赖项。