HttpClientUtil
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.io.UnsupportedEncodingException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLHandshakeException;
import org.apache.commons.io.IOUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.NameValuePair;
import org.apache.http.NoHttpResponseException;
import org.apache.http.client.HttpRequestRetryHandler;
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.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HttpContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
import io.leopard.javahost.JavaHost;
/**
* http/https 客户端连接工具类
*
*/
public class HttpClientUtil {
private static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
private static final int CONNECT_TIMEOUT = ConfigUtil.getInt("http.connect.timeout");// 设置连接建立的超时时间
private static final int SOCKET_TIMEOUT = ConfigUtil.getInt("http.socket.timeout");
private static final int MAX_CONN = ConfigUtil.getInt("http.max.pool.size"); // 最大连接数
private static final int Max_PRE_ROUTE = ConfigUtil.getInt("http.max.pool.size");
private static final int MAX_ROUTE = ConfigUtil.getInt("http.max.pool.size");
private static final int IDEL_TIMEOUT = ConfigUtil.getInt("http.idel.timeout"); // 空闲超时时间
private static final int MONITOR_INTERVAL = ConfigUtil.getInt("http.monitor.interval"); // 监控时间间隔
private static CloseableHttpClient httpClient; // 发送请求的客户端单例
private static PoolingHttpClientConnectionManager manager; // 连接池管理类
private static ScheduledExecutorService monitorExecutor;
public static String defaultEncoding = "utf-8";
private final static Object syncLock = new Object(); // 相当于线程锁,用于线程安全
/**
* 对http请求进行基本设置
*
* @param httpRequestBase http请求
*/
private static void setRequestConfig(HttpRequestBase httpRequestBase) {
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(CONNECT_TIMEOUT)
.setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
httpRequestBase.setConfig(requestConfig);
}
public static CloseableHttpClient getHttpClient(String url) {
String hostName = url.split("/")[2];
System.out.println(hostName);
int port = 80;
if (hostName.contains(":")) {
String[] args = hostName.split(":");
hostName = args[0];
port = Integer.parseInt(args[1]);
}
if (httpClient == null) {
// 多线程下多个线程同时调用getHttpClient容易导致重复创建httpClient对象的问题,所以加上了同步锁
synchronized (syncLock) {
if (httpClient == null) {
httpClient = createHttpClient(hostName, port);
// 开启监控线程,对异常和空闲线程进行关闭
monitorExecutor = Executors.newScheduledThreadPool(1);
monitorExecutor.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// 关闭异常连接
manager.closeExpiredConnections();
// 关闭5s空闲的连接
manager.closeIdleConnections(IDEL_TIMEOUT, TimeUnit.MILLISECONDS);
logger.info("close expired and idle connections.");
}
}, MONITOR_INTERVAL, MONITOR_INTERVAL, TimeUnit.MILLISECONDS);
}
}
}
return httpClient;
}
/**
* 根据host和port构建httpclient实例
*
* @param host 要访问的域名
* @param port 要访问的端口
* @return
*/
public static CloseableHttpClient createHttpClient(String host, int port) {
loadDns();
ConnectionSocketFactory plainSocketFactory = PlainConnectionSocketFactory.getSocketFactory();
LayeredConnectionSocketFactory sslSocketFactory = SSLConnectionSocketFactory.getSocketFactory();
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", plainSocketFactory).register("https", sslSocketFactory).build();
manager = new PoolingHttpClientConnectionManager(registry);
// 设置连接参数
manager.setMaxTotal(MAX_CONN); // 最大连接数
manager.setDefaultMaxPerRoute(Max_PRE_ROUTE); // 路由最大连接数
HttpHost httpHost = new HttpHost(host, port);
manager.setMaxPerRoute(new HttpRoute(httpHost), MAX_ROUTE);
// 请求失败时,进行请求重试
HttpRequestRetryHandler handler = new HttpRequestRetryHandler() {
public boolean retryRequest(IOException e, int i, HttpContext httpContext) {
if (i > 3) {
// 重试超过3次,放弃请求
logger.error("retry has more than 3 time, give up request");
return false;
}
if (e instanceof NoHttpResponseException) {
// 服务器没有响应,可能是服务器断开了连接,应该重试
logger.error("receive no response from server, retry");
return true;
}
if (e instanceof SSLHandshakeException) {
// SSL握手异常
logger.error("SSL hand shake exception");
return false;
}
if (e instanceof InterruptedIOException) {
// 超时
logger.error("InterruptedIOException");
return false;
}
if (e instanceof UnknownHostException) {
// 服务器不可达
logger.error("server host unknown");
return false;
}
if (e instanceof ConnectTimeoutException) {
// 连接超时
logger.error("Connection Time out");
return false;
}
if (e instanceof SSLException) {
logger.error("SSLException");
return false;
}
HttpClientContext context = HttpClientContext.adapt(httpContext);
HttpRequest request = context.getRequest();
if (!(request instanceof HttpEntityEnclosingRequest)) {
// 如果请求不是关闭连接的请求
return true;
}
return false;
}
};
CloseableHttpClient client = HttpClients.custom().setConnectionManager(manager).setRetryHandler(handler)
.build();
return client;
}
/**
* 设置post请求的参数
*
* @param httpPost
* @param params
*/
private static void setPostParams(HttpPost httpPost, Map<String, String> params) {
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
Set<String> keys = params.keySet();
for (String key : keys) {
nvps.add(new BasicNameValuePair(key, params.get(key)));
}
try {
httpPost.setEntity(new UrlEncodedFormEntity(nvps, defaultEncoding));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public static JSONObject post(String url, Map<String, String> params) {
HttpPost httpPost = new HttpPost(url);
setRequestConfig(httpPost);
setPostParams(httpPost, params);
return execPost(httpPost, url);
}
public static JSONObject postJson(String url, Map<String, String> headers, String json) {
HttpPost httpPost = new HttpPost(url);
setRequestConfig(httpPost);
setPostHeaders(httpPost, headers);
setPostEntity(httpPost, json);
return execPost(httpPost, url);
}
private static JSONObject execPost(HttpPost httpPost, String url) {
CloseableHttpResponse response = null;
InputStream in = null;
JSONObject object = null;
try {
response = getHttpClient(url).execute(httpPost, HttpClientContext.create());
HttpEntity entity = response.getEntity();
if (entity != null) {
in = entity.getContent();
String result = IOUtils.toString(in, defaultEncoding);
object = JSONObject.parseObject(result);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (in != null)
in.close();
if (response != null)
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return object;
}
private static void setPostEntity(HttpPost httpPost, String json) {
StringEntity requestEntity = new StringEntity(json, defaultEncoding);
requestEntity.setContentEncoding(defaultEncoding);
httpPost.setHeader("Content-type", "application/json");
httpPost.setEntity((HttpEntity) requestEntity);
}
/**
* 设置header信息
*
* @param httpPost
* @param headers
*/
private static void setPostHeaders(HttpPost httpPost, Map<String, String> headers) {
if (null != headers && headers.size() > 0) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
httpPost.addHeader((Header) new BasicHeader(key, value));
}
}
}
/**
* 关闭连接池
*/
public static void close() {
try {
httpClient.close();
manager.close();
monitorExecutor.shutdown();
logger.info("关闭连接池.");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* JVM虚拟DNS,用于解决https关于域名解析问题
*
*/
public static void loadDns() {
Properties props = new Properties();
props.put(ConfigUtil.get("host.name"), ConfigUtil.get("host.ip"));
JavaHost.updateVirtualDns(props); // 更新虚拟dns
JavaHost.printAllVirtualDns();// 打印所有虚拟DNS记录
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 配置管理类
*
*/
public class ConfigUtil {
private static final Logger logger = LoggerFactory.getLogger(ConfigUtil.class);
private static Properties prop = null;
private static void init() {
try {
prop = new Properties();
InputStream ips = (InputStream) ConfigUtil.class.getResourceAsStream("/demo.properties");
prop.load(new BufferedReader(new InputStreamReader(ips)));
ips.close();
} catch (IOException e) {
logger.error("读取配置文件失败:{}", e.getMessage());
}
}
/**
* 获取字符串属性值
*
* @param key 属性
* @return
*/
public static String get(String key) {
if (prop == null)
init();
return prop.getProperty(key);
}
/**
* 获取整型属性值
*
* @param key 属性
* @return
*/
public static int getInt(String key) {
return Integer.valueOf(get(key));
}
}
# http configs
http.connect.timeout=3000
http.socket.timeout=3000
http.max.pool.size=10
http.monitor.interval=3000
http.idel.timeout=2000
# jvm dns
host.name=www.demo.com
host.ip=192.168.0.123
<dependencies>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.71version>
dependency>
<dependency>
<groupId>commons-codecgroupId>
<artifactId>commons-codecartifactId>
<version>1.14version>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-lang3artifactId>
<version>3.10version>
dependency>
<dependency>
<groupId>commons-iogroupId>
<artifactId>commons-ioartifactId>
<version>2.7version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.30version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>1.7.30version>
dependency>
<dependency>
<groupId>org.apache.httpcomponentsgroupId>
<artifactId>httpclientartifactId>
<version>4.5.12version>
dependency>
<dependency>
<groupId>org.apache.httpcomponentsgroupId>
<artifactId>httpcoreartifactId>
<version>4.4.13version>
dependency>
<dependency>
<groupId>io.leopardgroupId>
<artifactId>javahostartifactId>
<version>0.9.12version>
dependency>
dependencies>