com.github.xuchen93.web.config.RestTemplateConfig Maven / Gradle / Ivy
package com.github.xuchen93.web.config;
import cn.hutool.core.thread.ThreadUtil;
import io.netty.channel.ConnectTimeoutException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import javax.net.ssl.SSLContext;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
public class RestTemplateConfig {
@Value("${rest.template.retry.time:3}")
int retryTime;
@Value("${rest.template.retry.interval.time:1000}")
int retryIntervalTime;
@Bean("retryRestTemplate")
public RestTemplate retryRestTemplate() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
RestTemplate restTemplate = new RestTemplate();
// HttpClientBuilder httpClientBuilder = httpClientBuilder();
// // 只有io异常才会触发重试
// httpClientBuilder.setRetryHandler((exception, curRetryCount, context) -> {
// // curRetryCount 每一次都会递增,从1开始
// if (curRetryCount > retryTime) {
// return false;
// }
// //重试延迟
// ThreadUtil.sleep(retryIntervalTime);
// if (exception instanceof ConnectTimeoutException
// || exception instanceof NoHttpResponseException
// || exception instanceof SocketTimeoutException
// || exception instanceof ConnectException) {
// return true;
// }
// HttpClientContext clientContext = HttpClientContext.adapt(context);
// org.apache.http.HttpRequest request = clientContext.getRequest();
// // 如果请求被认为是幂等的,那么就重试。即重复执行不影响程序其他效果的
// return !(request instanceof HttpEntityEnclosingRequest);
// }).setMaxConnTotal(400);
// HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
// clientHttpRequestFactory.setHttpClient(httpClientBuilder.build());
// clientHttpRequestFactory.setConnectTimeout(1000); // 连接超时,毫秒
// clientHttpRequestFactory.setReadTimeout(2000); // 读写超时,毫秒
// restTemplate.setRequestFactory(clientHttpRequestFactory);
return restTemplate;
}
// @Bean
// public HttpClientBuilder httpClientBuilder() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
// HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// //设置HTTP连接管理器
// httpClientBuilder.setConnectionManager(poolingConnectionManager());
// TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
//
// SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy)
// .build();
//
// SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
//
//
// httpClientBuilder.setSSLSocketFactory(csf);
// return httpClientBuilder;
// }
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy