在分布式系統(tǒng)中,為了保證數(shù)據(jù)分布式事務(wù)的強(qiáng)一致性,大家在調(diào)用RPC接口或者發(fā)送MQ時(shí),針對(duì)可能會(huì)出現(xiàn)網(wǎng)絡(luò)抖動(dòng)請(qǐng)求超時(shí)情況采取一下重試操作。大家用的最多的重試方式就是MQ了,但是如果你的項(xiàng)目中沒有引入MQ,那就不方便了,本文主要介紹一下如何使用Spring Retry實(shí)現(xiàn)重試操作。

創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括青山網(wǎng)站建設(shè)、青山網(wǎng)站制作、青山網(wǎng)頁制作以及青山網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,青山網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到青山省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
1. 添加maven依賴
<dependency> <groupId>org.springframework.retry</groupId> <artifactId>spring-retry</artifactId> <version>1.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.5.4</version> </dependency>
2. 在啟動(dòng)里添加重試配置
@SpringBootApplication
@EnableRetry
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
3. 編寫Service
@Service
public class RemoteService {
private static final Logger logger = LoggerFactory.getLogger(TestController.class);
@Retryable(value= {BusinessException.class},maxAttempts = 3,backoff = @Backoff(delay = 5000l,multiplier = 2))
public void call() throws Exception {
logger.info("do something...");
throw new BusinessException("RPC調(diào)用異常");
}
@Recover
public void recover(BusinessException e) {
logger.info(" --------------------------- ");
logger.info(e.getMessage());
}
}
4. 編寫Controller
@RestController
@RequestMapping("/test")
public class TestController {
private static final Logger logger = LoggerFactory.getLogger(TestController.class);
@Autowired
private RemoteService remoteService;
@RequestMapping("/test")
public String login() throws Exception {
remoteService.call();
return String.valueOf("11");
}
5. 訪問http://localhost:8080/test/test
6. 測(cè)試日志
2017-07-25 19:28:07 [INFO]-[http-nio-53602-exec-1]-[com.test.retry.service.RemoteService.call(RemoteService.java:19)] do something...
2017-07-25 19:28:12 [INFO]-[http-nio-53602-exec-1]-[com.test.retry.service.RemoteService.call(RemoteService.java:19)] do something...
2017-07-25 19:28:22 [INFO]-[http-nio-53602-exec-1]-[com.test.retry.service.RemoteService.call(RemoteService.java:19)] do something...
2017-07-25 19:28:22 [INFO]-[http-nio-53602-exec-1]-[com.test.retry.service.RemoteService.recover(RemoteService.java:24)] ---------------------------
2017-07-25 19:28:22 [INFO]-[http-nio-53602-exec-1]-[com.test.retry.service.RemoteService.recover(RemoteService.java:25)] RPC調(diào)用異常
7. 相關(guān)配置說明
@EnableRetry能否重試。當(dāng)proxyTargetClass屬性為true時(shí),使用CGLIB代理。默認(rèn)使用標(biāo)準(zhǔn)JAVA注解。在spring Boot中此參數(shù)寫在程序入口即可。
@Retryable 標(biāo)注此注解的方法在發(fā)生異常時(shí)會(huì)進(jìn)行重試
value:指定處理的異常類
include:指定處理的異常類和value一樣,默認(rèn)為空,當(dāng)exclude也為空時(shí),默認(rèn)所有異常
exclude:指定異常不處理,默認(rèn)空,當(dāng)include也為空時(shí),默認(rèn)所有異常
maxAttempts:最大重試次數(shù)。默認(rèn)3次
backoff: 重試等待策略。默認(rèn)使用@Backoff注解
@Backoff 重試等待策略
不設(shè)置參數(shù)時(shí),默認(rèn)使用FixedBackOffPolicy(指定等待時(shí)間),重試等待1000ms
設(shè)置delay,使用FixedBackOffPolicy(指定等待時(shí)間),重試等待填寫的時(shí)間
設(shè)置delay和maxDealy時(shí),重試等待在這兩個(gè)值之間均態(tài)分布
設(shè)置delay、maxDealy、multiplier,使用 ExponentialBackOffPolicy(指數(shù)級(jí)重試間隔的實(shí)現(xiàn) ),multiplier即指定延遲倍數(shù),比如delay=5000l,multiplier=2,則第一次重試為5秒,第二次為10秒,第三次為20秒……
@Recover 用于@Retryable重試失敗后處理方法,此注解注釋的方法參數(shù)一定要是@Retryable拋出的異常,否則無法識(shí)別,可以在該方法中進(jìn)行日志處理。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
文章名稱:spring-retry簡(jiǎn)單使用方法
網(wǎng)頁地址:http://www.chinadenli.net/article22/pechjc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)公司、品牌網(wǎng)站制作、動(dòng)態(tài)網(wǎng)站、響應(yīng)式網(wǎng)站、自適應(yīng)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)