一,建立配置属性文件
spring:
http-client:
pool:
#连接池的最大连接数,0代表不限;如果取0,需要考虑连接泄露导致系统崩溃的后果
maxTotalConnect: 1000
#每个路由的最大连接数,如果只调用一个地址,可以将其设置为最大连接数
maxConnectPerRoute: 200
# 指客户端和服务器建立连接的超时时间,ms , 最大约21秒,因为内部tcp在进行三次握手建立连接时,默认tcp超时时间是20秒
connectTimeout: 3000
# 指客户端从服务器读取数据包的间隔超时时间,不是总读取时间,也就是socket timeout,ms
readTimeout: 5000
# 从连接池获取连接的timeout,不宜过大,ms
connectionRequestTimout: 200
# 重试次数
retryTimes: 3
charset: UTF-8
# 长连接保持时间 单位s,不宜过长
keepAliveTime: 10
keepAliveTargetHost:
www.baidu.com: 5
二,创建配置属性类
package com.aishua.zdjfzxq.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* @Author: zh
* @Date:
* @Description:
*/
@Component
@ConfigurationProperties(prefix = "spring.http-client.pool")
public class HttpClientPoolConfig {
/**
* java配置的优先级低于yml配置;如果yml配置不存在,会采用java配置
*/
/**
* 连接池的最大连接数
*/
private int maxTotalConnect ;
/**
* 同路由的并发数
*/
private int maxConnectPerRoute ;
/**
* 客户端和服务器建立连接超时,默认2s
*/
private int connectTimeout = 2 * 1000;
/**
* 指客户端从服务器读取数据包的间隔超时时间,不是总读取时间,默认30s
*/
private int readTimeout = 30 * 1000;
private String charset = "UTF-8";
/**
* 重试次数,默认2次
*/
private int retryTimes = 2;
/**
* 从连接池获取连接的超时时间,不宜过长,单位ms
*/
private int connectionRequestTimout = 200;
/**
* 针对不同的地址,特别设置不同的长连接保持时间
*/
private Map<String,Integer> keepAliveTargetHost;
/**
* 针对不同的地址,特别设置不同的长连接保持时间,单位 s
*/
private int keepAliveTime = 60;
public int getMaxTotalConnect() {
return maxTotalConnect;
}
public void setMaxTotalConnect(int maxTotalConnect) {
this.maxTotalConnect = maxTotalConnect;
}
public int getMaxConnectPerRoute() {
return maxConnectPerRoute;
}
public void setMaxConnectPerRoute(int maxConnectPerRoute) {
this.maxConnectPerRoute = maxConnectPerRoute;
}
public int getConnectTimeout() {
return connectTimeout;
}
public void setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
}
public int getReadTimeout() {
return readTimeout;
}
public void setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
}
public String getCharset() {
return charset;
}
public void setCharset(String charset) {
this.charset = charset;
}
public int getRetryTimes() {
return retryTimes;
}
public void setRetryTimes(int retryTimes) {
this.retryTimes = retryTimes;
}
publ