Spring重试支持Spring Retry的方法

 更新时间:2018年04月06日 11:18:17   作者:人生的旅客  
本篇文章主要介绍了Spring重试支持Spring Retry的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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

第一步、引入maven依赖

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.3.RELEASE</version>
</parent>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.retry/spring-retry -->
<dependency>
  <groupId>org.springframework.retry</groupId>
  <artifactId>spring-retry</artifactId>
  <version>1.1.2.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.8.6</version>
</dependency>

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

package hello;

import org.springframework.remoting.RemoteAccessException;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

@Service
public class RemoteService {
@Retryable(value= {RemoteAccessException.class},maxAttempts = 3,backoff = @Backoff(delay = 5000l,multiplier = 1))
public void call() throws Exception {
    System.out.println("do something...");
    throw new RemoteAccessException("RPC调用异常");
}
@Recover
public void recover(RemoteAccessException e) {
    System.out.println(e.getMessage());
}
}

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

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

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

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

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

package hello;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.retry.annotation.EnableRetry;

@SpringBootApplication
@EnableRetry
public class Application {

  public static void main(String[] args) throws Exception {
    ApplicationContext annotationContext = new AnnotationConfigApplicationContext("hello");
    RemoteService remoteService = annotationContext.getBean("remoteService", RemoteService.class);
    remoteService.call();
  }
}

运行结果:

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

补充

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

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

相关文章

  • Java HtmlParse提取标签中的值操作

    Java HtmlParse提取标签中的值操作

    这篇文章主要介绍了Java HtmlParse提取标签中的值操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-08-08
  • springboot项目完整后端请求Controller层优雅处理

    springboot项目完整后端请求Controller层优雅处理

    这篇文章主要为大家介绍了springboot项目Controller层代码的优雅处理实例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • springboot接口服务,防刷、防止请求攻击,AOP实现方式

    springboot接口服务,防刷、防止请求攻击,AOP实现方式

    本文介绍了如何使用AOP防止Spring Boot接口服务被网络攻击,通过在pom.xml中加入AOP依赖,创建自定义注解类和AOP切面,以及在业务类中使用这些注解,可以有效地对接口进行保护,测试表明,这种方法有效地防止了网络攻击
    2024-11-11
  • IDEA的Project无法正常显示的问题解决

    IDEA的Project无法正常显示的问题解决

    本文主要介绍了IDEA的Project无法正常显示的问题解决,文中通过图文介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-02-02
  • Java后台接收数据的三种方式(url、form-data与application/json)

    Java后台接收数据的三种方式(url、form-data与application/json)

    本文主要介绍了Java后台接收数据的三种方式(url、form-data与application/json),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07
  • maven中profile的使用

    maven中profile的使用

    本文主要介绍了maven中profile的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-04-04
  • Java Spring的使用注解开发详解

    Java Spring的使用注解开发详解

    这篇文章主要为大家介绍了Java Spring注解开发,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-01-01
  • Java并发编程之原子操作类详情

    Java并发编程之原子操作类详情

    这篇文章主要介绍了Java并发编程之原子操作类详情,文章基于Java并发编程展开相关内容,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-04-04
  • Java合并区间的实现

    Java合并区间的实现

    本文主要介绍了Java合并区间的实现,通过合理使用集合类和排序算法,可以有效地解决合并区间问题,具有一定的参考价值,感兴趣的可以了解一下
    2023-08-08
  • Java将数组转换成字符串的四种方法总结

    Java将数组转换成字符串的四种方法总结

    这篇文章主要给大家介绍了关于Java将数组转换成字符串的四种方法,每种方法都有其适用的场景和优缺点,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-12-12

最新评论