前言
作為后端程序員,我們的日常工作就是調(diào)用一些第三方服務(wù),將數(shù)據(jù)存入數(shù)據(jù)庫,返回信息給前端。但你不能保證所有的事情一直都很順利。像有些第三方API,偶爾會(huì)出現(xiàn)超時(shí)。此時(shí),我們要重試幾次,這取決于你的重試策略。
下面舉一個(gè)我在日常開發(fā)中多次看到的例子:
public interface OutSource {
List;
}
@Service
public class OutSourceImpl implements OutSource {
static Random random = new Random();
@Override
public List{
//mock failure
if (random.nextInt(2) == 1)
throw new TimeOutException();
return List.of(1, 2, 3);
}
}
@Slf4j
@Service
public class ManuallyRetryService {
@Autowired
private OutSource outSource;
public List{
log.info("trigger time:{}", retryTimes);
if (retryTimes > 3) {
return List.of();
}
try {
List
看看上面這段代碼,我認(rèn)為它可以正常工作,當(dāng)retryTimes
達(dá)到4時(shí),無論如何我們都會(huì)得到最終結(jié)果。但是你覺得寫的好嗎?優(yōu)雅嗎?下面我來介紹Spring中的一個(gè)組件:spring-retry
,我們不妨來試一試。
Spring-Retry介紹使用
spring-retry
是Spring中的提供的一個(gè)重試框架,提供了注解的方式,在不入侵原有業(yè)務(wù)邏輯代碼的方式下,優(yōu)雅的實(shí)現(xiàn)重處理功能。
安裝依賴
- 如果你的是gradle應(yīng)用,引入下面的依賴
implementation 'org.springframework.boot:spring-boot-starter-aop''org.springframework.boot:spring-boot-starter-aop'
implementation 'org.springframework.retry:spring-retry'
- 如果你的項(xiàng)目使用的是maven項(xiàng)目,引入下面的依賴
<dependency>
<groupId>org.springframework.retry<span class="hljs-name"groupId>
<artifactId>spring-retry<span class="hljs-name"artifactId>
<span class="hljs-name"dependency>
<dependency>
<groupId>org.springframework.boot<span class="hljs-name"groupId>
<artifactId>spring-boot-starter-aop<span class="hljs-name"artifactId>
<span class="hljs-name"dependency>
啟用重試功能
添加@EnableRetry
注解在入口的類上從而啟用功能。
@SpringBootApplication
//看過來
@EnableRetry
public class TestSpringApplication {
public static void main(String[] args) {
SpringApplication.run(TestSpringApplication.class, args);
}
}
應(yīng)用
我們以前面的為例,看看怎么使用,如下面的代碼:
public interface OutSource {
List;
}
@Service
public class OutSourceImpl implements OutSource {
static Random random = new Random();
@Override
public List{
//mock failure will throw an exception every time
throw new TimeOutException();
}
}
@Slf4j
@Service
public class RetryableService {
@Autowired
private OutSource outSource;
// 看這里
@Retryable(value = {TimeOutException.class}, maxAttempts = 3)
public List{
log.info("trigger timestamp:{}", System.currentTimeMillis() / 1000);
List
- 關(guān)鍵在于
Service
層中的實(shí)現(xiàn)類中添加了@Retryable
注解,實(shí)現(xiàn)了重試, 指定value是TimeOutException
異常會(huì)進(jìn)行重試,最大重試maxAttempts
3次。
驗(yàn)證
這一次,當(dāng)我們?cè)L問http://localhost:8080/retryable時(shí),我們將看到瀏覽器上的結(jié)果失敗。然后在你的終端上看到:
INFO 66776 --- [nio-9997-exec-1] c.m.testspring.service.RetryableService : trigger timestamp:1668236840
INFO 66776 --- [nio-9997-exec-1] c.m.testspring.service.RetryableService : trigger timestamp:1668236841
INFO 66776 --- [nio-9997-exec-1] c.m.testspring.service.RetryableService : trigger timestamp:1668236842
ERROR 66776 --- [nio-9997-exec-1] c.m.t.controller.RetryTestController : retryable final exception
總結(jié)
本文分享了spring-retry
重試框架最基礎(chǔ)的使用,可以無侵入業(yè)務(wù)代碼進(jìn)行重試。關(guān)于spring-retry
更多的使用建議可以自己去官網(wǎng)https://github.com/spring-projects/spring-retry 探索。
-
API
+關(guān)注
關(guān)注
2文章
1556瀏覽量
63353 -
數(shù)據(jù)庫
+關(guān)注
關(guān)注
7文章
3885瀏覽量
65626 -
spring
+關(guān)注
關(guān)注
0文章
340瀏覽量
14816 -
SpringBoot
+關(guān)注
關(guān)注
0文章
175瀏覽量
293
發(fā)布評(píng)論請(qǐng)先 登錄
如何把第三方庫加到PROTEUS中?
如何在LabVIEW中嵌入第三方軟件界面
labview 通過API調(diào)用第三方軟件 無法通過ID獲取窗口元素的句柄
第三方dll調(diào)用問題!!!
LabVIEW與第三方軟件交互問題
國內(nèi)知名第三方檢測(cè)認(rèn)證機(jī)構(gòu)排名
關(guān)于LabVIEW調(diào)用第三方exe,如何去控制第三方exe按鈕的問題
頭文件中包含第三方文件
鴻蒙開源第三方組件資料合集
在Framework下調(diào)用第三方的C++算法庫
銀行和第三方支付:戀愛進(jìn)行時(shí)
鴻蒙開發(fā)中怎么引入第三方庫
調(diào)用第三方 API 接口會(huì)遇到哪些問題
調(diào)用第三方API接口會(huì)遇到哪些問題?如何解決?

評(píng)論