SpringBoot整合Retry實(shí)現(xiàn)錯(cuò)誤重試過(guò)程逐步介紹
引入依賴
<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>開(kāi)啟spring-retry
啟動(dòng)類上增加注解 @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。默認(rèn)為空(如果excludes也為空,則重試所有異常)
- include:可重試的異常類型。默認(rèn)為空(如果excludes也為空,則重試所有異常)
- exclude:無(wú)需重試的異常類型。默認(rèn)為空(如果includes也為空,則重試所有異常)
- maxAttempts:最大重試次數(shù)(包括第一次失敗),默認(rèn)為3次
- backoff:重試等待策略,下面會(huì)在@Backoff中介紹
- recover:表示重試次數(shù)到達(dá)最大重試次數(shù)后的回調(diào)方法
@Backoff注解
- delay,重試之間的等待時(shí)間(以毫秒為單位)
- maxDelay,重試之間的最大等待時(shí)間(以毫秒為單位)
- multiplier,指定延遲的倍數(shù)
- delayExpression,重試之間的等待時(shí)間表達(dá)式
- maxDelayExpression,重試之間的最大等待時(shí)間表達(dá)式
- multiplierExpression,指定延遲的倍數(shù)表達(dá)式
- random,隨機(jī)指定延遲時(shí)間
使用示例
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;
/**
* 模擬網(wǎng)絡(luò)請(qǐng)求異常
* @return
*/
@Retryable(recover = "errorHandler")
public String getResponse() {
count++;
System.out.println("time: " + count);
if (count < 4) {
throw new RuntimeException("count: " + count);
}
return "success";
}
/**
* 錯(cuò)誤處理函數(shù)
* 注意:需要返回 String,否則會(huì)拋出方法找不到異常
* org.springframework.retry.ExhaustedRetryException: Cannot locate recovery method
*
* @param e
* @return
*/
@Recover
public String errorHandler(RuntimeException e) {
System.out.println("errorHandler");
return "ok";
}
}測(cè)試
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 {
@Autowired
private HttpRequest httpRequest;
@Test
public void getResponse(){
httpRequest.getResponse();
}
}
輸出結(jié)果
time: 1
time: 2
time: 3
errorHandler
參考
SpringBoot 中使用 spring-retry 輕松解決重試
到此這篇關(guān)于SpringBoot整合Retry實(shí)現(xiàn)錯(cuò)誤重試過(guò)程逐步介紹的文章就介紹到這了,更多相關(guān)SpringBoot Retry錯(cuò)誤重試內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring?@Transactional事務(wù)失效的原因分析
一個(gè)程序中不可能沒(méi)有事務(wù),Spring中,事務(wù)的實(shí)現(xiàn)方式分為兩種:編程式事務(wù)和聲明式事務(wù)。日常項(xiàng)目中,我們都會(huì)使用聲明式事務(wù)?@Transactional來(lái)實(shí)現(xiàn)事務(wù),本文來(lái)和大家聊聊什么情況會(huì)導(dǎo)致@Transactional事務(wù)失效2022-09-09
SpringBoot中配置Web靜態(tài)資源路徑的方法
這篇文章主要介紹了SpringBoot中配置Web靜態(tài)資源路徑的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
eclipse里沒(méi)有“Dynamic Web Project“這個(gè)選項(xiàng)的問(wèn)題解決
本文主要介紹了eclipse里沒(méi)有“Dynamic Web Project“這個(gè)選項(xiàng)的問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
Java中Controller引起的Ambiguous?mapping問(wèn)題及解決
Java有趣好玩的圖形界面開(kāi)發(fā)八個(gè)案例實(shí)現(xiàn)

