欢迎来到工商注册核名查询系统!

Java

当前位置:主页 > 软件编程 > Java >

Spring boot使用spring retry重试机制的方法示例

来源:本站原创|时间:2022-11-25|栏目:Java|

当我们调用接口的时候由于网络原因可能失败,再尝试就成功了,这就是重试机制。非幂等的情况下要小心使用重试。

tips:幂等性

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

注解方式使用Spring Retry

(一)Maven依赖

<!-- 重试机制 -->
<dependency>
  <groupId>org.springframework.retry</groupId>
  <artifactId>spring-retry</artifactId>
  <version>1.2.4.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.9.4</version>
</dependency>

(二)配置类添加注解 @EnableRetry

@EnableRetry
@Configuration
public class RetryConfiguration {

}

(三)Service方法编写

@Retryable注解:

value: 抛出指定异常才会重试

include:和value一样,默认为空,当exclude也为空时,默认所以异常

exclude:指定不处理的异常

maxAttempts:最大重试次数,默认3次

backoff:重试等待策略,默认使用@Backoff,@Backoff的value默认为1000L;multiplier(指定延迟倍数)

@Recover注解:

当重试达到指定次数时候该注解的方法将被回调

发生的异常类型需要和@Recover注解的参数一致

@Retryable注解的方法不能有返回值,不然@Recover注解的方法无效

@Service
public class RetryService {

 private Logger logger = LoggerFactory.getLogger(RetryService.class);

 @Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 2000L, multiplier = 2))
 public void devide(double a, double b){
   logger.info("开始进行除法运算");
   if (b == 0) {
     throw new RuntimeException();
   }
   logger.info("{} / {} = {}", a, b, a / b);
 }

 @Recover
 public void recover() {
   logger.error("被除数不能为0");
 }

}

(四)测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class BootdemoApplicationTests {

  @Autowired
  private RetryService retryService;

  private Logger logger = LoggerFactory.getLogger(BootdemoApplication.class);

  @Test
  public void retryTest() {
    //int count = retryService.retry(-1);
    retryService.retry(-1);
    //logger.info("库存为:" + count);
  }
}

注意事项

@Retryable不能在本类使用,不然不会生效。如果直接调用execute重试机制将不会生效,调用devide则重试生效。

public void execute(double a, double b) throws DevideException {
    devide(a, b);
  }

  @Retryable(value = DevideException.class, maxAttempts = 3, backoff = @Backoff(delay = 2000L, multiplier = 2))
  public void devide(double a, double b) throws DevideException {
    logger.info("开始进行除法运算");
    if (b == 0) {
      throw new DevideException("被除数不能为0");
    }
    logger.info("{} / {} = {}", a, b, a / b);
  }

使用@Retryable不能使用try catch捕获异常为简单

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

更多Java

您可能感兴趣的文章

阅读排行

本栏相关

随机阅读

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 工商注册核名查询系统 版权所有