使用@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篇-請求失敗自動重試機制 - 字母哥的文章 - 知乎