1.添加jar包
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
2.applicationContext-httpclient.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--定义连接管理器 -->
<bean id="connectionManager"
class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager"
destroy-method="close">
<!-- 最大连接数 -->
<property name="maxTotal" value="500" />
<!--设置每个主机最大的并发数 -->
<property name="defaultMaxPerRoute" value="100" />
</bean>
<!--定义HttpClient构建器 -->
<bean id="httpClientBuilder" class="org.apache.http.impl.client.HttpClientBuilder"
factory-method="create">
<property name="connectionManager" ref="connectionManager" />
</bean>
<!--定义httpClient对象,该bean一定是多例的 -->
<bean id="httpClient" class="org.apache.http.impl.client.CloseableHttpClient"
factory-bean="httpClientBuilder" factory-method="build" scope="prototype"></bean>
<!--定义requestConfig构建器 -->
<bean id="requestConfigBuilder" class="org.apache.http.client.config.RequestConfig.Builder">
<!--设置创建连接的最长时间 -->
<property name="connectTimeout" value="2000" />
<!--从连接池中获取到连接的最长时间 -->
<property n