SpringBoot整合Retry的詳細(xì)指南
問(wèn)題背景
在現(xiàn)代的分布式系統(tǒng)中,服務(wù)間的調(diào)用往往需要處理各種網(wǎng)絡(luò)異常、超時(shí)等問(wèn)題。重試機(jī)制是一種常見(jiàn)的解決策略,它允許應(yīng)用程序在網(wǎng)絡(luò)故障或臨時(shí)性錯(cuò)誤后自動(dòng)重新嘗試失敗的操作。Spring Boot 提供了靈活的方式來(lái)集成重試機(jī)制,這可以通過(guò)使用 Spring Retry 模塊來(lái)實(shí)現(xiàn)。本文將通過(guò)一個(gè)具體的使用場(chǎng)景來(lái)詳細(xì)介紹如何在 Spring Boot 應(yīng)用中集成和使用 Spring Retry 技術(shù)。
場(chǎng)景描述
假如我們正在開(kāi)發(fā)一個(gè)OMS
系統(tǒng)(訂單管理系統(tǒng)),其中一個(gè)關(guān)鍵服務(wù) OrderService
負(fù)責(zé)訂單創(chuàng)建和調(diào)用WMS
服務(wù)扣減庫(kù)存API 。由于網(wǎng)絡(luò)不穩(wěn)定或外部 API 可能暫時(shí)不可用,我們需要在這些情況下實(shí)現(xiàn)重試機(jī)制,以確保請(qǐng)求的成功率和系統(tǒng)的穩(wěn)定性。
實(shí)現(xiàn)步驟
1. 添加依賴
首先,在你的 pom.xml
文件中添加 Spring Retry
和 AOP
的依賴:
<dependencies> <!-- Spring Boot Starter Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Retry --> <dependency> <groupId>org.springframework.retry</groupId> <artifactId>spring-retry</artifactId> </dependency> <!-- Spring Boot Starter AOP --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> </dependencies>
2. 創(chuàng)建配置類
創(chuàng)建一個(gè)配置類來(lái)配置重試模板:
package com.zlp.retry.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.retry.backoff.ExponentialBackOffPolicy; import org.springframework.retry.policy.SimpleRetryPolicy; import org.springframework.retry.support.RetryTemplate; /** * 全局重試配置 */ @Configuration public class CustomRetryConfig { /** * * 這段代碼定義了一個(gè) `CustomRetryConfig` 類,其中包含一個(gè) `retryTemplate` 方法。該方法用于創(chuàng)建并配置一個(gè) `RetryTemplate` 對(duì)象,該對(duì)象用于處理重試邏輯。 * * 1. 創(chuàng)建 `RetryTemplate` 實(shí)例**:創(chuàng)建一個(gè) `RetryTemplate` 對(duì)象。 * 2. 設(shè)置重試策略:使用 `SimpleRetryPolicy` 設(shè)置最大重試次數(shù)為5次。 * 3. 設(shè)置延遲策略:使用 `ExponentialBackOffPolicy` 設(shè)置初始延遲時(shí)間為1000毫秒,每次重試間隔時(shí)間乘以2。 * 4. 應(yīng)用策略:將重試策略和延遲策略應(yīng)用到 `RetryTemplate` 對(duì)象。 */ @Bean public RetryTemplate retryTemplate() { RetryTemplate template = new RetryTemplate(); // 設(shè)置重試策略 SimpleRetryPolicy policy = new SimpleRetryPolicy(); policy.setMaxAttempts(5); // 設(shè)置延遲策略 ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy(); backOffPolicy.setInitialInterval(1000); backOffPolicy.setMultiplier(2.0); template.setRetryPolicy(policy); template.setBackOffPolicy(backOffPolicy); return template; } }
3. 創(chuàng)建服務(wù)類
創(chuàng)建一個(gè)服務(wù)類 OrderService
和接口實(shí)現(xiàn)類OrderServiceImpl
,并在需要重試的方法上使用 @Retryable
注解:
/** * @Classname OrderService * @Date 2024/11/18 21:03 * @Created by ZouLiPing */ public interface OrderService { /** * 創(chuàng)建訂單 * @param createOrderReq * @return */ String createOrder(CreateOrderReq createOrderReq); }
package com.zlp.retry.service.impl; import cn.hutool.core.date.DateUtil; import com.alibaba.fastjson.JSON; import com.zlp.retry.dto.CreateOrderReq; import com.zlp.retry.service.OrderService; import lombok.extern.slf4j.Slf4j; import org.springframework.retry.annotation.Backoff; import org.springframework.retry.annotation.Recover; import org.springframework.retry.annotation.Retryable; import org.springframework.stereotype.Service; import java.util.UUID; /** * @Classname OrderServiceImpl * @Date 2024/11/18 21:06 * @Created by ZouLiPing */ @Service @Slf4j(topic = "OrderServiceImpl") public class OrderServiceImpl implements OrderService { @Override @Retryable(value = {Exception.class},maxAttempts = 4, backoff = @Backoff(delay = 3000)) public String createOrder(CreateOrderReq createOrderReq) { log.info("createOrder.req createOrderReq:{}", JSON.toJSONString(createOrderReq)); try { log.info("createOrder.deductStock.調(diào)用時(shí)間={}", DateUtil.formatDateTime(DateUtil.date())); // 扣減庫(kù)存服務(wù) this.deductStock(createOrderReq); } catch (Exception e) { throw new RuntimeException(e); } return UUID.randomUUID().toString(); } /** * 模擬扣減庫(kù)存 */ private void deductStock(CreateOrderReq createOrderReq) { throw new RuntimeException("庫(kù)存扣減失敗"); } /** * 當(dāng)重試四次仍未能成功創(chuàng)建訂單時(shí)調(diào)用此方法進(jìn)行最終處理 * * @param ex 異常對(duì)象,包含重試失敗的原因 * @param createOrderReq 創(chuàng)建訂單的請(qǐng)求對(duì)象,包含訂單相關(guān)信息 * @return 返回處理結(jié)果,此處返回"fail"表示最終失敗 */ @Recover public String recover(Exception ex, CreateOrderReq createOrderReq) { // 記錄重試四次后仍失敗的日志,包括異常信息和訂單請(qǐng)求內(nèi)容 log.error("recover.resp.重試四次還是失敗.error:{},createOrderReq:{}",ex.getMessage(),JSON.toJSONString(createOrderReq)); // 處理最終失敗的情況 // 可以記錄日志,或者是投遞MQ,采用最終一致性的方式處理 return "fail"; } }
4. 啟用重試功能
在主配置類或啟動(dòng)類上添加 @EnableRetry
注解,以啟用重試功能:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.retry.annotation.EnableRetry; @SpringBootApplication @EnableRetry public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
5. 驗(yàn)證重試方法
@RestController @RequiredArgsConstructor public class RetryController { private final OrderService orderService; @GetMapping("getRetry") public String retry(){ CreateOrderReq createOrderReq = new CreateOrderReq(); createOrderReq.setOrderId(UUID.randomUUID().toString()); createOrderReq.setProductId("SKU001"); createOrderReq.setCount(10); createOrderReq.setMoney(100); return orderService.createOrder(createOrderReq); } }
6.執(zhí)行操作說(shuō)明
- 添加依賴:引入了 Spring Retry 和 AOP 的依賴,以便使用重試功能。
- 配置重試模板:創(chuàng)建了一個(gè)配置類
CustomRetryConfig
,配置了重試策略,設(shè)置最大重試次數(shù)為5次。 - 創(chuàng)建服務(wù)類:在
OrderServiceImpl
類中,使用@Retryable
注解標(biāo)記了createOrder
方法,指定了當(dāng)發(fā)生Exception
時(shí)進(jìn)行重試,最大重試次數(shù)為4
次,每次重試間隔3秒。同時(shí),使用@Recover
注解標(biāo)記了recover
方法,當(dāng)所有重試都失敗后,會(huì)調(diào)用這個(gè)方法。 - 啟用重試功能:在主配置類或啟動(dòng)類上添加
@EnableRetry
注解,以啟用重試功能。
Retry執(zhí)行流程
Retry整體流程圖
打印日志
從日志分析每隔3秒鐘會(huì)重試一次,直到到達(dá)設(shè)置最大重試次數(shù),會(huì)調(diào)用功<font style="color:#DF2A3F;">recover</font>
方法中
7.結(jié)論
通過(guò)以上步驟,我們成功地在 Spring Boot應(yīng)用中集成了 Spring Retry 技術(shù),實(shí)現(xiàn)了服務(wù)調(diào)用的重試機(jī)制。這不僅提高了系統(tǒng)的健壯性和穩(wěn)定性,還減少了因網(wǎng)絡(luò)問(wèn)題或外部服務(wù)暫時(shí)不可用導(dǎo)致的請(qǐng)求失敗。希望本文對(duì)你理解和應(yīng)用 Spring Boot 中的重試技術(shù)有所幫助。
Retry配置的優(yōu)先級(jí)規(guī)則
- 方法級(jí)別配置:如果某個(gè)配置在方法上定義了,則該方法上的配置會(huì)覆蓋類級(jí)別的配置和全局配置。
- 類級(jí)別配置:如果某個(gè)配置在類上定義了,并且該類的方法沒(méi)有單獨(dú)定義配置,則使用類級(jí)別的配置。
- 全局配置:如果沒(méi)有在方法或類上定義配置,則使用全局配置。
下面通過(guò)一個(gè)具體的例子來(lái)展示這些優(yōu)先級(jí)規(guī)則。假設(shè)我們有一個(gè)服務(wù)類 <font style="color:rgb(44, 44, 54);">MyService</font>
,其中包含一些方法,并且我們?cè)诓煌膶哟紊线M(jìn)行了重試策略的配置。
示例代碼
import org.springframework.retry.annotation.Backoff; import org.springframework.retry.annotation.Retryable; import org.springframework.stereotype.Service; @Service @Retryable( value = {RuntimeException.class}, maxAttempts = 3, backoff = @Backoff(delay = 1000) // 類級(jí)別的配置 ) public class MyService { @Retryable( value = {RuntimeException.class}, maxAttempts = 5, backoff = @Backoff(delay = 500) // 方法級(jí)別的配置 ) public void retryableMethodWithSpecificConfig() { System.out.println("Retrying with specific config..."); throw new RuntimeException("Simulated exception"); } @Retryable( value = {RuntimeException.class} ) public void retryableMethodWithoutSpecificDelay() { System.out.println("Retrying without specific delay..."); throw new RuntimeException("Simulated exception"); } public void nonRetryableMethod() { System.out.println("This method does not retry."); throw new RuntimeException("Simulated exception"); } }
解釋
- retryableMethodWithSpecificConfig
- 方法級(jí)別配置:
<font style="color:rgb(44, 44, 54);">maxAttempts = 5</font>
<font style="color:rgb(44, 44, 54);">backoff.delay = 500</font>
- 這些配置會(huì)覆蓋類級(jí)別的配置。
- 方法級(jí)別配置:
- retryableMethodWithoutSpecificDelay
- 方法級(jí)別配置:
<font style="color:rgb(44, 44, 54);">maxAttempts = 3</font>
(繼承自類級(jí)別)<font style="color:rgb(44, 44, 54);">backoff.delay = 1000</font>
(繼承自類級(jí)別)
- 這些配置繼承自類級(jí)別的配置。
- 方法級(jí)別配置:
- nonRetryableMethod
- 該方法沒(méi)有使用
<font style="color:rgb(44, 44, 54);">@Retryable</font>
注解,因此不會(huì)進(jìn)行重試。
- 該方法沒(méi)有使用
全局配置示例
為了進(jìn)一步說(shuō)明全局配置的優(yōu)先級(jí),我們可以配置一個(gè)全局的重試模板。
配置類
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.retry.backoff.FixedBackOffPolicy; import org.springframework.retry.policy.SimpleRetryPolicy; import org.springframework.retry.support.RetryTemplate; @Configuration public class RetryConfig { @Bean public RetryTemplate retryTemplate() { RetryTemplate template = new RetryTemplate(); SimpleRetryPolicy policy = new SimpleRetryPolicy(); policy.setMaxAttempts(4); template.setRetryPolicy(policy); FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy(); fixedBackOffPolicy.setBackOffPeriod(750L); template.setBackOffPolicy(fixedBackOffPolicy); return template; } }
使用全局配置的服務(wù)類
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.retry.annotation.Retryable; import org.springframework.stereotype.Service; @Service public class AnotherService { @Autowired private RetryTemplate retryTemplate; @Retryable( value = {RuntimeException.class}, maxAttempts = 6, // 方法級(jí)別的配置 backoff = @Backoff(delay = 300) // 方法級(jí)別的配置 ) public void retryableMethodWithGlobalAndLocalConfig() { System.out.println("Retrying with global and local config..."); throw new RuntimeException("Simulated exception"); } @Retryable( value = {RuntimeException.class} ) public void retryableMethodWithOnlyGlobalConfig() { System.out.println("Retrying with only global config..."); throw new RuntimeException("Simulated exception"); } }
解釋
- retryableMethodWithGlobalAndLocalConfig
- 方法級(jí)別配置:
<font style="color:rgb(44, 44, 54);">maxAttempts = 6</font>
<font style="color:rgb(44, 44, 54);">backoff.delay = 300</font>
- 這些配置會(huì)覆蓋全局配置。
- 方法級(jí)別配置:
- retryableMethodWithOnlyGlobalConfig
- 全局配置:
<font style="color:rgb(44, 44, 54);">maxAttempts = 4</font>
<font style="color:rgb(44, 44, 54);">backoff.delay = 750</font>
- 這些配置繼承自全局配置。
- 全局配置:
總結(jié)一下,配置的優(yōu)先級(jí)從高到低依次是:
- 方法級(jí)別配置
- 類級(jí)別配置
- 全局配置
希望這個(gè)示例能幫助你理解不同層次配置的優(yōu)先級(jí)。
什么樣的場(chǎng)景不適合Retry
在使用重試機(jī)制時(shí),確實(shí)有一些場(chǎng)景不適合應(yīng)用重試策略。了解這些場(chǎng)景有助于避免不必要的重試操作,從而提高系統(tǒng)的性能和穩(wěn)定性。以下是幾種不適合使用重試機(jī)制的常見(jiàn)場(chǎng)景:
- 冪等性不可保證的操作
- 解釋:如果一個(gè)操作不是冪等的(即多次執(zhí)行會(huì)產(chǎn)生不同的結(jié)果),那么重試可能導(dǎo)致數(shù)據(jù)不一致或其他問(wèn)題。
- 示例:插入數(shù)據(jù)庫(kù)記錄的操作通常不是冪等的,因?yàn)橹貜?fù)插入會(huì)導(dǎo)致重復(fù)的數(shù)據(jù)。
- 長(zhǎng)時(shí)間運(yùn)行的操作
- 解釋:對(duì)于耗時(shí)較長(zhǎng)的操作,頻繁重試可能會(huì)導(dǎo)致系統(tǒng)資源被大量占用,影響其他任務(wù)的執(zhí)行。
- 示例:批量處理大數(shù)據(jù)集、長(zhǎng)時(shí)間計(jì)算的任務(wù)。
- 外部依賴不穩(wěn)定但無(wú)法恢復(fù)
- 解釋:某些外部服務(wù)或API可能存在根本性的故障,無(wú)法通過(guò)簡(jiǎn)單的重試解決。在這種情況下,重試只會(huì)浪費(fèi)資源。
- 示例:調(diào)用第三方支付接口,如果返回的是明確的失敗狀態(tài)碼(如賬戶余額不足),則不應(yīng)該重試。
- 網(wǎng)絡(luò)超時(shí)且無(wú)可用備用路徑
- 解釋:在網(wǎng)絡(luò)請(qǐng)求超時(shí)時(shí),如果沒(méi)有任何備用路徑或解決方案,重試可能仍然會(huì)失敗。
- 示例:嘗試連接到某個(gè)特定IP地址的服務(wù),如果該地址一直不通,則重試沒(méi)有意義。
- 用戶交互過(guò)程中需要立即反饋的操作
- 解釋:在用戶等待響應(yīng)的過(guò)程中,長(zhǎng)時(shí)間的重試可能導(dǎo)致用戶體驗(yàn)不佳。
- 示例:提交表單后立即顯示成功消息,如果在此期間發(fā)生錯(cuò)誤并進(jìn)行重試,用戶可能會(huì)感到困惑。
- 涉及敏感信息的操作
- 解釋:對(duì)于涉及敏感信息的操作(如密碼修改、資金轉(zhuǎn)賬),重試可能會(huì)導(dǎo)致敏感信息泄露或重復(fù)操作。
- 示例:更新用戶的銀行賬戶信息,一旦確認(rèn)操作完成,不應(yīng)再進(jìn)行重試。
- 事務(wù)邊界內(nèi)的操作
- 解釋:在事務(wù)邊界內(nèi),重試可能會(huì)導(dǎo)致事務(wù)沖突或回滾,增加復(fù)雜性。
- 示例:在一個(gè)復(fù)雜的數(shù)據(jù)庫(kù)事務(wù)中,部分操作失敗后進(jìn)行重試可能導(dǎo)致整個(gè)事務(wù)失敗。
- 已知的永久性錯(cuò)誤
- 解釋:如果能夠明確判斷出錯(cuò)誤是永久性的(如配置錯(cuò)誤、代碼bug),重試不會(huì)解決問(wèn)題。
- 示例:嘗試讀取不存在的文件,這種錯(cuò)誤通常是永久性的。
- 高并發(fā)環(huán)境下的寫操作
- 解釋:在高并發(fā)環(huán)境下,頻繁的重試可能會(huì)加劇數(shù)據(jù)庫(kù)負(fù)載,導(dǎo)致更多的鎖競(jìng)爭(zhēng)和死鎖。
- 示例:在電商網(wǎng)站的下單高峰期,對(duì)庫(kù)存的減少操作不宜頻繁重試。
示例代碼
為了更好地理解這些原則,下面是一個(gè)簡(jiǎn)單的示例,展示了如何在Spring Boot中使用<font style="color:rgb(44, 44, 54);">@Retryable</font>
注解,并根據(jù)上述原則決定哪些操作適合重試。
import org.springframework.retry.annotation.Backoff; import org.springframework.retry.annotation.Retryable; import org.springframework.stereotype.Service; @Service public class MyService { // 適合重試的操作:冪等性強(qiáng)、短時(shí)間操作、可恢復(fù)的錯(cuò)誤 @Retryable( value = {RuntimeException.class}, maxAttempts = 3, backoff = @Backoff(delay = 1000) ) public void retryableFetchData() { System.out.println("Fetching data..."); // 模擬網(wǎng)絡(luò)請(qǐng)求或短暫的外部服務(wù)調(diào)用 if (Math.random() > 0.5) { throw new RuntimeException("Simulated transient network error"); } } // 不適合重試的操作:冪等性不可保證、長(zhǎng)時(shí)間運(yùn)行 public void nonRetryableLongRunningTask() { System.out.println("Starting long-running task..."); try { Thread.sleep(10000); // 模擬長(zhǎng)時(shí)間運(yùn)行的任務(wù) } catch (InterruptedException e) { Thread.currentThread().interrupt(); } System.out.println("Long-running task completed."); } // 不適合重試的操作:涉及敏感信息 public void updateSensitiveInformation(String sensitiveData) { System.out.println("Updating sensitive information..."); // 這里假設(shè)更新操作不是冪等的,也不應(yīng)該重試 throw new RuntimeException("Simulated failure in updating sensitive information"); } // 不適合重試的操作:已知的永久性錯(cuò)誤 public void fetchDataFromNonExistentResource() { System.out.println("Fetching data from a non-existent resource..."); throw new RuntimeException("Permanent error: Resource not found"); } }
解釋
- retryableFetchData
- 適用條件:
- 冪等性強(qiáng):每次請(qǐng)求的結(jié)果相同。
- 短時(shí)間操作:模擬網(wǎng)絡(luò)請(qǐng)求或短暫的外部服務(wù)調(diào)用。
- 可恢復(fù)的錯(cuò)誤:模擬暫時(shí)的網(wǎng)絡(luò)錯(cuò)誤。
- 重試策略:
- 最大重試次數(shù)為3次。
- 每次重試間隔1秒。
- 適用條件:
- nonRetryableLongRunningTask
- 不適用原因:
- 長(zhǎng)時(shí)間運(yùn)行:模擬長(zhǎng)時(shí)間運(yùn)行的任務(wù)。
- 重試可能導(dǎo)致資源過(guò)度消耗。
- 不適用原因:
- updateSensitiveInformation
- 不適用原因:
- 涉及敏感信息:更新操作不是冪等的,也不應(yīng)該重試。
- 重試可能導(dǎo)致數(shù)據(jù)不一致或其他安全問(wèn)題。
- 不適用原因:
- fetchDataFromNonExistentResource
- 不適用原因:
- 已知的永久性錯(cuò)誤:資源不存在,重試不會(huì)解決問(wèn)題。
- 不適用原因:
通過(guò)這些示例,你可以更好地理解哪些操作適合重試以及為什么某些操作不適合重試。
以上就是SpringBoot整合Retry的詳細(xì)指南的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot整合Retry的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Jenkins Pipeline 部署 SpringBoot 應(yīng)用的教程詳解
這篇文章主要介紹了Jenkins Pipeline 部署 SpringBoot 應(yīng)用的詳細(xì)教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07詳解Java的call by value和call by reference
在本篇文章里小編給大家總結(jié)了關(guān)于Java的call by value和call by reference的相關(guān)用法和知識(shí)點(diǎn)內(nèi)容,需要的朋友們學(xué)習(xí)下。2019-03-03Java數(shù)據(jù)庫(kù)連接PreparedStatement的使用詳解
這篇文章主要介紹了Java數(shù)據(jù)庫(kù)連接PreparedStatement的使用詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08Java實(shí)現(xiàn)輕松處理日期和時(shí)間的API小結(jié)
這篇文章主要為大家詳細(xì)介紹了Java中的日期和時(shí)間API,可以輕松處理日期和時(shí)間,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03springboot3.x版本集成log4j遇到Logging?system?failed?to?initial
使用Springboot?3.x集成Log4j時(shí)可能會(huì)遇到版本沖突的問(wèn)題,這通??梢酝ㄟ^(guò)檢查Maven依賴樹來(lái)識(shí)別,一旦發(fā)現(xiàn)沖突,將Log4j的版本統(tǒng)一更新到最新的兼容版本,例如2.21.1,即可解決問(wèn)題,此方法有效解決了日志打印錯(cuò)誤,是處理類似問(wèn)題的一個(gè)實(shí)用參考2024-09-09SpringBoot如何配置數(shù)據(jù)庫(kù)主從shardingsphere
這篇文章主要介紹了SpringBoot如何配置數(shù)據(jù)庫(kù)主從shardingsphere問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04解決使用this.getClass().getResource()獲取文件時(shí)遇到的坑
這篇文章主要介紹了解決使用this.getClass().getResource()獲取文件時(shí)遇到的坑問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12