Spring重试支持Spring Retry

Spring重试支持Spring Retry第一步 引入 maven 依赖 dependencygr springframew boot groupIdartif boot starter web artifactId dependencyht mvnrepositor com artifact org springframew

第一步、引入maven依赖

 
   
   
     org.springframework.boot 
    
   
     spring-boot-starter-parent 
    
   
     1.5.3.RELEASE 
    
   
   
   
     org.springframework.boot 
    
   
     spring-boot-starter-web 
    
   
   
   
   
     org.springframework.retry 
    
   
     spring-retry 
    
   
     1.1.2.RELEASE 
    
   
   
   
     org.aspectj 
    
   
     aspectjweaver 
    
   
     1.8.6 
    
   

第二步、添加@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

补充
对于非幂等的请求(比如新增,更新操作),千万不要使用重试,对数据一致性会造成很大影响。另外对Spring Retry实现原理感兴趣的可以看下这篇博客

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/203808.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月19日 下午9:32
下一篇 2026年3月19日 下午9:32


相关推荐

  • Linux查找文件路径

    Linux查找文件路径Linux 查找文件路径

    2026年3月16日
    2
  • 《JavaScript 模式》读书笔记(2)— 基本技巧1

    这篇文章的主要内容,介绍了一些js编程中的基本技巧,其实这些技巧,大家在开发的过程中,或多或少都在使用,或者已经可以熟练的应用于自己的代码或项目中了。那么,这篇文章,就一起来回顾下这些“基本技巧”。

    2022年3月25日
    41
  • java正则驼峰_javascript 驼峰算法

    java正则驼峰_javascript 驼峰算法JS 字符串转成驼峰的三种方法问题描述 写一个字符串转成驼峰的方法 例 border bottom color gt borderBottom 方法一 操作字符串数组 functiontran str varstrArr str split for vari 1 i 学习 JavaScript 数据结构和算法 部分三 看了 LoianeGroner 著的 学

    2026年3月3日
    2
  • 卷积神经网络实现图像识别及过程可视化

    卷积神经网络实现图像识别及过程可视化卷积神经网络实现图像识别及过程可视化本博文提供经典的卷积神经网络实现代码,将CNN的工具类等代码分别封装,并提供接口自行替换使用的模型(可以换成自己的神经网络及图片样本),代码中提供模型保存和读取,并对卷积层的计算结果反卷积还原成图片输出到tensorboard中,最后可以在tensorboard中观察CNN训练的过程和结果数据,并查看过程中卷积核提取的具体特征。实验环…

    2022年6月8日
    34
  • 缺货!“养龙虾”带火了新生意→

    缺货!“养龙虾”带火了新生意→

    2026年3月14日
    3
  • matlab中的normrnd函数,MATLAB中normrnd函数的使用方法

    matlab中的normrnd函数,MATLAB中normrnd函数的使用方法基本结构为 1 r normrnd mu sigma 生成服从正态分布 mu 参数代表均值 sigma 参数代表标准差 的随机数 输入的向量或矩阵 mu 和 sigma 必须形式相同 输出 r 也和它们形式相同 标量输入将被扩展成和其它输入具有相同维数的矩阵 2 r normrnd mu sigma m 生成服从正态分布 mu 参数代表均值 sigma 参数代表标准差 的随机数矩阵 矩阵的形式由 m 定义 m 是一

    2026年3月18日
    1

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号