Java springboot HTTP請求失敗重試


使用@Retryable注解

在啟動類上加入@EnableRetry注解開啟重試機制。

@Retryable是基於方法級別的,在需要重試的方法上,加上@Retryable

private int sum = 0;

@Retryable(value = Exception.class, maxAttempts = 3,
            backoff = @Backoff(delay = 2000L,multiplier = 2, maxDelay=5000))
  public HttpStatus test() {
    System.out.println("發起API請求:......" + (sum++) );

    String url = "http://localhost:7070/xxx";
    restTemplate.postForObject("http://localhost:7070/xxx", jsonObject, Map.class);
  }

SpringBoot @Retryable注解
精講RestTemplate第8篇-請求失敗自動重試機制 - 字母哥的文章 - 知乎


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM