您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“SpringBoot整合Retry如何實現錯誤重試”,內容詳細,步驟清晰,細節處理妥當,希望這篇“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>
啟動類上增加注解 @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); } }
value,可重試的異常類型。含義同include。默認為空(如果excludes也為空,則重試所有異常)
include:可重試的異常類型。默認為空(如果excludes也為空,則重試所有異常)
exclude:無需重試的異常類型。默認為空(如果includes也為空,則重試所有異常)
maxAttempts:最大重試次數(包括第一次失敗),默認為3次
backoff:重試等待策略,下面會在@Backoff中介紹
recover:表示重試次數到達最大重試次數后的回調方法
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 */ @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; @SpringBootTest public class HttpRequestTest { @Autowired private HttpRequest httpRequest; @Test public void getResponse(){ httpRequest.getResponse(); } }
輸出結果
time: 1
time: 2
time: 3
errorHandler
讀到這里,這篇“SpringBoot整合Retry如何實現錯誤重試”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。