当前位置:   article > 正文

Spring Boot集成Spring Retry快速入门Demo

Spring Boot集成Spring Retry快速入门Demo

1.Retry介绍

Spring Retry

提供了自动重新调用失败的操作的功能。这在错误可能是暂时的(例如瞬时网络故障)的情况下很有用。从2.2.0版本开始,重试功能已从Spring Batch中撤出,成为一个独立的新库:Spring Retry

使用场景

在日常开发过程中,难免会与第三方接口发生交互,例如:短信发送、远程服务调用、争抢锁等场景,当正常调用发生异常时,例如:网络抖动,这些间歇性的异常在一段时候之后会自行恢复,程序为了更加健壮并且更不容易出现故障,需要重新触发业务操作,以防止间歇性的异常对程序照成的影响。

tips:幂等性

非幂等的情况下要小心使用重试。HTTP/1.1中对幂等性的定义是:一次和多次请求某一个资源对于资源本身应该具有同样的结果(网络超时等问题除外)。也就是说,其任意多次执行对资源本身所产生的影响均与一次执行的影响相同。

2.代码工程

实验目标:模拟三方接口异常,触发重试

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>springboot-demo</artifactId>
  7. <groupId>com.et</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>SpringRetry</artifactId>
  12. <properties>
  13. <maven.compiler.source>8</maven.compiler.source>
  14. <maven.compiler.target>8</maven.compiler.target>
  15. </properties>
  16. <dependencies>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-web</artifactId>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-autoconfigure</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-test</artifactId>
  28. <scope>test</scope>
  29. </dependency>
  30. <!--retry-->
  31. <dependency>
  32. <groupId>org.springframework.boot</groupId>
  33. <artifactId>spring-boot-starter-web</artifactId>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework.retry</groupId>
  37. <artifactId>spring-retry</artifactId>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.aspectj</groupId>
  41. <artifactId>aspectjweaver</artifactId>
  42. </dependency>
  43. </dependencies>
  44. </project>

application.yaml

  1. server:
  2. port: 8088

RemoteApiService.java

  • @Retryable注解,value值表示当哪些异常的时候触发重试,maxAttempts表示最大重试次数默认为3,delay表示重试的延迟时间,multiplier表示上一次延时时间是这一次的倍数。

  • @Recover注解,当重试次数达到设置的次数的时候,还是失败抛出异常,执行的回调函数。

  1. package com.et.retry.service;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.retry.annotation.Backoff;
  5. import org.springframework.retry.annotation.Recover;
  6. import org.springframework.retry.annotation.Retryable;
  7. import org.springframework.stereotype.Service;
  8. import java.time.LocalDateTime;
  9. import java.time.LocalTime;
  10. @Service
  11. public class RemoteApiService {
  12. private Logger logger = LoggerFactory.getLogger(getClass());
  13. @Retryable(value = Exception.class,maxAttempts = 3,backoff = @Backoff(delay = 2000,multiplier = 1.5))
  14. public boolean pay(int num) throws Exception{
  15. logger.info("invoke third method");
  16. logger.info("do something... {}", LocalDateTime.now());
  17. //mock exception
  18. if(num==0) {
  19. throw new Exception("error,need retry");
  20. }
  21. return true;
  22. }
  23. @Recover
  24. public boolean recover(int num) throws Exception {
  25. logger.info("recover ... {},{}", num, LocalDateTime.now());
  26. return false;
  27. }
  28. }

DemoApplication.java

在主类上加上@EnableRetry注解,表示启用重试机制。

  1. package com.et.retry;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.retry.annotation.EnableRetry;
  5. @SpringBootApplication
  6. @EnableRetry
  7. public class DemoApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(DemoApplication.class, args);
  10. }
  11. }

以上只是一些关键代码,所有代码请参见下面代码仓库

代码仓库

  • https://github.com/Harries/springboot-demo

3.测试

编写测试类

  1. package com.et.retry;
  2. import com.et.retry.service.RemoteApiService;
  3. import org.junit.After;
  4. import org.junit.Before;
  5. import org.junit.Test;
  6. import org.junit.runner.RunWith;
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.boot.test.context.SpringBootTest;
  11. import org.springframework.test.context.junit4.SpringRunner;
  12. @RunWith(SpringRunner.class)
  13. @SpringBootTest(classes = DemoApplication.class)
  14. public class DemoTests {
  15. private Logger log = LoggerFactory.getLogger(getClass());
  16. @Autowired
  17. RemoteApiService remoteApiService;
  18. @Before
  19. public void before() {
  20. log.info("init some data");
  21. }
  22. @After
  23. public void after(){
  24. log.info("clean some data");
  25. }
  26. @Test
  27. public void execute() throws Exception {
  28. log.info("pay result:"+remoteApiService.pay(0));
  29. }
  30. }

运行测试方法,结果如下

  1. 2024-04-03 10:35:54.738 INFO 13096 --- [ main] com.et.retry.DemoTests : init some data
  2. 2024-04-03 10:35:54.756 INFO 13096 --- [ main] com.et.retry.service.RemoteApiService : invoke third method
  3. 2024-04-03 10:35:54.759 INFO 13096 --- [ main] com.et.retry.service.RemoteApiService : do something... 2024-04-03T10:35:54.758
  4. 2024-04-03 10:35:56.766 INFO 13096 --- [ main] com.et.retry.service.RemoteApiService : invoke third method
  5. 2024-04-03 10:35:56.766 INFO 13096 --- [ main] com.et.retry.service.RemoteApiService : do something... 2024-04-03T10:35:56.766
  6. 2024-04-03 10:35:59.776 INFO 13096 --- [ main] com.et.retry.service.RemoteApiService : invoke third method
  7. 2024-04-03 10:35:59.776 INFO 13096 --- [ main] com.et.retry.service.RemoteApiService : do something... 2024-04-03T10:35:59.776
  8. 2024-04-03 10:35:59.776 INFO 13096 --- [ main] com.et.retry.service.RemoteApiService : recover ... 0,2024-04-03T10:35:59.776
  9. 2024-04-03 10:35:59.776 INFO 13096 --- [ main] com.et.retry.DemoTests : pay result:false
  10. 2024-04-03 10:35:59.778 INFO 13096 --- [ main] com.et.retry.DemoTests : clean some data

4.引用

  • https://www.jianshu.com/p/314059943f1c

  • http://www.liuhaihua.cn/archives/710389.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/382104
推荐阅读
相关标签
  

闽ICP备14008679号