当前位置: 首页 > news >正文

Java:SpringBoot 整合Spring-Retry实现错误重试

SpringBoot 整合Spring-Retry可以实现错误重试

目录

    • 引入依赖
    • 开启spring-retry
    • 使用重试注解
      • `@Retryable` 注解
      • `@Backoff` 注解
    • 测试
    • 参考

引入依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency><groupId>org.springframework.retry</groupId><artifactId>spring-retry</artifactId>
</dependency>

完整依赖pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.7</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>demo</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- spring-retry --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency><dependency><groupId>org.springframework.retry</groupId><artifactId>spring-retry</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build></project>

开启spring-retry

启动类上增加注解 @EnableRetry

package com.example.demo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.retry.annotation.EnableRetry;@EnableRetry
@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}

使用重试注解

@Retryable 注解

  • value,可重试的异常类型。含义同include。默认为空(如果excludes也为空,则重试所有异常)
  • include:可重试的异常类型。默认为空(如果excludes也为空,则重试所有异常)
  • exclude:无需重试的异常类型。默认为空(如果includes也为空,则重试所有异常)
  • maxAttempts:最大重试次数(包括第一次失败),默认为3次
  • backoff:重试等待策略,下面会在@Backoff中介绍
  • recover:表示重试次数到达最大重试次数后的回调方法

@Backoff 注解

  • delay,重试之间的等待时间(以毫秒为单位)
  • maxDelay,重试之间的最大等待时间(以毫秒为单位)
  • multiplier,指定延迟的倍数
  • delayExpression,重试之间的等待时间表达式
  • maxDelayExpression,重试之间的最大等待时间表达式
  • multiplierExpression,指定延迟的倍数表达式
  • random,随机指定延迟时间

使用示例

package com.example.demo.component;import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Component;@Component
public class HttpRequest {private int count = 0;/*** 模拟网络请求异常* @return*/@Retryable(recover = "errorHandler")public String getResponse() {count++;System.out.println("time: " + count);if (count < 4) {throw new RuntimeException("count: " + count);}return "success";}/*** 错误处理函数* 注意:需要返回 String,否则会抛出方法找不到异常* org.springframework.retry.ExhaustedRetryException: Cannot locate recovery method** @param e* @return*/@Recoverpublic String errorHandler(RuntimeException e) {System.out.println("errorHandler");return "ok";}
}

测试

package com.example.demo.component;import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
public class HttpRequestTest {@Autowiredprivate HttpRequest httpRequest;@Testpublic void getResponse(){httpRequest.getResponse();}
}

输出结果

time: 1
time: 2
time: 3
errorHandler

参考

  • SpringBoot 中使用 spring-retry 轻松解决重试

相关文章:

Java:SpringBoot 整合Spring-Retry实现错误重试

SpringBoot 整合Spring-Retry可以实现错误重试 目录引入依赖开启spring-retry使用重试注解Retryable 注解Backoff 注解测试参考引入依赖 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactI…...

MyBatis学习笔记(二) —— 搭建MyBatis项目

2、搭建MyBatis 2.1、开发环境 IDE&#xff1a;idea 2019.2 构建工具&#xff1a;maven 3.5.4 MySQL版本&#xff1a;MySQL 8 MyBatis版本&#xff1a;MyBatis 3.5.7 MySQL不同版本的注意事项 1、驱动类 driver-class-name MySQL 5版本使用jdbc5驱动&#xff0c;驱动类使用…...

linux服务器上Docker中安装jenkins

前言 Jenkins是开源CI&CD软件领导者&#xff0c; 提供超过1000个插件来支持构建、部署、自动化&#xff0c; 满足任何项目的需要。 本文主要提供通过docker安装jenkins镜像&#xff0c;并配置nginx反向代理页面配置和使用。通过jenkins完成项目的自动部署。 我在安装之前…...

自考都有哪些科目?怎么搭配报考?

第一次自考科目搭配 先报理论课&#xff0c;熟悉学习和考试套路 参考搭配模式&#xff1a; 一、全报考公共课 公共课难度较低&#xff0c;通过率高&#xff0c;复习起来比较轻松。对于不确定考什么专业&#xff0c;后期想换专业的同学&#xff0c;考过公共课&#xff0c…...

HIVE --- 高级查询

目录 CTE和嵌套查询 嵌套查询 关联查询&#xff08;join&#xff09; MapJoin MapJoin操作在Map端完成 开启MapJoin操作 MAPJOIN不支持的操作 union 数据交换&#xff08;import/export&#xff09; 数据排序 order by sort by distribute by cluster by CTE和嵌…...

【手撕源码】vue2.x双向数据绑定原理

&#x1f431; 个人主页&#xff1a;不叫猫先生 &#x1f64b;‍♂️ 作者简介&#xff1a;前端领域新星创作者、阿里云专家博主&#xff0c;专注于前端各领域技术&#xff0c;共同学习共同进步&#xff0c;一起加油呀&#xff01; &#x1f4ab;系列专栏&#xff1a;vue3从入门…...

Allegro如何显示层叠Options和Find操作界面

Allegro如何显示层叠Options和Find操作界面 Allegro常规有三大操作界面,层叠,Options和Find,如下图 软件第一次启动的时候,三大界面是关闭的,下面介绍如何把它们打开,具体操作步骤如下 点击菜单上的View点击Windows...

【数据结构】双向链表

目录 数据结构之双向链表&#xff1a;&#xff1a; List.h List.c 1.创建返回链表的头结点 2.双向链表初始化 3.双向链表打印 4.双向链表销毁 5.双向链表尾插 6.双向链表尾删 7.双向链表头插 8.双向链表头删 9.双向链表查找 10.双向链表在pos前插入 11.双向链表删除pos位置 12…...

Editor工具开发基础三:自定义组件菜单拓展 CustomEditor

一.创建脚本路径 创建脚本路径不再限制 一般写在自定义组件类的下边二.特性CustomEditor 定义主设计图面由自定义代码实现数组的编辑器。两个构造函数1.public CustomEditor(Type inspectedType);2.public CustomEditor(Type inspectedType, bool editorForChildClasses);参数意…...

拒绝摆烂!神仙网站Python自学,一路从入门闯到最后,边学边玩

前言给大家推荐3个边玩边学python的网站在刚接触编程&#xff0c;培养对其持续的兴趣是最最重要的事情辣&#xff01;&#xff01;&#xff01;因为前期需要大量的基础代码知识积累&#xff0c;这个过程对于不少人来说还是挺枯燥的&#xff0c;很有可能学到一半就放弃了&#x…...

Linux基础命令-locate快速查找文件

文章​​​​​​​目录 locate 命令介绍 语法格式 基本参数 参考实例 1&#xff09;查找1.txt相关的文件 2&#xff09;查找包含pass和txt都有的文件 3&#xff09;只匹配文件名&#xff0c;有路径的情况下不进行匹配 4&#xff09;匹配不区分大小写的文件 5&#…...

揭穿数据分析的六大谎言

目前许多企业在决策时仍沿用以往的个人经验&#xff0c;没有用数据说话&#xff0c;这在实际决策运行时会出现很多问题。在数据分析行业发展成熟的国家&#xff0c;90%的市场决策和经营决策都是通过数据分析研究确定的。用数据说话&#xff0c;重视定量分析&#xff0c;也逐渐成…...

LinkSLA智能运维技术派-Redis的监控

Redis是一个开源&#xff0c;内存存储的数据服务器&#xff0c;可用作数据库、高速缓存和消息队列代理等场景。 首先我们对内存进行监控&#xff0c;主要指标如下&#xff1a; - used_memory:使用内存 - used_memory_rss:从操作系统分配的内存 - mem_fragmentation_ratio:内…...

Hugging face 模型微调学习:T5-base的微调

最近想做一点文本生成的小实验&#xff0c;无意发现了NLPer应该了解到了一个网站&#xff1a;Hugging face。 Hugging face 在 github上开源了一个Transformers库&#xff0c;允许用户上传和下载的预训练的模型&#xff0c;并进行原有模型的基础上进行微调。如此&#xff0c;使…...

JavaScript 测试 Prototype

文章目录JavaScript 测试 Prototype引用 PrototypePrototype 描述测试 PrototypeJavaScript 测试 Prototype 测试 JavaScript 框架库 - Prototype 引用 Prototype 如需测试 JavaScript 库&#xff0c;您需要在网页中引用它。 为了引用某个库&#xff0c;请使用 <!DOCTYP…...

pnpm / yarn / npm管理依赖包

pnpm pnpm官网&#xff1a;https://pnpm.io/zh/ pnpm安装方式有很多&#xff0c;详见官网。 用最简单的npm来安装pnpm&#xff1a;npm install -g pnpm pnpm安装依赖包 pnpm install # 安装所有项目中的依赖包 pnpm install vue # 安装依赖到dependencies pnpm in…...

注意力机制详解系列(一):注意力机制概述

&#x1f468;‍&#x1f4bb;作者简介&#xff1a; 大数据专业硕士在读&#xff0c;CSDN人工智能领域博客专家&#xff0c;阿里云专家博主&#xff0c;专注大数据与人工智能知识分享。 &#x1f389;专栏推荐&#xff1a; 目前在写CV方向专栏&#xff0c;更新不限于目标检测、…...

搜索引擎 Elasticsearch 的三大坑

搜索引擎的坑 ES 搜索引擎系列文章汇总&#xff1a; 一、别只会搜日志了&#xff0c;求你懂点原理吧 二、ES 终于可以搜到”悟空哥“了&#xff01; 三、1W字&#xff5c;40 图&#xff5c;硬核 ES 实战 本文主要内容如下&#xff1a; 搜索引擎现在是用得越来越多了&#…...

运营级手机直播平台源码 短视频直播带货APP源码

短视频直播带货APP源码 全开源原生直播APP源码 前端&#xff1a;原生APP 安卓端&#xff1a;Java 苹果端&#xff1a;OC 后台&#xff1a;PHP 数据库&#xff1a;Mysql 技术框架&#xff1a;Thinkphp5.1 系统特色功能包括&#xff1a;礼物系统&#xff1b;提现方式&#…...

http/HTTPS相关的一些知识

2、http和https HTTP&#xff0c;超文本传输协议&#xff0c;规定了浏览器和服务器之间数据传输的规则。HTTP 是应用层协议&#xff0c;它以 TCP&#xff08;传输层&#xff09;作为底层协议&#xff0c;默认端口为 80。 http的通信过程&#xff1a;服务器在80端口等待客户的请…...

Oracle查询表空间大小

1 查询数据库中所有的表空间以及表空间所占空间的大小 SELECTtablespace_name,sum( bytes ) / 1024 / 1024 FROMdba_data_files GROUP BYtablespace_name; 2 Oracle查询表空间大小及每个表所占空间的大小 SELECTtablespace_name,file_id,file_name,round( bytes / ( 1024 …...

《基于Apache Flink的流处理》笔记

思维导图 1-3 章 4-7章 8-11 章 参考资料 源码&#xff1a; https://github.com/streaming-with-flink 博客 https://flink.apache.org/bloghttps://www.ververica.com/blog 聚会及会议 https://flink-forward.orghttps://www.meetup.com/topics/apache-flink https://n…...

uniapp中使用aixos 报错

问题&#xff1a; 在uniapp中使用aixos&#xff0c;运行后报如下错误&#xff1a; AxiosError: There is no suitable adapter to dispatch the request since : - adapter xhr is not supported by the environment - adapter http is not available in the build 解决方案&…...

Python 包管理器 uv 介绍

Python 包管理器 uv 全面介绍 uv 是由 Astral&#xff08;热门工具 Ruff 的开发者&#xff09;推出的下一代高性能 Python 包管理器和构建工具&#xff0c;用 Rust 编写。它旨在解决传统工具&#xff08;如 pip、virtualenv、pip-tools&#xff09;的性能瓶颈&#xff0c;同时…...

NXP S32K146 T-Box 携手 SD NAND(贴片式TF卡):驱动汽车智能革新的黄金组合

在汽车智能化的汹涌浪潮中&#xff0c;车辆不再仅仅是传统的交通工具&#xff0c;而是逐步演变为高度智能的移动终端。这一转变的核心支撑&#xff0c;来自于车内关键技术的深度融合与协同创新。车载远程信息处理盒&#xff08;T-Box&#xff09;方案&#xff1a;NXP S32K146 与…...

scikit-learn机器学习

# 同时添加如下代码, 这样每次环境(kernel)启动的时候只要运行下方代码即可: # Also add the following code, # so that every time the environment (kernel) starts, # just run the following code: import sys sys.path.append(/home/aistudio/external-libraries)机…...

Caliper 配置文件解析:fisco-bcos.json

config.yaml 文件 config.yaml 是 Caliper 的主配置文件,通常包含以下内容: test:name: fisco-bcos-test # 测试名称description: Performance test of FISCO-BCOS # 测试描述workers:type: local # 工作进程类型number: 5 # 工作进程数量monitor:type: - docker- pro…...

Ubuntu Cursor升级成v1.0

0. 当前版本低 使用当前 Cursor v0.50时 GitHub Copilot Chat 打不开&#xff0c;快捷键也不好用&#xff0c;当看到 Cursor 升级后&#xff0c;还是蛮高兴的 1. 下载 Cursor 下载地址&#xff1a;https://www.cursor.com/cn/downloads 点击下载 Linux (x64) &#xff0c;…...

SpringAI实战:ChatModel智能对话全解

一、引言&#xff1a;Spring AI 与 Chat Model 的核心价值 &#x1f680; 在 Java 生态中集成大模型能力&#xff0c;Spring AI 提供了高效的解决方案 &#x1f916;。其中 Chat Model 作为核心交互组件&#xff0c;通过标准化接口简化了与大语言模型&#xff08;LLM&#xff0…...

FTXUI::Dom 模块

DOM 模块定义了分层的 FTXUI::Element 树&#xff0c;可用于构建复杂的终端界面&#xff0c;支持响应终端尺寸变化。 namespace ftxui {...// 定义文档 定义布局盒子 Element document vbox({// 设置文本 设置加粗 设置文本颜色text("The window") | bold | color(…...