您好,欢迎访问一九零五行业门户网

SpringBoot整合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>
完整依赖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@springbootapplicationpublic 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;@componentpublic 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 */ @recover public 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;@springboottestpublic class httprequesttest { @autowired private httprequest httprequest; @test public void getresponse(){ httprequest.getresponse(); }}
输出结果
time: 1
time: 2
time: 3
errorhandler
以上就是springboot整合retry如何实现错误重试的详细内容。
其它类似信息

推荐信息