com.soento.core.config.HttpClientConfig Maven / Gradle / Ivy
package com.soento.core.config;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author soento
*/
@Configuration
public class HttpClientConfig {
/**
* 最大连接数
*/
@Value("${http.pool.maxTotal:100}")
private Integer maxTotal;
/**
* 并发数
*/
@Value("${http.pool.defaultMaxPerRoute:20}")
private Integer defaultMaxPerRoute;
/**
* 创建连接最长时间
*/
@Value("${http.pool.connectTimeout:1000}")
private Integer connectTimeout;
/**
* 从连接池获取连接最长时间
*/
@Value("${http.pool.connectionRequestTimeout:500}")
private Integer connectionRequestTimeout;
/**
* 数据传输最长时间
*/
@Value("${http.pool.socketTimeout:10000}")
private Integer socketTimeout;
/**
* 请求前的测试请求是否可用
*/
@Value("${http.pool.staleConnectionCheckEnabled:true}")
private boolean staleConnectionCheckEnabled;
/**
* 首先实例化一个连接池管理器,设置最大连接数、并发连接数
*
* @return
*/
@Bean(destroyMethod = "close")
public PoolingHttpClientConnectionManager httpClientConnectionManager() {
PoolingHttpClientConnectionManager httpClientConnectionManager = new PoolingHttpClientConnectionManager();
//最大连接数
httpClientConnectionManager.setMaxTotal(maxTotal);
//并发数
httpClientConnectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute);
return httpClientConnectionManager;
}
/**
* Builder是RequestConfig的一个内部类
* 通过RequestConfig的custom方法来获取到一个Builder对象
* 设置builder的连接信息
* 这里还可以设置proxy,cookieSpec等属性。有需要的话可以在此设置
*/
@Bean
public RequestConfig.Builder requestConfigBuilder() {
RequestConfig.Builder builder = RequestConfig.custom();
return builder.setConnectTimeout(connectTimeout)
.setConnectionRequestTimeout(connectionRequestTimeout)
.setSocketTimeout(socketTimeout);
}
/**
* 使用builder构建一个RequestConfig对象
*/
@Bean
public RequestConfig requestConfig(RequestConfig.Builder requestConfigBuilder) {
return requestConfigBuilder.build();
}
/**
* 实例化连接池,设置连接池管理器。
* 这里需要以参数形式注入上面实例化的连接池管理器
*
* @param httpClientConnectionManager
* @return
*/
@Bean
public HttpClientBuilder httpClientBuilder(PoolingHttpClientConnectionManager httpClientConnectionManager, RequestConfig requestConfig) {
//HttpClientBuilder中的构造方法被protected修饰,所以这里不能直接使用new来实例化一个HttpClientBuilder,可以使用HttpClientBuilder提供的静态方法create()来获取HttpClientBuilder对象
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
httpClientBuilder.setConnectionManager(httpClientConnectionManager);
httpClientBuilder.setDefaultRequestConfig(requestConfig);
httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler());
return httpClientBuilder;
}
/**
* 注入连接池,用于获取httpClient
*
* @param httpClientBuilder
* @return
*/
@Bean
public CloseableHttpClient httpClient(HttpClientBuilder httpClientBuilder) {
return httpClientBuilder.build();
}
}