SpringBoot 整合 Spring Retry——优雅实现接口重试 接口调用失败时直接返回错误不是最佳选择有些场景重试一下就成功了。Spring Retry 提供了声明式重试机制。一、引入依赖dependencygroupIdorg.springframework.retry/groupIdartifactIdspring-retry/artifactId/dependencydependencygroupIdorg.aspectj/groupIdartifactIdaspectjweaver/artifactId/dependency二、开启重试SpringBootApplicationEnableRetrypublicclassSeckillApplication{publicstaticvoidmain(String[]args){SpringApplication.run(SeckillApplication.class,args);}}三、使用 RetryableServicepublicclassPaymentService{privatestaticfinalLoggerlogLoggerFactory.getLogger(PaymentService.class);Retryable(value{RemoteAccessException.class,TimeoutException.class},maxAttempts3,backoffBackoff(delay1000,multiplier2))publicbooleanrefund(StringorderNo){log.info(退款请求: {},orderNo);// 调用第三方支付接口returnthirdPartyRefund(orderNo);}Recoverpublicbooleanrecover(RemoteAccessExceptione,StringorderNo){log.error(退款失败记录到异常表: {},orderNo,e);// 记录到失败表人工处理returnfalse;}}四、自定义重试策略ConfigurationpublicclassRetryConfig{BeanpublicRetryTemplateretryTemplate(){RetryTemplatetemplatenewRetryTemplate();// 重试策略最多重试5次间隔递增ExponentialBackOffPolicybackOffnewExponentialBackOffPolicy();backOff.setInitialInterval(1000);backOff.setMultiplier(2);backOff.setMaxInterval(10000);template.setBackOffPolicy(backOff);// 异常判断哪些异常需要重试SimpleRetryPolicyretryPolicynewSimpleRetryPolicy();retryPolicy.setMaxAttempts(5);template.setRetryPolicy(retryPolicy);returntemplate;}}五、编程式重试ServicepublicclassOrderService{AutowiredprivateRetryTemplateretryTemplate;publicbooleanprocessOrder(LongorderId){returnretryTemplate.execute(context-{log.info(处理订单第{}次尝试,context.getRetryCount()1);returndoProcess(orderId);},context-{log.error(最终失败,context.getLastThrowable());returnfalse;});}}六、重试配置spring:retry:max-attempts:3# 全局最大重试次数七、秒杀系统应用ServicepublicclassSeckillService{Retryable(valueOptimisticLockException.class,maxAttempts3,backoffBackoff(delay200))publicbooleandeductStock(LongproductId){// 乐观锁失败时自动重试returnproductMapper.updateStock(productId)0;}} 觉得有用的话点赞 关注【张老师技术栈】吧

相关新闻

最新新闻

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现 【免费下载链接】serenity The Serenity Operating System 🐞 项目地址: https://gitcode.com/GitHub_Trending/se/serenity 导读 本文以 getopt(3) 手册 为核心&a…

2026/10/5 3:18:56
轻量服务器还是ECS?大促云服务器选购与避坑实战指南

轻量服务器还是ECS?大促云服务器选购与避坑实战指南

每年大促节点,群里永远有人在问同一个问题:“38元的轻量服务器到底怎么抢?为什么我每次点进去都是已售罄?68元直购和99元的ECS我到底选哪个?”作为一个常年帮团队和自己采购云服务器的老用户,我太清楚这种纠…

2026/10/5 3:42:18
为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南

为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南

为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南 【免费下载链接】agents Multi-harness agentic plugin marketplace for Claude Code, Codex, Cursor, OpenCode, GitHub Copilot, and Google Antigravity 项目地址:…

2026/10/3 16:42:22
PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署

PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署

PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署 【免费下载链接】PaddleOCR Turn any PDF or image document into structured data for your AI. A powerful, lightweight OCR toolkit that bridges the gap between i…

2026/10/5 16:06:34
Spring源码解析:构造器注入的类型转换与候选匹配机制

Spring源码解析:构造器注入的类型转换与候选匹配机制

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/5 5:51:09
openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由

openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由

openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由 【免费下载链接】openai-agents-python A lightweight, powerful framework for multi-agent workflows 项目地址: https://gitcode.com/GitHub_Trending/op/openai-agents-pyth…

2026/10/5 5:40:36

日新闻

周新闻

月新闻