springboot使用httpclient在高并发的情况下会出现Timeout waiting for connection from pool,经测试是因为和有些银行接口建立链接时会超时,有些则不会,可能和双方网络有关系,或者对方服务器限制了连接数,需要设长从连接池中获取到连接的最长时间http.connectionRequestTimeout=10000,之前是500,单位毫秒,另外并发数 http.defaultMaxPerRoute =200,需要设置大一点,之前是20。完整的配置文件参考如下:
#httpclient config
#最大连接数
http.maxTotal = 1000
#并发数
http.defaultMaxPerRoute = 200
#创建连接的最长时间,单位毫秒
http.connectTimeout=10000
#从连接池中获取到连接的最长时间,单位毫秒
http.connectionRequestTimeout=10000
#数据传输的最长时间,单位毫秒
http.socketTimeout=10000
#提交请求前测试连接是否可用
http.staleConnectionCheckEnabled=false
工具类HttpClientUtils.java也给出供参考
/**
*
*/
package com.chinapay.utils;
import java.net.URLDecoder;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.net.ssl.SSLContext;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.figo.util.log.MonitorLogger;
/**
* @author figo .
*
*/
@Component
public class HttpClientUtils {
/**
* logger .
*/
protected MonitorLogger logger = MonitorLogger.getLogger(getClass());
/**
* httpClient .
*/
@Autowired
private CloseableHttpClient httpClient;
/**
* config .
*/
@Autowired
private RequestConfig config;
/**
* 信任所有https请求,避免有些银行使用自己的CA生成证书,请求时会发生握手失败的情况 .
*/
@PostConstruct
public void init() {
try {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy(){
//信任所有
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
httpClient=HttpClients.custom().setSSLSocketFactory(sslsf).build();
} catch (Exception e) {
// TODO: handle exception
}
}
/**
* 不带参数的get请求,如果状态码为200,则返回body,如果不为200,则返回null .
*
* @param url .
* @return .
* @throws Exception .
*/
public String doGet(String url) throws Exception {
logger.info("doGet请求url="+url);
// 声明 http get 请求
HttpGet httpGet = new HttpGet(url);
// 装载配置信息
httpGet.setConfig(config);
// 发起请求
CloseableHttpResponse response = this.httpClient.execute(httpGet);
// 判断状态码是否为200
if (response.getStatusLine().getStatusCode() == 200) {
// 返回响应体的内容
return EntityUtils.toString(response.getEntity(), "UTF-8");
}
return null;
}
/**
* 带参数的get请求,如果状态码为200,则返回body,如果不为200,则返回null .
*
* @param url .
* @param map .
* @return .
* @throws Exception .
*/
public String doGet(String url, Map<String, Object> map) throws Exception {
URIBuilder uriBuilder = new URIBuilder(url);
if (map != null) {
// 遍历map,拼接请求参数
for (Map.Entry<String, Object> entry : map.entrySet()) {
uriBuilder.setParameter(entry.getKey(), entry.getValue().toString());
}
}
// 调用不带参数的get请求
return this.doGet(uriBuilder.build().toString());
}
/**
* 带参数的post请求 .
*
* @param url .
* @param map .
* @return .
* @throws Exception .
*/
public HttpResult doPost(String url, Map<String, Object> map) throws Exception {
logger.info("doPost请求url="+url);
// 声明httpPost请求
HttpPost httpPost = new HttpPost(url);
// 加入配置信息
httpPost.setConfig(config);
// 判断map是否为空,不为空则进行遍历,封装from表单对象
if (map != null) {
List<NameValuePair> list = new ArrayList<NameValuePair>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
list.add(new BasicNameValuePair(entry.getKey(), (String)entry.getValue()));
}
// 构造from表单对象
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list, "UTF-8");
// 把表单放到post里
httpPost.setEntity(urlEncodedFormEntity);
}
// 发起请求
CloseableHttpResponse response = this.httpClient.execute(httpPost);
String respBody=EntityUtils.toString(response.getEntity(), "UTF-8");
//2判断是否做了urlEncode如果做了,urldecode一下
if(UrlEncoderUtils.hasUrlEncoded(respBody))
{
logger.info("返回数据做了urlEncode,respBody="+respBody);
respBody=URLDecoder.decode(respBody, "UTF-8");
logger.info("urlDecode之后,respBody="+respBody);
}
logger.info("返回数据未做urlEncode,respBody="+respBody);
return new HttpResult(response.getStatusLine().getStatusCode(), respBody);
}
/**
* 不带参数post请求 .
*
* @param url .
* @return .
* @throws Exception .
*/
public HttpResult doPost(String url) throws Exception {
return this.doPost(url, null);
}
}
|