javascript
使用Spring-Retry重试处理
只要軟件組件相互通信,就有可能出現(xiàn)臨時(shí)的自我糾正錯(cuò)誤。 此類(lèi)故障包括服務(wù)的暫時(shí)不可用,網(wǎng)絡(luò)連接的暫時(shí)丟失或服務(wù)繁忙時(shí)出現(xiàn)的超時(shí)。 在這種情況下,適當(dāng)?shù)闹卦囂幚砜梢詼p少這些故障可能引起的問(wèn)題。
在這篇文章中,我們將看到如何使用Spring Retry向Spring應(yīng)用程序添加健壯的重試邏輯。 Spring Retry可能不是很了解,因?yàn)樗鼪](méi)有在Spring文檔概述中列出。 但是,您可以在Spring Initializr頁(yè)面上找到它。
設(shè)定
要使用Spring Retry,我們需要在項(xiàng)目中添加以下依賴(lài)項(xiàng):
<dependency><groupid>org.springframework.retry</groupid><artifactid>spring-retry</artifactid><version>1.1.2.RELEASE</version> </dependency>Spring Retry使用AOP,因此請(qǐng)確保Spring AOP可用:
<dependency><groupid>org.springframework</groupid><artifactid>spring-aop</artifactid><version>4.2.5.RELEASE</version> </dependency> <dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.8</version> </dependency>如果您使用的是Spring Boot ,那么可以改用spring-boot-starter-aop:
<dependency><groupid>org.springframework.boot</groupid><artifactid>spring-boot-starter-aop</artifactid> </dependency>要啟用Spring Retry,我們只需要將@EnableRetry添加到我們的應(yīng)用程序配置類(lèi)中:
@EnableRetry @SpringBootApplication // or @Configuration if you are not using Spring Boot public class RetryExampleApplication {// ... }添加帶注釋的重試處理
現(xiàn)在,我們準(zhǔn)備向方法添加重試處理。 為此,我們只需要使用@Retryable注釋適當(dāng)?shù)姆椒?#xff1a;
@Service public class MyService {@Retryablepublic void simpleRetry() {// perform operation that is likely to fail} }帶有@Retryable注釋的方法可以像其他任何方法一樣調(diào)用。 但是,每當(dāng)可重試方法的執(zhí)行因異常而失敗時(shí),Spring都會(huì)自動(dòng)重試多達(dá)三遍。 默認(rèn)情況下,Spring在方法調(diào)用之間使用1秒的延遲。 請(qǐng)注意,調(diào)用線(xiàn)程在重試處理期間會(huì)阻塞。
重試行為可以通過(guò)多種方式自定義。 例如:
@Service public class MyService {@Retryable(value = {FooException.class, BarException.class}, maxAttempts = 5)public void retryWithException() {// perform operation that is likely to fail}@Recoverpublic void recover(FooException exception) {// recover from FooException} }在這里,我們告訴Spring僅在拋出FooException或BarException類(lèi)型的Exception時(shí)應(yīng)用重試處理。 其他異常不會(huì)導(dǎo)致重試。 maxAttempts = 5告訴Spring如果失敗,最多重試該方法5次。
使用@Recover我們?yōu)镕ooException定義了單獨(dú)的恢復(fù)方法。 當(dāng)可重試的方法因FooException而失敗時(shí),這使我們可以運(yùn)行特殊的恢復(fù)代碼。
使用RetryTemplate添加重試處理
除了注釋之外,Spring Retry還提供了RetryTemplate,可用于在Java代碼中定義重試處理。 像任何其他bean一樣,可以在我們的配置類(lèi)中簡(jiǎn)單地配置RetryTemplate:
@EnableRetry @SpringBootApplication // or @Configuration if you are not using Spring Boot public class RetryExampleApplication {@Beanpublic RetryTemplate retryTemplate() {SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();retryPolicy.setMaxAttempts(5);FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();backOffPolicy.setBackOffPeriod(1500); // 1.5 secondsRetryTemplate template = new RetryTemplate();template.setRetryPolicy(retryPolicy);template.setBackOffPolicy(backOffPolicy);return template;}// ... }RetryPolicy確定何時(shí)應(yīng)重試操作。 SimpleRetryPolicy是一個(gè)RetryPolicy實(shí)現(xiàn),可重試固定次數(shù)。
BackOffPolicy是一個(gè)策略界面,用于控制重試嘗試之間的退避。 在繼續(xù)之前,FixedBackOffPolicy會(huì)暫停一段固定的時(shí)間。 其他一些默認(rèn)的BackOffPolicy實(shí)現(xiàn)是ExponentialBackOffPolicy(增加每次重試的退避時(shí)間)或NoBackOffPolicy(重試之間沒(méi)有延遲)。
現(xiàn)在,我們可以將RetryTemplate注入我們的服務(wù)。 要使用重試處理來(lái)運(yùn)行代碼,我們只需調(diào)用RetryTemplate.execute():
@Service public class RetryService {@Autowiredprivate RetryTemplate retryTemplate;public void withTemplate() {retryTemplate.execute(context -> {// perform operation that is likely to fail});}// ... }RetryTemplate.exeucte()以RetryCallback <T,E>作為參數(shù)。 RetryCallback是一個(gè)功能接口,因此可以使用Java 8 Lambda表達(dá)式來(lái)實(shí)現(xiàn)(如上所示)。
摘要
Spring重試提供了一種向Spring應(yīng)用程序添加重試處理的簡(jiǎn)便方法。 可以使用批注(@Retryable和@Recover)或通過(guò)將RetryCallback傳遞給RetryTemplate來(lái)添加重試處理。
- 您可以在GitHub上找到完整的示例源代碼。
翻譯自: https://www.javacodegeeks.com/2016/03/retry-handling-spring-retry.html
總結(jié)
以上是生活随笔為你收集整理的使用Spring-Retry重试处理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 流的多层次分组
- 下一篇: ddos攻击会严重消耗的资源吗(ddos