配置使用连接池的httpClient

本文介绍如何在 HttpClient4.3 中配置连接池,替代已弃用的 DefaultHttpClient 和 PoolingClientConnectionManager。提供了详细的 Spring 配置示例,并展示了如何使用 RestTemplate 进行整合。

httpClient4.3开始弃用了DefaultHttpClient和PoolingClientConnectionManager.原来这样配置的连接工厂

 

<bean id="clientHttpRequestFactory"
		class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
		<constructor-arg>
			<bean class="org.apache.http.impl.client.DefaultHttpClient">
				<constructor-arg>
					<bean class="org.apache.http.impl.conn.PoolingClientConnectionManager">
						<property name="maxTotal" value="10" />
						<property name="defaultMaxPerRoute" value="5" />
					</bean>
				</constructor-arg>
			</bean>
		</constructor-arg>
		<property name="connectTimeout" value="30000" />
		<property name="readTimeout" value="30000" />
	</bean>

现在改用spring推荐的类,配置如下:

 

<bean id="pollingConnectionManager"
		class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager">
		<property name="maxTotal" value="10" />
		<property name="defaultMaxPerRoute" value="5" />
	</bean>

	<bean id="httpClientBuilder" class="org.apache.http.impl.client.HttpClientBuilder"
		factory-method="create">
		<property name="connectionManager" ref="pollingConnectionManager" />
	</bean>

	<bean id="httpClient" factory-bean="httpClientBuilder"
		factory-method="build" />

	<!-- -->
	<bean id="clientHttpRequestFactory"
		class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
		<constructor-arg ref="httpClient" />
		<property name="connectTimeout" value="30000" />
		<property name="readTimeout" value="30000" />
	</bean>

 

  配置带有连接池的org.springframework.web.client.RestTemplate

 

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
		<constructor-arg ref="clientHttpRequestFactory" />
		<property name="messageConverters">
			<list>
				<bean
					class="org.springframework.http.converter.StringHttpMessageConverter" />
				<bean
					class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
				<bean class="org.springframework.http.converter.FormHttpMessageConverter" />
				<bean
					class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
				<bean
					class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter" />
			</list>
		</property>
	</bean>

 

 

 

 

 

 

配置 HttpClient 连接池,你可以使用 Apache HttpComponents 提供的 HttpClient。下面是一个示例代码: ```java import org.apache.http.HttpHost; import org.apache.http.client.HttpClient; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.HttpGet; import org.apache.http.config.Registry; import org.apache.http.config.RegistryBuilder; import org.apache.http.conn.socket.ConnectionSocketFactory; import org.apache.http.conn.socket.PlainConnectionSocketFactory; import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.ssl.SSLContextBuilder; import javax.net.ssl.SSLContext; public class HttpClientPoolExample { public static void main(String[] args) { // 配置连接池管理器 PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); connectionManager.setMaxTotal(100); // 最大连接数 connectionManager.setDefaultMaxPerRoute(20); // 每个路由的最大连接数 // 配置 SSL SSLContext sslContext = SSLContextBuilder.create().loadTrustMaterial((chain, authType) -> true).build(); SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE); // 注册连接工厂 Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.getSocketFactory()) .register("https", sslSocketFactory) .build(); // 创建 HttpClient CloseableHttpClient httpClient = HttpClientBuilder.create() .setConnectionManager(connectionManager) .setSSLSocketFactory(sslSocketFactory) .setDefaultRequestConfig(RequestConfig.custom() .setConnectTimeout(5000) // 连接超时时间 .setSocketTimeout(5000) // 读取超时时间 .build()) .build(); // 使用 HttpClient 发送请求 HttpGet httpGet = new HttpGet("http://www.example.com"); try { httpClient.execute(httpGet); // 处理响应 } catch (Exception e) { e.printStackTrace(); } finally { httpGet.releaseConnection(); } } } ``` 在上面的示例中,我们使用连接池管理器 `PoolingHttpClientConnectionManager` 来管理连接。我们还为连接池设置了最大连接数和每个路由的最大连接数。同时,我们还配置了 SSL 支持,以便可以发送 HTTPS 请求。 请注意,这只是一个示例,你可以根据你的具体需求进行调整。另外,记得在使用完 `HttpClient` 后调用 `releaseConnection()` 方法来释放连接。 希望能对你有所帮助!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值