ClientUtil中client改为连接池方式
This commit is contained in:
5
pom.xml
5
pom.xml
@@ -700,5 +700,10 @@
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jersey.connectors</groupId>
|
||||
<artifactId>jersey-apache-connector</artifactId>
|
||||
<version>2.23.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@@ -736,4 +736,9 @@ public final class Constants {
|
||||
public static final Integer SAVE_AND_DEL_THREAD_SIZE=Configurations.getIntProperty("save_and_del_thread_size", 5);
|
||||
public static final Integer MAX_ALLOWED_PACKET=Configurations.getIntProperty("max_allowed_packet", 1048576);
|
||||
|
||||
//http client连接池相关参数
|
||||
public static final Integer HTTP_MAX_CONNECTION=Configurations.getIntProperty("http_max_connection", 1000);
|
||||
public static final Integer DEFAULT_MAX_PERROUTE=Configurations.getIntProperty("default_max_perroute", 100);
|
||||
public static final Integer POOLCM_SOCKET_TIMEOUT=Configurations.getIntProperty("poolcm_socket_timeout", 3000);
|
||||
|
||||
}
|
||||
|
||||
@@ -17,7 +17,12 @@ import net.sf.json.JSONObject;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.config.SocketConfig;
|
||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.glassfish.jersey.apache.connector.ApacheClientProperties;
|
||||
import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
|
||||
import org.glassfish.jersey.client.ClientConfig;
|
||||
import org.glassfish.jersey.client.ClientProperties;
|
||||
import org.glassfish.jersey.jackson.JacksonFeature;
|
||||
import org.glassfish.jersey.media.multipart.MultiPartFeature;
|
||||
@@ -29,6 +34,7 @@ public class ClientUtil {
|
||||
|
||||
private static Client client;//客户端
|
||||
private static Logger logger = Logger.getLogger(ClientUtil.class);
|
||||
|
||||
/**
|
||||
* 初始化https client
|
||||
* @throws Exception
|
||||
@@ -37,13 +43,24 @@ public class ClientUtil {
|
||||
*/
|
||||
public static void initClient(){
|
||||
try{
|
||||
client = ClientBuilder.newBuilder().register(ClientRequestHeaderFilter.class)//请求过滤器,自动添加header信息
|
||||
.register(JacksonFeature.class)//json支持
|
||||
.register(MultiPartFeature.class)//文件上传支持
|
||||
.property(ClientProperties.CONNECT_TIMEOUT, Constants.CLIENT_CONNECT_TIMEOUT)//连接超时时间
|
||||
.property(ClientProperties.READ_TIMEOUT, Constants.CLIENT_READ_TIMEOUT)//读取超时时间
|
||||
.build();
|
||||
logger.info("客户端初始化成功");
|
||||
if(client==null){
|
||||
PoolingHttpClientConnectionManager pcm = new PoolingHttpClientConnectionManager();
|
||||
pcm.setDefaultSocketConfig( SocketConfig.custom(). setSoTimeout(Constants.POOLCM_SOCKET_TIMEOUT). build());
|
||||
pcm.setMaxTotal(Constants.HTTP_MAX_CONNECTION); pcm.setDefaultMaxPerRoute(Constants.DEFAULT_MAX_PERROUTE);
|
||||
|
||||
ClientConfig clientConfig = new ClientConfig();
|
||||
clientConfig.property(ClientProperties.CONNECT_TIMEOUT, Constants.CLIENT_CONNECT_TIMEOUT);//连接超时时间
|
||||
clientConfig.property(ClientProperties.READ_TIMEOUT, Constants.CLIENT_READ_TIMEOUT);//读取超时时间
|
||||
clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER, pcm);
|
||||
clientConfig.connectorProvider(new ApacheConnectorProvider());
|
||||
clientConfig.register(JacksonFeature.class);
|
||||
|
||||
client = ClientBuilder.newClient(clientConfig).register(ClientRequestHeaderFilter.class)//请求过滤器,自动添加header信息
|
||||
.register(JacksonFeature.class)//json支持
|
||||
.register(MultiPartFeature.class);//文件上传支持
|
||||
logger.info("客户端初始化成功");
|
||||
}
|
||||
|
||||
}catch (Exception e) {
|
||||
logger.error("初始化客户端失败,请检查证书是否正确!",e);
|
||||
System.exit(1);//程序退出
|
||||
@@ -56,7 +73,9 @@ public class ClientUtil {
|
||||
* @return
|
||||
*/
|
||||
public static WebTarget getWebTarger(String path){
|
||||
initClient();
|
||||
if(client==null){
|
||||
initClient();
|
||||
}
|
||||
if(StringUtils.isNotBlank(path)){
|
||||
path = formatHttpStr(path);//格式化url字符串
|
||||
return client.target(path);
|
||||
|
||||
@@ -552,4 +552,8 @@ ntcCollectVoipLog=ntcCollectVoipLogs
|
||||
ntcKeywordsUrlLog=ntcKeywordsUrlLogs
|
||||
save_and_del_thread_size=5
|
||||
#mysql 单个sql大小限制,根据mariadb相关参数配置
|
||||
max_allowed_packet=1048576
|
||||
max_allowed_packet=1048576
|
||||
#http client连接池相关参数,http_max_connection:最大连接数,default_max_perroute:每个主机地址的并发数,poolcm_socket_timeout:socket超时时间
|
||||
http_max_connection=1000
|
||||
default_max_perroute=100
|
||||
poolcm_socket_timeout=3000
|
||||
Reference in New Issue
Block a user