如何在SpringBoot中优雅地重试调用第三方API?
-
提高系统的稳定性:在面对临时性故障时,通过重试机制可以减轻对系统的影响,确保服务的可用性。 -
降低因故障而导致的用户体验差:用户可能无法感知到一次短暂的故障,而重试机制可以在不干扰用户操作的情况下自动修复问题。

<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;
public class ThirdPartyService {
value = { RestClientException.class },
maxAttempts = 3,
backoff = @Backoff(delay = 1000, multiplier = 2)
)
public String callThirdPartyApi() {
// 调用第三方API的逻辑
// ...
}
}
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
public class ThirdPartyService {
value = { RestClientException.class },
maxAttempts = 3,
backoff = @Backoff(delay = 1000, multiplier = 2)
)
public String callThirdPartyApi() {
// 调用第三方API的逻辑
// ...
}
public String fallback() {
// 降级处理逻辑
// ...
}
}
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;
public class AsyncThirdPartyService {
value = { RestClientException.class },
maxAttempts = 3,
backoff = @Backoff(delay = 1000, multiplier = 2)
)
public CompletableFuture<String> callAsyncThirdPartyApi() {
// 异步调用第三方API的逻辑
// ...
}
}
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
public class AsyncThirdPartyService {
value = { RestClientException.class },
maxAttempts = 3,
backoff = @Backoff(delay = 1000, multiplier = 2)
)
public CompletableFuture<String> callAsyncThirdPartyApi() {
// 异步调用第三方API的逻辑
// ...
}
public CompletableFuture<String> fallback() {
// 异步降级处理逻辑
// ...
}
}
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;
public class ThirdPartyService {
value = { RestClientException.class, TimeoutException.class },
maxAttempts = 3,
backoff = @Backoff(delay = 1000, multiplier = 2)
)
public String callThirdPartyApi() {
// 调用第三方API的逻辑
// ...
}
}
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;
public class ThirdPartyService {
value = { RestClientException.class },
maxAttempts = 3,
backoff = @Backoff(delay = 1000, multiplier = 2),
exclude = { TimeoutException.class }
)
public String callThirdPartyApi() {
// 调用第三方API的逻辑
// ...
}
}
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
public class YourApplication {
public static void main(String[] args) {
SpringApplication.run(YourApplication.class, args);
}
}
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
public class ThirdPartyService {
"fallback") (fallbackMethod =
public String callThirdPartyApi() {
// 调用第三方API的逻辑
// ...
}
public String fallback() {
// 熔断时的降级逻辑
// ...
}
}
微信赞赏
支付宝扫码领红包
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。侵权投诉:375170667@qq.com