spring-retry簡單使用方法
在分布式系統(tǒng)中,為了保證數(shù)據(jù)分布式事務(wù)的強一致性,大家在調(diào)用RPC接口或者發(fā)送MQ時,針對可能會出現(xiàn)網(wǎng)絡(luò)抖動請求超時情況采取一下重試操作。大家用的最多的重試方式就是MQ了,但是如果你的項目中沒有引入MQ,那就不方便了,本文主要介紹一下如何使用Spring Retry實現(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. 在啟動里添加重試配置
@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能否重試。當proxyTargetClass屬性為true時,使用CGLIB代理。默認使用標準JAVA注解。在spring Boot中此參數(shù)寫在程序入口即可。
@Retryable 標注此注解的方法在發(fā)生異常時會進行重試
value:指定處理的異常類
include:指定處理的異常類和value一樣,默認為空,當exclude也為空時,默認所有異常
exclude:指定異常不處理,默認空,當include也為空時,默認所有異常
maxAttempts:最大重試次數(shù)。默認3次
backoff: 重試等待策略。默認使用@Backoff注解
@Backoff 重試等待策略
不設(shè)置參數(shù)時,默認使用FixedBackOffPolicy(指定等待時間),重試等待1000ms
設(shè)置delay,使用FixedBackOffPolicy(指定等待時間),重試等待填寫的時間
設(shè)置delay和maxDealy時,重試等待在這兩個值之間均態(tài)分布
設(shè)置delay、maxDealy、multiplier,使用 ExponentialBackOffPolicy(指數(shù)級重試間隔的實現(xiàn) ),multiplier即指定延遲倍數(shù),比如delay=5000l,multiplier=2,則第一次重試為5秒,第二次為10秒,第三次為20秒……
@Recover 用于@Retryable重試失敗后處理方法,此注解注釋的方法參數(shù)一定要是@Retryable拋出的異常,否則無法識別,可以在該方法中進行日志處理。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java中l(wèi)ong數(shù)據(jù)類型轉(zhuǎn)換為int類型
這篇文章主要講解Java中基本數(shù)據(jù)類型,java long 類型與其java int類型的轉(zhuǎn)換的幾種方法,希望能給大家做一個參考2016-07-07java使用MulticastSocket實現(xiàn)組播
這篇文章主要為大家詳細介紹了java使用MulticastSocket實現(xiàn)組播,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01Go反射底層原理及數(shù)據(jù)結(jié)構(gòu)解析
這篇文章主要介紹了Go反射底層原理及數(shù)據(jù)結(jié)構(gòu)解析,反射的實現(xiàn)和interface的組成很相似,都是由“類型”和“數(shù)據(jù)值”構(gòu)成,下面小編分享更多相關(guān)內(nèi)容需要的小伙伴可以參考一下2022-06-06關(guān)于SpringMVC對Restful風格的支持詳解
Restful就是一個資源定位及資源操作的風格,不是標準也不是協(xié)議,只是一種風格,是對http協(xié)議的詮釋,下面這篇文章主要給大家介紹了關(guān)于SpringMVC對Restful風格支持的相關(guān)資料,需要的朋友可以參考下2022-01-01往DAO類中注入@PersistenceContext和@Resource的區(qū)別詳解
這篇文章主要介紹了往DAO類中注入@PersistenceContext和@Resource的區(qū)別詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02