spring-retry簡單使用方法
在分布式系統(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)重試操作。
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. 測試日志
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í)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java中l(wèi)ong數(shù)據(jù)類型轉(zhuǎn)換為int類型
這篇文章主要講解Java中基本數(shù)據(jù)類型,java long 類型與其java int類型的轉(zhuǎn)換的幾種方法,希望能給大家做一個(gè)參考2016-07-07java使用MulticastSocket實(shí)現(xiàn)組播
這篇文章主要為大家詳細(xì)介紹了java使用MulticastSocket實(shí)現(xiàn)組播,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01Go反射底層原理及數(shù)據(jù)結(jié)構(gòu)解析
這篇文章主要介紹了Go反射底層原理及數(shù)據(jù)結(jié)構(gòu)解析,反射的實(shí)現(xiàn)和interface的組成很相似,都是由“類型”和“數(shù)據(jù)值”構(gòu)成,下面小編分享更多相關(guān)內(nèi)容需要的小伙伴可以參考一下2022-06-06關(guān)于SpringMVC對(duì)Restful風(fēng)格的支持詳解
Restful就是一個(gè)資源定位及資源操作的風(fēng)格,不是標(biāo)準(zhǔn)也不是協(xié)議,只是一種風(fēng)格,是對(duì)http協(xié)議的詮釋,下面這篇文章主要給大家介紹了關(guān)于SpringMVC對(duì)Restful風(fēng)格支持的相關(guān)資料,需要的朋友可以參考下2022-01-01往DAO類中注入@PersistenceContext和@Resource的區(qū)別詳解
這篇文章主要介紹了往DAO類中注入@PersistenceContext和@Resource的區(qū)別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02