Spring Boot 中一個註解優雅實現重試
重試,在項目需求中是非常常見的,例如遇到網絡波動等,要求某個接口或者是方法可以最多 / 最少調用幾次;實現重試機制,非得用 Retry 這個重試框架嗎?那肯定不是,相信很多夥伴手寫一下控制流程的邏輯也可以達到重試的目的。
那麼用 Retry 的好處是什麼?簡單來說,就是優雅。
Retry 重試框架,支持 AOP 切入的方式使用,而且能使用註解;想想,重試次數、重試延遲、重試觸發條件、重試的回調方法等等我們都能很輕鬆結合註解以一種類似配置參數的方式去實現,優雅無疑。
那麼,我們接下來就來一起使用 Springboot 整合這個 Retry 重試框架:
首先是 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 http://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.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.mail</groupId>
<artifactId>elegant</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>elegant</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.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId >
<artifactId>aspectjweaver</artifactId >
<version>1.6.11</version >
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
然後創建一個測試重試的TestRetryService.java
:
/**
* @Author : JCccc
* @Description :
**/
public interface TestRetryService {
int dignifiedTest(int code) throws Exception;
}
然後是TestRetryServiceImpl.java
:
import com.mail.elegant.service.TestRetryService;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import java.time.LocalTime;
/**
* @Author : JCccc
* @Description :
**/
@Service
public class TestRetryServiceImpl implements TestRetryService {
@Override
@Retryable(value = Exception.class,maxAttempts = 3,backoff = @Backoff(delay = 2000,multiplier = 1.5))
public int dignifiedTest(int code) throws Exception{
System.out.println("dignifiedTest被調用,時間:"+LocalTime.now());
if (code==0){
throw new Exception("情況不對頭!");
}
System.out.println("dignifiedTest被調用,情況對頭了!");
return 200;
}
@Recover
public int recover(Exception e){
System.out.println("回調方法執行!!!!");
//記日誌到數據庫 或者調用其餘的方法
return 400;
}
}
到這裏,已經整合完畢,最後剩下測試了,在測試前,我們先一起來看看代碼裏面的關鍵信息的意義:
可以看到代碼裏面,這個方法上面加上了註解 ,
@Retryable(value = Exception.class,maxAttempts = 3,backoff = @Backoff(delay = 2000,multiplier = 1.5,maxDelay=360000L))
-
@Retryable:註解方式標記當前方法會使用重試機制
-
裏面的 value:重試的觸發機制,當遇到 Exception 異常的時候,觸發;
-
maxAttempts:重試的次數(包括第一次調用,也就是說如果設置 3 次,調用一次後,如果一直失敗觸發重試,那麼還當前方法還會調用 2 次);
-
delay:重試的延遲時間,也就是距離上一次重試方法調用的間隔,單位毫秒
-
multiplier:delay 間隔時間的倍數,也就是說,第一次重試間隔如果是 2000ms,那第二次重試的時候就是 2000ms 乘以這個倍數 1.5,就是 3000ms;
-
maxDelay: 重試次數之間的最大時間間隔, 默認爲 0, 即忽略, 如果小於 delay 的設置, 則默認爲 30000L;
再來看下面的這個小方法:
@Recover
public int recover(Exception e){
System.out.println("回調方法執行!!!!");
//記日誌到數據庫 或者調用其餘的方法
return 400;
}
這個方法用到了@Recover
,也就是用註解方式標記當期方法爲回調方法,可以看到傳參裏面寫的是 Exception e
,這個是作爲回調的接頭暗號(重試次數用完了,還是失敗,我們拋出這個Exception e
通知觸發這個回調方法)。
PS:該回調方法與重試方法寫在同一個實現類裏面。
然後在啓動類加上開啓重試註解:
@EnableRetry
好了,基本簡單講解完畢,接下來測試看看什麼效果,
創建一個TestController.java
,寫過簡單的測試方法:
import com.mail.elegant.service.TestRetryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author : JCccc
* @Description :
**/
@RestController
public class TestController {
@Autowired
TestRetryService testRetryServiceImpl;
@GetMapping("/testRetry")
public String testRetry() throws Exception {
int code=0;
int result = testRetryServiceImpl.dignifiedTest(code);
return "result:"+result;
}
}
我們這個測試模擬的場景是,傳值 code,一直是 0;然後業務方法判斷如果是 0,代表業務不通,失敗(網絡波動了或者是等等),然後就是觸發重試,最後如果重試幾次都不成功,然後調用回調方法(可以進行日誌記錄或者調用其他業務方法等等)。
我們調用接口看看效果:
接口返回了 400,是回調方法返回的:
看看控制檯輸出情況:
OK,到此,重試整合及測試已經完畢。
ps:如果不需要回調方法,可以直接不寫回調方法,那麼實現的效果是,重試次數完了後,如果還是沒成功沒符合業務判斷,就拋出異常
_感謝閱讀,希望對你有所幫助 :) _
來源:blog.csdn.net/qq_35387940/article
/details/99676114
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源:https://mp.weixin.qq.com/s/qUN4ieLi0EBZAWCcVObVHQ