From b3ac9bce44fb4407e45d50ceb4fe6c948bf07d7d Mon Sep 17 00:00:00 2001 From: chenjianxing Date: Tue, 28 Dec 2021 14:29:40 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20restTemplate=E5=A2=9E=E5=8A=A0=E9=87=8D?= =?UTF-8?q?=E8=AF=95=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/metersphere/config/WebConfig.java | 40 ++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/backend/src/main/java/io/metersphere/config/WebConfig.java b/backend/src/main/java/io/metersphere/config/WebConfig.java index 6200b43fd3..44fb039e4f 100644 --- a/backend/src/main/java/io/metersphere/config/WebConfig.java +++ b/backend/src/main/java/io/metersphere/config/WebConfig.java @@ -1,11 +1,20 @@ 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.Configuration; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import java.io.IOException; +import java.net.ConnectException; + @Configuration public class WebConfig implements WebMvcConfigurer { @@ -21,11 +30,40 @@ public class WebConfig implements WebMvcConfigurer { private RestTemplate getTimeOutTemplate(int requestTimeout, int connectTimeout, int readTimeout) { RestTemplate restTemplate = new RestTemplate(); - HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory(); + HttpComponentsClientHttpRequestFactory httpRequestFactory = + new HttpComponentsClientHttpRequestFactory(getHttpClientBuilder().build()); httpRequestFactory.setConnectionRequestTimeout(requestTimeout); httpRequestFactory.setConnectTimeout(connectTimeout); httpRequestFactory.setReadTimeout(readTimeout); restTemplate.setRequestFactory(httpRequestFactory); 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; + } + }