import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import javax.net.ssl.*;
import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
@Slf4j
@Configuration
public class HttpConfig {
@Bean
public RestTemplate restTemplate(HttpClient httpClient) {
ClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
RestTemplate restTemplate = new RestTemplate(factory);
restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
return restTemplate;
}
@Bean
public HttpClient httpClient() {
RequestConfig config = RequestConfig.custom()
.setConnectionRequestTimeout(5000)
.setConnectTimeout(5000)
.setSocketTimeout(120000)
.build();
HttpClient client = HttpClientBuilder.create()
.setDefaultRequestConfig(config)
.setMaxConnTotal(400)
.setMaxConnPerRoute(100)
.setConnectionManager(httpClientConnectionManager(trustManagers()))
.build();
return client;
}
private HttpClientConnectionManager httpClientConnectionManager(TrustManager[] trustManagers) {
SSLContext sc = null;
try {
sc = SSLContext.getInstance("SSL");
sc.init(null, trustManagers, new SecureRandom());
} catch (NoSuchAlgorithmException | KeyManagementException e) {
log.error(e.getMessage(), e);
}
HostnameVerifier verifier = (String hostname, SSLSession session) -> {
return true;
};
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sc, verifier);
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", sslSocketFactory)
.build();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
connectionManager.setMaxTotal(3000);
connectionManager.setDefaultMaxPerRoute(1000);
return connectionManager;
}
public TrustManager[] trustManagers() {
TrustManager trustManager = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1) {
}
@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1) {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
};
return new TrustManager[]{trustManager};
}
}
@Autowired
RestTemplate restTemplate;
s = restTemplate.postForObject(replace, httpEntity, String.class);
|