javascript
spring-retry_使用Spring-Retry重试处理
spring-retry
每當軟件組件相互通信時,就有可能出現暫時的自我糾正錯誤。 這些故障包括服務的暫時不可用,網絡連接的瞬時丟失或服務繁忙時出現的超時。 在這種情況下,適當的重試處理可以減少這些故障可能引起的問題。
在這篇文章中,我們將看到如何使用Spring Retry向Spring應用程序添加健壯的重試邏輯。 Spring Retry可能不是很了解,因為它沒有在Spring文檔概述中列出。 但是,您可以在Spring Initializr頁面上找到它。
建立
要使用Spring Retry,我們需要在項目中添加以下依賴項:
<dependency><groupid>org.springframework.retry</groupid><artifactid>spring-retry</artifactid><version>1.1.2.RELEASE</version> </dependency>Spring Retry使用AOP,因此請確保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添加到我們的應用程序配置類中:
@EnableRetry @SpringBootApplication // or @Configuration if you are not using Spring Boot public class RetryExampleApplication {// ... }使用批注添加重試處理
現在,我們準備向方法添加重試處理。 為此,我們僅需使用@Retryable注釋適當的方法:
@Service public class MyService {@Retryablepublic void simpleRetry() {// perform operation that is likely to fail} }帶有@Retryable注釋的方法可以像其他任何方法一樣調用。 但是,每當可重試方法的執行因異常而失敗時,Spring都會自動重試多達三遍。 默認情況下,Spring在方法調用之間使用1秒的延遲。 請注意,調用線程在重試處理期間會阻塞。
重試行為可以通過多種方式自定義。 例如:
@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類型的Exception時應用重試處理。 其他異常不會導致重試。 maxAttempts = 5告訴Spring如果失敗,最多重試該方法5次。
使用@Recover,我們為FooException定義了單獨的恢復方法。 當可重試的方法因FooException而失敗時,這使我們可以運行特殊的恢復代碼。
使用RetryTemplate添加重試處理
除了注釋之外,Spring Retry還提供了RetryTemplate,可用于在Java代碼中定義重試處理。 與其他任何bean一樣,可以在我們的配置類中簡單地配置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確定何時應重試操作。 SimpleRetryPolicy是一個RetryPolicy實現,可重試固定次數。
BackOffPolicy是一個策略接口,用于控制重試嘗試之間的退避。 在繼續之前,FixedBackOffPolicy會暫停一段固定的時間。 其他一些默認的BackOffPolicy實現是ExponentialBackOffPolicy(增加每次重試的退避時間)或NoBackOffPolicy(重試之間沒有延遲)。
現在,我們可以將RetryTemplate注入我們的服務。 要使用重試處理來運行代碼,我們只需調用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>作為參數。 RetryCallback是一個功能接口,因此可以使用Java 8 Lambda表達式來實現(如上所示)。
摘要
Spring重試提供了一種向Spring應用程序添加重試處理的簡便方法。 可以使用批注(@Retryable和@Recover)或通過將RetryCallback傳遞給RetryTemplate來添加重試處理。
- 您可以在GitHub上找到完整的示例源代碼。
翻譯自: https://www.javacodegeeks.com/2016/03/retry-handling-spring-retry.html
spring-retry
總結
以上是生活随笔為你收集整理的spring-retry_使用Spring-Retry重试处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 废水中的TKN是什么意思 废水中的TKN
- 下一篇: 可变lambda_Lambda的Lamb