SpringBoot整合Retry实现错误重试过程逐步介绍

重试的使用场景比较多,比如调用远程服务时,由于网络或者服务端响应慢导致调用超时,此时可以多重试几次。用定时任务也可以实现重试的效果,但比较麻烦,用Spring Retry的话一个注解搞定所有,感兴趣的可以了解一下

引入依赖

 org.springframework.bootspring-boot-starter-aop org.springframework.retryspring-retry

完整依赖pom.xml

  4.0.0 org.springframework.bootspring-boot-starter-parent2.7.7com.exampledemo0.0.1-SNAPSHOTdemoDemo project for Spring Boot 1.8  org.springframework.bootspring-boot-starter-web org.springframework.bootspring-boot-devtoolsruntimetrue org.projectlomboklomboktrue org.springframework.bootspring-boot-starter-testtest org.springframework.bootspring-boot-starter-aop org.springframework.retryspring-retry   org.springframework.bootspring-boot-maven-plugin   org.projectlomboklombok

开启spring-retry

启动类上增加注解 @EnableRetry

package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.retry.annotation.EnableRetry; @EnableRetry @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } 

使用重试注解

@Retryable注解

  • value,可重试的异常类型。含义同include。默认为空(如果excludes也为空,则重试所有异常)
  • include:可重试的异常类型。默认为空(如果excludes也为空,则重试所有异常)
  • exclude:无需重试的异常类型。默认为空(如果includes也为空,则重试所有异常)
  • maxAttempts:最大重试次数(包括第一次失败),默认为3次
  • backoff:重试等待策略,下面会在@Backoff中介绍
  • recover:表示重试次数到达最大重试次数后的回调方法

@Backoff注解

  • delay,重试之间的等待时间(以毫秒为单位)
  • maxDelay,重试之间的最大等待时间(以毫秒为单位)
  • multiplier,指定延迟的倍数
  • delayExpression,重试之间的等待时间表达式
  • maxDelayExpression,重试之间的最大等待时间表达式
  • multiplierExpression,指定延迟的倍数表达式
  • random,随机指定延迟时间

使用示例

package com.example.demo.component; import org.springframework.retry.annotation.Recover; import org.springframework.retry.annotation.Retryable; import org.springframework.stereotype.Component; @Component public class HttpRequest { private int count = 0; /** * 模拟网络请求异常 * @return */ @Retryable(recover = "errorHandler") public String getResponse() { count++; System.out.println("time: " + count); if (count <4) { throw new RuntimeException("count: " + count); } return "success"; } /** * 错误处理函数 * 注意:需要返回 String,否则会抛出方法找不到异常 * org.springframework.retry.ExhaustedRetryException: Cannot locate recovery method * * @param e * @return */ @Recover public String errorHandler(RuntimeException e) { System.out.println("errorHandler"); return "ok"; } }

测试

package com.example.demo.component; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest public class HttpRequestTest { @Autowired private HttpRequest httpRequest; @Test public void getResponse(){ httpRequest.getResponse(); } } 

输出结果

time: 1
time: 2
time: 3
errorHandler

参考

SpringBoot 中使用 spring-retry 轻松解决重试

到此这篇关于SpringBoot整合Retry实现错误重试过程逐步介绍的文章就介绍到这了,更多相关SpringBoot Retry错误重试内容请搜索0133技术站以前的文章或继续浏览下面的相关文章希望大家以后多多支持0133技术站!

以上就是SpringBoot整合Retry实现错误重试过程逐步介绍的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java