fix: restTemplate增加重试机制

This commit is contained in:
chenjianxing 2021-12-28 14:29:40 +08:00 committed by jianxing
parent bac612a9e6
commit db4a5dbda4
1 changed files with 39 additions and 1 deletions

View File

@ -1,11 +1,20 @@
package io.metersphere.config; package io.metersphere.config;
import io.metersphere.commons.utils.LogUtil;
import org.apache.http.NoHttpResponseException;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HttpContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.io.IOException;
import java.net.ConnectException;
@Configuration @Configuration
public class WebConfig implements WebMvcConfigurer { public class WebConfig implements WebMvcConfigurer {
@ -21,11 +30,40 @@ public class WebConfig implements WebMvcConfigurer {
private RestTemplate getTimeOutTemplate(int requestTimeout, int connectTimeout, int readTimeout) { private RestTemplate getTimeOutTemplate(int requestTimeout, int connectTimeout, int readTimeout) {
RestTemplate restTemplate = new RestTemplate(); RestTemplate restTemplate = new RestTemplate();
HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory(); HttpComponentsClientHttpRequestFactory httpRequestFactory =
new HttpComponentsClientHttpRequestFactory(getHttpClientBuilder().build());
httpRequestFactory.setConnectionRequestTimeout(requestTimeout); httpRequestFactory.setConnectionRequestTimeout(requestTimeout);
httpRequestFactory.setConnectTimeout(connectTimeout); httpRequestFactory.setConnectTimeout(connectTimeout);
httpRequestFactory.setReadTimeout(readTimeout); httpRequestFactory.setReadTimeout(readTimeout);
restTemplate.setRequestFactory(httpRequestFactory); restTemplate.setRequestFactory(httpRequestFactory);
return restTemplate; return restTemplate;
} }
private HttpClientBuilder getHttpClientBuilder() {
final int retryTimes = 3;
final long retryIntervalTime = 1000L;
HttpClientBuilder httpClientBuilder = HttpClients.custom();
// 只有io异常才会触发重试
httpClientBuilder.setRetryHandler((IOException exception, int curRetryCount, HttpContext context) -> {
// curRetryCount 每一次都会递增从1开始
if (curRetryCount > retryTimes) return false;
try {
//重试延迟
Thread.sleep(curRetryCount * retryIntervalTime);
} catch (InterruptedException e) {
e.printStackTrace();
LogUtil.error(e);
}
if (exception instanceof ConnectTimeoutException ||
exception instanceof NoHttpResponseException ||
exception instanceof ConnectException) {
LogUtil.error("重试次数: " + curRetryCount);
return true;
}
return false;
});
return httpClientBuilder;
}
} }