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