如果@Retryable使用RestTemplate.下面給出了函數,問題是我已經將 maxAttempts 設置為 4 以防萬一發生任何異常它應該嘗試 4 次。但即使沒有任何異常,該函數也執行了 4 次,并且在數據庫中創建了 4 個員工條目。createEmployee 函數調用另一個服務在數據庫中創建員工@Retryable(value = { Exception.class }, maxAttempts = 4, backoff = @Backoff(delay = 1000))public Response createEmployee(EmployeeRequest employeeRequest) { log.info(“creating employee”) : // calls another micro service using RestTemplate which creates employee into the DB :}@EnableRetry在 AppConfig 中@Configuration@EnableRetrypublic class AppConfig {}誰能幫我解決這個問題
3 回答
慕容3067478
TA貢獻1773條經驗 獲得超3個贊
檢查您的服務在根據該設置回退時間失敗之前所花費的最長時間。檢查在失敗的情況下 resttemplate 可以拋出什么異常,僅將它們標記為您的重試。
翻閱古今
TA貢獻1780條經驗 獲得超5個贊
您應該檢查您的“調用另一個微服務”實施。
它可能是由該邏輯內部調用的另一個服務拋出異常。我建議創建一個自定義異常并將其用于您的重試定義。然后你可以檢查另一種意外異常是否是強制重試 4 次的異常。
@Retryable(value = { YourCustomException.class }, maxAttempts = 4, backoff = @Backoff(delay = 1000))
public Response createEmployee(EmployeeRequest employeeRequest)
{
log.info(“creating employee”)
:
// calls another micro service using RestTemplate which creates employee into the DB
:
}
添加回答
舉報
0/150
提交
取消
