查看: 2026|回复: 0

[Java语言] Spring重试支持Spring Retry的方法

发表于 2018-5-5 09:30:04

本文介绍了Spring重试支持Spring Retry的方法,分享给大家,具体如下:

第一步、引入maven依赖

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>1.5.3.RELEASE</version>
  5. </parent>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-web</artifactId>
  9. </dependency>
  10. <!-- https://mvnrepository.com/artifact/org.springframework.retry/spring-retry -->
  11. <dependency>
  12. <groupId>org.springframework.retry</groupId>
  13. <artifactId>spring-retry</artifactId>
  14. <version>1.1.2.RELEASE</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.aspectj</groupId>
  18. <artifactId>aspectjweaver</artifactId>
  19. <version>1.8.6</version>
  20. </dependency>
复制代码

第二步、添加@Retryable和@Recover注解

  1. package hello;
  2. import org.springframework.remoting.RemoteAccessException;
  3. import org.springframework.retry.annotation.Backoff;
  4. import org.springframework.retry.annotation.Recover;
  5. import org.springframework.retry.annotation.Retryable;
  6. import org.springframework.stereotype.Service;
  7. @Service
  8. public class RemoteService {
  9. @Retryable(value= {RemoteAccessException.class},maxAttempts = 3,backoff = @Backoff(delay = 5000l,multiplier = 1))
  10. public void call() throws Exception {
  11. System.out.println("do something...");
  12. throw new RemoteAccessException("RPC调用异常");
  13. }
  14. @Recover
  15. public void recover(RemoteAccessException e) {
  16. System.out.println(e.getMessage());
  17. }
  18. }
复制代码

@Retryable注解
被注解的方法发生异常时会重试
value:指定发生的异常进行重试
include:和value一样,默认空,当exclude也为空时,所有异常都重试
exclude:指定异常不重试,默认空,当include也为空时,所有异常都重试
maxAttemps:重试次数,默认3
backoff:重试补偿机制,默认没有

@Backoff注解
delay:指定延迟后重试
multiplier:指定延迟的倍数,比如delay=5000l,multiplier=2时,第一次重试为5秒后,第二次为10秒,第三次为20秒

@Recover
当重试到达指定次数时,被注解的方法将被回调,可以在该方法中进行日志处理。需要注意的是发生的异常和入参类型一致时才会回调

第三步、SpringBoot方式启动容器、测试

添加@EnableRetry注解,启用重试功能

  1. package hello;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  5. import org.springframework.retry.annotation.EnableRetry;
  6. @SpringBootApplication
  7. @EnableRetry
  8. public class Application {
  9. public static void main(String[] args) throws Exception {
  10. ApplicationContext annotationContext = new AnnotationConfigApplicationContext("hello");
  11. RemoteService remoteService = annotationContext.getBean("remoteService", RemoteService.class);
  12. remoteService.call();
  13. }
  14. }
复制代码

运行结果:

16:50:51.012 [main] DEBUG org.springframework.retry.support.RetryTemplate - Retry: count=0
do something…
16:50:51.025 [main] DEBUG org.springframework.retry.backoff.ExponentialBackOffPolicy - Sleeping for 5000
16:50:56.026 [main] DEBUG org.springframework.retry.support.RetryTemplate - Checking for rethrow: count=1
16:50:56.026 [main] DEBUG org.springframework.retry.support.RetryTemplate - Retry: count=1
do something…
16:50:56.026 [main] DEBUG org.springframework.retry.backoff.ExponentialBackOffPolicy - Sleeping for 5000
16:51:01.026 [main] DEBUG org.springframework.retry.support.RetryTemplate - Checking for rethrow: count=2
16:51:01.027 [main] DEBUG org.springframework.retry.support.RetryTemplate - Retry: count=2
do something…
16:51:01.027 [main] DEBUG org.springframework.retry.support.RetryTemplate - Checking for rethrow: count=3
16:51:01.027 [main] DEBUG org.springframework.retry.support.RetryTemplate - Retry failed last attempt: count=3
RPC调用异常

参考 :https://github.com/spring-projects/spring-retry

补充

对于非幂等的请求(比如新增,更新操作),千万不要使用重试,对数据一致性会造成很大影响。

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



回复

使用道具 举报