소스 검색

修改http工具

xueyiming 8 달 전
부모
커밋
f733588c55

+ 78 - 1
we-com-server/src/main/java/com/tzld/piaoquan/wecom/component/HttpPoolClient.java

@@ -1,17 +1,94 @@
 package com.tzld.piaoquan.wecom.component;
 
-import org.apache.http.client.methods.HttpGet;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpStatus;
+import org.apache.http.client.methods.*;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.entity.mime.MultipartEntityBuilder;
 import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.util.EntityUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
+import java.io.File;
 import java.io.IOException;
+import java.net.SocketTimeoutException;
+import java.nio.charset.StandardCharsets;
 
+@Slf4j
 @Component
 public class HttpPoolClient {
 
     @Autowired
     private CloseableHttpClient httpClient;
 
+    public String get(String url) throws IOException {
+        HttpGet httpGet = new HttpGet(url);
+        return request(httpGet);
+    }
+
+    public String post(String url) throws IOException {
+        HttpPost httpPost = new HttpPost(url);
+        return request(httpPost);
+    }
+
+
+    public String post(String url, String json) throws IOException {
+        HttpPost httpPost = new HttpPost(url);
+        if (StringUtils.isBlank(json)) {
+            return request(httpPost);
+        }
+        StringEntity entity = new StringEntity(json, StandardCharsets.UTF_8);
+        entity.setContentEncoding("UTF-8");
+        entity.setContentType("application/json");
+        httpPost.setEntity(entity);
+        return request(httpPost);
+    }
+
+    public String post(String url, File file) throws IOException {
+        HttpPost uploadFile = new HttpPost(url);
+        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
+        builder.addBinaryBody("media", file);
+        uploadFile.setEntity(builder.build());
+        return request(uploadFile);
+    }
+
+    public String request(HttpRequestBase request) throws IOException {
+
+        HttpEntity entity = null;
+        CloseableHttpResponse response = request((HttpUriRequest) request);
+        if (response == null) {
+            log.error("call api exception no response");
+            throw new RuntimeException("call api exception no response");
+        }
+        entity = response.getEntity();
+        String content = null;
+        if (entity != null) {
+            content = EntityUtils.toString(entity, "UTF-8");
+        }
+        int httpStatus = response.getStatusLine().getStatusCode();
+        if (httpStatus == HttpStatus.SC_OK) {
+            return content;
+        }
+        String path = request.getURI().toString();
+        log.error("http call api {} fail response status {} content {}", path, httpStatus, content);
+        return null;
+    }
+
+
+    public CloseableHttpResponse request(HttpUriRequest request) {
+        try {
+            return httpClient.execute(request);
+        } catch (Exception e) {
+            String path = request.getURI().toString();
+            if (e instanceof SocketTimeoutException) {
+                log.error(String.format("http timeout request url = %s .", path));
+            }
+            throw new RuntimeException(String.format("http exception request url = %s ", path), e);
+        }
+    }
+
 
 }

+ 35 - 11
we-com-server/src/main/java/com/tzld/piaoquan/wecom/config/HttpClientConfig.java

@@ -1,6 +1,5 @@
 package com.tzld.piaoquan.wecom.config;
 
-import com.tzld.piaoquan.wecom.utils.HttpPoolClient;
 import org.apache.http.client.config.RequestConfig;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
@@ -12,29 +11,54 @@ import org.springframework.context.annotation.Configuration;
 @Configuration
 public class HttpClientConfig {
 
-    int connectTimeout = 30000;
-    int socketTimeout = 30000;
-    int connectionWaitTimeout = 30000;
 
-    int retryCount = 5;
+    /**
+     * 链接建立的超时时间 ms
+     */
+    private static final int CONNECTION_TIMEOUT = 3000;
+    /**
+     * 响应超时时间 ms
+     */
+    private static final int SOCKET_TIMEOUT = 10000;
+
+    /**
+     * 每个路由的最大连接数
+     */
+    private static final int MAX_PER_ROUTE = 20;
+
+    /**
+     * 最大连接数
+     */
+    private static final int MAX_TOTAL = 100;
+
+    /**
+     * 重试次数,默认0
+     */
+    private static final int RETRY_COUNT = 3;
+
+    /**
+     * 从connection pool中获得一个connection的超时时间 ms
+     */
+    private static final int CONNECTION_WAIT_TIMEOUT = 2000;
+
 
     @Bean
     public CloseableHttpClient httpClient() {
 
         RequestConfig requestConfig = RequestConfig.custom()
-                .setConnectTimeout(connectTimeout)
-                .setSocketTimeout(socketTimeout)
-                .setConnectionRequestTimeout(connectionWaitTimeout)
+                .setConnectTimeout(CONNECTION_TIMEOUT)
+                .setSocketTimeout(SOCKET_TIMEOUT)
+                .setConnectionRequestTimeout(CONNECTION_WAIT_TIMEOUT)
                 .build();
 
         PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
-        connectionManager.setMaxTotal(100); // 设置最大连接数
-        connectionManager.setDefaultMaxPerRoute(20); // 每个路由的最大连接数
+        connectionManager.setMaxTotal(MAX_TOTAL); // 设置最大连接数
+        connectionManager.setDefaultMaxPerRoute(MAX_PER_ROUTE); // 每个路由的最大连接数
 
         return HttpClientBuilder.create()
                 .setDefaultRequestConfig(requestConfig)
                 .setConnectionManager(connectionManager)
-                .setRetryHandler(new DefaultHttpRequestRetryHandler(retryCount, false))
+                .setRetryHandler(new DefaultHttpRequestRetryHandler(RETRY_COUNT, false))
                 .build();
     }
 }

+ 6 - 6
we-com-server/src/main/java/com/tzld/piaoquan/wecom/job/WeComHistoryDataJob.java

@@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONObject;
 import com.google.common.collect.Lists;
 import com.tzld.piaoquan.wecom.common.constant.TimeConstant;
 import com.tzld.piaoquan.wecom.common.enums.MessageAttachmentTypeEnum;
+import com.tzld.piaoquan.wecom.component.HttpPoolClient;
 import com.tzld.piaoquan.wecom.dao.mapper.HistoryMessageMapper;
 import com.tzld.piaoquan.wecom.dao.mapper.StaffMapper;
 import com.tzld.piaoquan.wecom.dao.mapper.UserMapper;
@@ -18,7 +19,6 @@ import com.tzld.piaoquan.wecom.service.HistoryMessageService;
 import com.tzld.piaoquan.wecom.service.MessageAttachmentService;
 import com.tzld.piaoquan.wecom.service.UserService;
 import com.tzld.piaoquan.wecom.utils.HttpClientUtil;
-import com.tzld.piaoquan.wecom.utils.HttpPoolClient;
 import com.tzld.piaoquan.wecom.utils.LarkRobotUtil;
 import com.tzld.piaoquan.wecom.utils.MessageUtil;
 import com.tzld.piaoquan.wecom.utils.page.Page;
@@ -42,8 +42,8 @@ import static com.tzld.piaoquan.wecom.common.constant.WeComConstant.*;
 @Component
 public class WeComHistoryDataJob {
 
-    private static final HttpPoolClient httpPoolClientDefault = HttpClientUtil.create(30000, 30000, 2000, 5000, 5, 30000);
-
+    @Autowired
+    private HttpPoolClient httpPoolClient;
 
     @Autowired
     private AccessTokenService accessTokenService;
@@ -267,7 +267,7 @@ public class WeComHistoryDataJob {
         if (StringUtils.isNotEmpty(cursor)) {
             param.put("cursor", cursor);
         }
-        return httpPoolClientDefault.post(url, param.toJSONString());
+        return httpPoolClient.post(url, param.toJSONString());
     }
 
 
@@ -308,7 +308,7 @@ public class WeComHistoryDataJob {
         if (StringUtils.isNotEmpty(cursor)) {
             param.put("cursor", cursor);
         }
-        return httpPoolClientDefault.post(url, param.toJSONString());
+        return httpPoolClient.post(url, param.toJSONString());
     }
 
     private void delHistoryMessageList(Long startTime, Long endTime) {
@@ -371,7 +371,7 @@ public class WeComHistoryDataJob {
         if (StringUtils.isNotEmpty(cursor)) {
             param.put("cursor", cursor);
         }
-        return httpPoolClientDefault.post(url, param.toJSONString());
+        return httpPoolClient.post(url, param.toJSONString());
     }
 
 

+ 4 - 4
we-com-server/src/main/java/com/tzld/piaoquan/wecom/job/WeComStaffDataJob.java

@@ -1,12 +1,12 @@
 package com.tzld.piaoquan.wecom.job;
 
 import com.alibaba.fastjson.JSONObject;
+import com.tzld.piaoquan.wecom.component.HttpPoolClient;
 import com.tzld.piaoquan.wecom.dao.mapper.StaffMapper;
 import com.tzld.piaoquan.wecom.model.po.Staff;
 import com.tzld.piaoquan.wecom.model.po.StaffExample;
 import com.tzld.piaoquan.wecom.service.AccessTokenService;
 import com.tzld.piaoquan.wecom.utils.HttpClientUtil;
-import com.tzld.piaoquan.wecom.utils.HttpPoolClient;
 import com.tzld.piaoquan.wecom.utils.LarkRobotUtil;
 import com.xxl.job.core.biz.model.ReturnT;
 import com.xxl.job.core.handler.annotation.XxlJob;
@@ -25,8 +25,8 @@ import static com.tzld.piaoquan.wecom.common.constant.WeComConstant.GET_WE_COM_F
 @Component
 public class WeComStaffDataJob {
 
-    private static final HttpPoolClient httpPoolClientDefault = HttpClientUtil.create(30000, 30000, 2000, 5000, 5, 30000);
-
+    @Autowired
+    private HttpPoolClient httpPoolClient;
 
     @Autowired
     private AccessTokenService accessTokenService;
@@ -59,7 +59,7 @@ public class WeComStaffDataJob {
     public List<String> getCarrierIdList() throws IOException {
         String weComAccessToken = accessTokenService.getWeComAccessToken();
         String url = String.format(GET_WE_COM_FOLLOW_USER_LIST + "?access_token=%s", weComAccessToken);
-        String res = httpPoolClientDefault.get(url);
+        String res = httpPoolClient.get(url);
         JSONObject jsonObject = JSONObject.parseObject(res);
         Integer errcode = jsonObject.getInteger("errcode");
         if (errcode == 0) {

+ 6 - 6
we-com-server/src/main/java/com/tzld/piaoquan/wecom/job/WeComUserDataJob.java

@@ -2,6 +2,7 @@ package com.tzld.piaoquan.wecom.job;
 
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.tzld.piaoquan.wecom.component.HttpPoolClient;
 import com.tzld.piaoquan.wecom.dao.mapper.StaffMapper;
 import com.tzld.piaoquan.wecom.dao.mapper.StaffWithUserMapper;
 import com.tzld.piaoquan.wecom.dao.mapper.UserMapper;
@@ -10,7 +11,6 @@ import com.tzld.piaoquan.wecom.model.po.*;
 import com.tzld.piaoquan.wecom.service.AccessTokenService;
 import com.tzld.piaoquan.wecom.service.UserService;
 import com.tzld.piaoquan.wecom.utils.HttpClientUtil;
-import com.tzld.piaoquan.wecom.utils.HttpPoolClient;
 import com.tzld.piaoquan.wecom.utils.LarkRobotUtil;
 import com.tzld.piaoquan.wecom.utils.page.Page;
 import com.xxl.job.core.biz.model.ReturnT;
@@ -31,8 +31,8 @@ import static com.tzld.piaoquan.wecom.common.constant.WeComConstant.*;
 @Component
 public class WeComUserDataJob {
 
-    private static final HttpPoolClient httpPoolClientDefault = HttpClientUtil.create(30000, 30000, 2000, 5000, 5, 30000);
-
+    @Autowired
+    private HttpPoolClient httpPoolClient;
     @Autowired
     private StaffMapper staffMapper;
     @Autowired
@@ -144,7 +144,7 @@ public class WeComUserDataJob {
         if (StringUtils.isNotEmpty(cursor)) {
             param.put("cursor", cursor);
         }
-        return httpPoolClientDefault.post(url, param.toJSONString());
+        return httpPoolClient.post(url, param.toJSONString());
     }
 
     @XxlJob("insertStaffWithUserJob")
@@ -173,7 +173,7 @@ public class WeComUserDataJob {
     public List<String> getUserList(String userId) throws IOException {
         String weComAccessToken = accessTokenService.getWeComAccessToken();
         String url = String.format(GET_WE_COM_EXTERNAL_CONTACT_LIST + "?access_token=%s&userid=%s", weComAccessToken, userId);
-        String res = httpPoolClientDefault.get(url);
+        String res = httpPoolClient.get(url);
         JSONObject jsonObject = JSONObject.parseObject(res);
         Integer errcode = jsonObject.getInteger("errcode");
         if (errcode == 0) {
@@ -225,7 +225,7 @@ public class WeComUserDataJob {
         JSONObject param = new JSONObject();
         param.put("external_userid", externalUserId3rdParty);
         param.put("source_agentid", 1000009);
-        String res = httpPoolClientDefault.post(url, param.toJSONString());
+        String res = httpPoolClient.post(url, param.toJSONString());
         JSONObject jsonObject = JSONObject.parseObject(res);
         Integer errcode = jsonObject.getInteger("errcode");
         if (errcode == 0) {

+ 5 - 5
we-com-server/src/main/java/com/tzld/piaoquan/wecom/service/Impl/AccessTokenServiceImpl.java

@@ -1,9 +1,9 @@
 package com.tzld.piaoquan.wecom.service.Impl;
 
 import com.alibaba.fastjson.JSONObject;
+import com.tzld.piaoquan.wecom.component.HttpPoolClient;
 import com.tzld.piaoquan.wecom.service.AccessTokenService;
 import com.tzld.piaoquan.wecom.utils.HttpClientUtil;
-import com.tzld.piaoquan.wecom.utils.HttpPoolClient;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -21,8 +21,8 @@ import static com.tzld.piaoquan.wecom.common.constant.WeComConstant.*;
 @Service
 public class AccessTokenServiceImpl implements AccessTokenService {
 
-    private static final HttpPoolClient httpPoolClientDefault = HttpClientUtil.create(30000, 30000, 2000, 5000, 5, 30000);
-
+    @Autowired
+    private HttpPoolClient httpPoolClient;
     @Autowired
     private RedisTemplate<String, Object> redisTemplate;
 
@@ -36,7 +36,7 @@ public class AccessTokenServiceImpl implements AccessTokenService {
         param.put("corp_id", CROP_ID);
         param.put("secret", SECRET);
         try {
-            String res = httpPoolClientDefault.post(POST_ACCESS_TOKEN_URL, param.toJSONString());
+            String res = httpPoolClient.post(POST_ACCESS_TOKEN_URL, param.toJSONString());
             JSONObject jsonObject = JSONObject.parseObject(res);
             Long expiresIn = jsonObject.getLong("expires_in");
             String newAccessToken = jsonObject.getString("access_token");
@@ -58,7 +58,7 @@ public class AccessTokenServiceImpl implements AccessTokenService {
         param.put("corp_id", CROP_ID);
         param.put("secret", SECRET);
         try {
-            String res = httpPoolClientDefault.get(String.format(GET_WE_COM_ACCESS_TOKEN_URL + "?corpid=%s&corpsecret=%s", WE_COM_CROP_ID, WE_COM_SECRET));
+            String res = httpPoolClient.get(String.format(GET_WE_COM_ACCESS_TOKEN_URL + "?corpid=%s&corpsecret=%s", WE_COM_CROP_ID, WE_COM_SECRET));
             JSONObject jsonObject = JSONObject.parseObject(res);
             Long expiresIn = jsonObject.getLong("expires_in");
             String newAccessToken = jsonObject.getString("access_token");

+ 7 - 7
we-com-server/src/main/java/com/tzld/piaoquan/wecom/service/Impl/MessageAttachmentServiceImpl.java

@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.tzld.piaoquan.wecom.common.base.CommonResponse;
 import com.tzld.piaoquan.wecom.common.enums.MessageAttachmentTypeEnum;
+import com.tzld.piaoquan.wecom.component.HttpPoolClient;
 import com.tzld.piaoquan.wecom.dao.mapper.MessageAttachmentMapper;
 import com.tzld.piaoquan.wecom.model.bo.AdPutFlowParam;
 import com.tzld.piaoquan.wecom.model.bo.VideoDetail;
@@ -16,7 +17,6 @@ import com.tzld.piaoquan.wecom.service.AccessTokenService;
 import com.tzld.piaoquan.wecom.service.MessageAttachmentService;
 import com.tzld.piaoquan.wecom.utils.DateUtil;
 import com.tzld.piaoquan.wecom.utils.HttpClientUtil;
-import com.tzld.piaoquan.wecom.utils.HttpPoolClient;
 import com.tzld.piaoquan.wecom.utils.LarkRobotUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
@@ -48,8 +48,8 @@ import static com.tzld.piaoquan.wecom.common.constant.WeComConstant.POST_WE_COM_
 @Service
 public class MessageAttachmentServiceImpl implements MessageAttachmentService {
 
-    private static final HttpPoolClient httpPoolClientDefault = HttpClientUtil.create(30000, 30000, 2000, 5000, 5, 30000);
-
+    @Autowired
+    private HttpPoolClient httpPoolClient;
     @Autowired
     private AccessTokenService accessTokenService;
 
@@ -119,7 +119,7 @@ public class MessageAttachmentServiceImpl implements MessageAttachmentService {
             Map<Long, VideoDetail> map = new HashMap<>();
             JSONObject params = new JSONObject();
             params.put("videoIdList", videoIdList);
-            String post = httpPoolClientDefault.post(POST_VIDEO_DETAIL_URL, params.toJSONString());
+            String post = httpPoolClient.post(POST_VIDEO_DETAIL_URL, params.toJSONString());
             JSONObject res = JSONObject.parseObject(post);
             JSONArray data = res.getJSONArray("data");
             for (int i = 0; i < data.size(); i++) {
@@ -164,7 +164,7 @@ public class MessageAttachmentServiceImpl implements MessageAttachmentService {
             File file = new java.io.File(filePath);
             String weComAccessToken = accessTokenService.getWeComAccessToken();
             String url = String.format(POST_WE_COM_MEDIA_UPLOAD + "?access_token=%s&type=%s", weComAccessToken, "image");
-            String res = httpPoolClientDefault.post(url, file);
+            String res = httpPoolClient.post(url, file);
             JSONObject jsonObject = JSONObject.parseObject(res);
             if (jsonObject != null && jsonObject.getInteger("errcode") == 0) {
                 mediaId = jsonObject.getString("media_id");
@@ -213,7 +213,7 @@ public class MessageAttachmentServiceImpl implements MessageAttachmentService {
                 .replace("${uuid}", "" + UUID.randomUUID());
         requestParam.put("jumpPage", jumpPage);
         param.setRequestParam(requestParam);
-        String res = httpPoolClientDefault.post(POST_ADD_TENCENT, JSONObject.toJSONString(param));
+        String res = httpPoolClient.post(POST_ADD_TENCENT, JSONObject.toJSONString(param));
         JSONObject jsonObject = JSONObject.parseObject(res);
         JSONObject data = jsonObject.getJSONObject("data");
         return data.getString("url");
@@ -224,7 +224,7 @@ public class MessageAttachmentServiceImpl implements MessageAttachmentService {
         for (int n = 1; n <= totalPage; n++) {
             String url = getUrl(videoId, n);
             try {
-                String res = httpPoolClientDefault.get(url);
+                String res = httpPoolClient.get(url);
                 JSONObject jsonObject = JSONObject.parseObject(res);
                 JSONObject data = jsonObject.getJSONObject("data");
                 if (data == null) {

+ 4 - 4
we-com-server/src/main/java/com/tzld/piaoquan/wecom/service/Impl/MessageServiceImpl.java

@@ -3,11 +3,11 @@ package com.tzld.piaoquan.wecom.service.Impl;
 import com.alibaba.fastjson.JSONObject;
 import com.tzld.piaoquan.wecom.common.constant.MessageConstant;
 import com.tzld.piaoquan.wecom.common.exception.CustomizeException;
+import com.tzld.piaoquan.wecom.component.HttpPoolClient;
 import com.tzld.piaoquan.wecom.model.vo.MessageTextParam;
 import com.tzld.piaoquan.wecom.service.AccessTokenService;
 import com.tzld.piaoquan.wecom.service.MessageService;
 import com.tzld.piaoquan.wecom.utils.HttpClientUtil;
-import com.tzld.piaoquan.wecom.utils.HttpPoolClient;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -27,8 +27,8 @@ import static com.tzld.piaoquan.wecom.common.constant.WeComConstant.POST_MESSAGE
 @Service
 public class MessageServiceImpl implements MessageService {
 
-    private static final HttpPoolClient httpPoolClientDefault = HttpClientUtil.create(30000, 30000, 2000, 5000, 5, 30000);
-
+    @Autowired
+    private HttpPoolClient httpPoolClient;
     @Autowired
     private AccessTokenService accessTokenService;
 
@@ -40,7 +40,7 @@ public class MessageServiceImpl implements MessageService {
             String accessToken = accessTokenService.getAccessToken();
             String url = POST_MESSAGE_PUSH_URL
                     + "?access_token=" + accessToken;
-            String s = httpPoolClientDefault.post(url, jsonObject.toJSONString());
+            String s = httpPoolClient.post(url, jsonObject.toJSONString());
             JSONObject res = JSONObject.parseObject(s);
             log.info("pushMessage res={}", res);
             Integer code = res.getInteger("errcode");

+ 4 - 4
we-com-server/src/main/java/com/tzld/piaoquan/wecom/service/Impl/UserServiceImpl.java

@@ -2,6 +2,7 @@ package com.tzld.piaoquan.wecom.service.Impl;
 
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.tzld.piaoquan.wecom.component.HttpPoolClient;
 import com.tzld.piaoquan.wecom.dao.mapper.StaffMapper;
 import com.tzld.piaoquan.wecom.dao.mapper.StaffWithUserMapper;
 import com.tzld.piaoquan.wecom.dao.mapper.UserMapper;
@@ -9,7 +10,6 @@ import com.tzld.piaoquan.wecom.model.po.*;
 import com.tzld.piaoquan.wecom.service.AccessTokenService;
 import com.tzld.piaoquan.wecom.service.UserService;
 import com.tzld.piaoquan.wecom.utils.HttpClientUtil;
-import com.tzld.piaoquan.wecom.utils.HttpPoolClient;
 import com.tzld.piaoquan.wecom.utils.LarkRobotUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
@@ -27,8 +27,8 @@ import static com.tzld.piaoquan.wecom.common.constant.WeComConstant.GET_WE_COM_E
 @Slf4j
 @Service
 public class UserServiceImpl implements UserService {
-    private static final HttpPoolClient httpPoolClientDefault = HttpClientUtil.create(30000, 30000, 2000, 5000, 5, 30000);
-
+    @Autowired
+    private HttpPoolClient httpPoolClient;
     @Autowired
     private AccessTokenService accessTokenService;
 
@@ -207,7 +207,7 @@ public class UserServiceImpl implements UserService {
     public JSONObject getUserDetail(String externalUserId) throws IOException {
         String weComAccessToken = accessTokenService.getWeComAccessToken();
         String url = String.format(GET_WE_COM_EXTERNAL_CONTACT_GET + "?access_token=%s&external_userid=%s", weComAccessToken, externalUserId);
-        String res = httpPoolClientDefault.get(url);
+        String res = httpPoolClient.get(url);
         JSONObject jsonObject = JSONObject.parseObject(res);
         Integer errcode = jsonObject.getInteger("errcode");
         if (errcode == 0) {

+ 13 - 13
we-com-server/src/main/java/com/tzld/piaoquan/wecom/utils/HttpClientUtil.java

@@ -43,34 +43,34 @@ public class HttpClientUtil {
      */
     private static final int DEFAULT_CONNECTION_WAIT_TIMEOUT = 300;
 
-    private static final Map<String, HttpPoolClient> CREATED_HTTP_CLIENTS = new HashMap<>();
+    private static final Map<String, HttpPoolClientUtil> CREATED_HTTP_CLIENTS = new HashMap<>();
 
     private static final Lock LOCK = new ReentrantLock();
 
     private HttpClientUtil() {
     }
 
-    public static HttpPoolClient useDefault() {
+    public static HttpPoolClientUtil useDefault() {
         return createCached(DEFAULT_SHARED_KEY);
     }
 
 
-    public static HttpPoolClient createCached(String cachedKey) {
-        HttpPoolClient httpPoolClient = CREATED_HTTP_CLIENTS.get(cachedKey);
-        if (httpPoolClient != null) {
-            return httpPoolClient;
+    public static HttpPoolClientUtil createCached(String cachedKey) {
+        HttpPoolClientUtil httpPoolClientUtil = CREATED_HTTP_CLIENTS.get(cachedKey);
+        if (httpPoolClientUtil != null) {
+            return httpPoolClientUtil;
         }
         LOCK.lock();
         try {
-            httpPoolClient = CREATED_HTTP_CLIENTS.get(cachedKey);
-            if (httpPoolClient == null) {
-                httpPoolClient = create(DEFAULT_CONNECTION_TIMEOUT, DEFAULT_SOCKET_TIMEOUT, DEFAULT_DEFAULT_MAX_PER_ROUTE, DEFAULT_DEFAULT_MAX_TOTAL, DEFAULT_RETRY_COUNT, DEFAULT_CONNECTION_WAIT_TIMEOUT);
-                CREATED_HTTP_CLIENTS.put(cachedKey, httpPoolClient);
+            httpPoolClientUtil = CREATED_HTTP_CLIENTS.get(cachedKey);
+            if (httpPoolClientUtil == null) {
+                httpPoolClientUtil = create(DEFAULT_CONNECTION_TIMEOUT, DEFAULT_SOCKET_TIMEOUT, DEFAULT_DEFAULT_MAX_PER_ROUTE, DEFAULT_DEFAULT_MAX_TOTAL, DEFAULT_RETRY_COUNT, DEFAULT_CONNECTION_WAIT_TIMEOUT);
+                CREATED_HTTP_CLIENTS.put(cachedKey, httpPoolClientUtil);
             }
         } finally {
             LOCK.unlock();
         }
-        return httpPoolClient;
+        return httpPoolClientUtil;
     }
 
     /**
@@ -84,7 +84,7 @@ public class HttpClientUtil {
      * @param connectionWaitTimeout 连接等待超市时间 ms
      * @return httpclient instance
      */
-    public static HttpPoolClient create(int connectTimeout, int socketTimeout, int maxPerRoute, int maxTotal, int retryCount, int connectionWaitTimeout) {
-        return HttpPoolClient.create(connectTimeout, socketTimeout, maxPerRoute, maxTotal, retryCount, connectionWaitTimeout);
+    public static HttpPoolClientUtil create(int connectTimeout, int socketTimeout, int maxPerRoute, int maxTotal, int retryCount, int connectionWaitTimeout) {
+        return HttpPoolClientUtil.create(connectTimeout, socketTimeout, maxPerRoute, maxTotal, retryCount, connectionWaitTimeout);
     }
 }

+ 5 - 5
we-com-server/src/main/java/com/tzld/piaoquan/wecom/utils/HttpPoolClient.java → we-com-server/src/main/java/com/tzld/piaoquan/wecom/utils/HttpPoolClientUtil.java

@@ -45,9 +45,9 @@ import java.util.concurrent.TimeUnit;
  *
  * @author xueyiming
  */
-public class HttpPoolClient {
+public class HttpPoolClientUtil {
 
-    private static final Logger LOGGER = LoggerFactory.getLogger(HttpPoolClient.class);
+    private static final Logger LOGGER = LoggerFactory.getLogger(HttpPoolClientUtil.class);
 
     private static final ScheduledExecutorService SCHEDULED_CLOSED_EXECUTOR = new ScheduledThreadPoolExecutor(1,
             new BasicThreadFactory.Builder().namingPattern("http conn-closed-thread-%s").priority(Thread.NORM_PRIORITY).daemon(false).build(), (r, e) -> LOGGER.error(" monitor push reject task error={}", e.toString()));
@@ -60,7 +60,7 @@ public class HttpPoolClient {
 
     private CloseableHttpClient closeableHttpClient;
 
-    private HttpPoolClient(CloseableHttpClient closeableHttpClient) {
+    private HttpPoolClientUtil(CloseableHttpClient closeableHttpClient) {
         this.closeableHttpClient = closeableHttpClient;
     }
 
@@ -164,14 +164,14 @@ public class HttpPoolClient {
      * @param retryCount     重试次数
      * @return httpclient instance
      */
-    protected static HttpPoolClient create(int connectTimeout, int socketTimeout, int maxPerRoute, int maxTotal, int retryCount, int connectionWaitTimeout) {
+    protected static HttpPoolClientUtil create(int connectTimeout, int socketTimeout, int maxPerRoute, int maxTotal, int retryCount, int connectionWaitTimeout) {
         try {
             RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(connectTimeout).setSocketTimeout(socketTimeout).setConnectionRequestTimeout(connectionWaitTimeout).build();
             CloseableHttpClient client = HttpClientBuilder.create()
                     .setDefaultRequestConfig(requestConfig)
                     .setConnectionManager(createConnectionManager(maxPerRoute, maxTotal))
                     .setRetryHandler(new DefaultHttpRequestRetryHandler(retryCount, false)).addInterceptorFirst(getInterceptor()).build();
-            return new HttpPoolClient(client);
+            return new HttpPoolClientUtil(client);
         } catch (Throwable e) {
             LOGGER.error("create HttpPoolClient exception", e);
             throw new RuntimeException("create HttpPoolClient exception");

+ 2 - 2
we-com-server/src/main/java/com/tzld/piaoquan/wecom/utils/LarkRobotUtil.java

@@ -17,7 +17,7 @@ public class LarkRobotUtil {
 
     private static final String URL = "https://open.feishu.cn/open-apis/bot/v2/hook/93787b70-33d3-42c1-beae-c09310c9b38b";
 
-    private static final HttpPoolClient httpPoolClientDefault = HttpClientUtil.create(30000, 30000, 2000, 5000, 5, 30000);
+    private static final HttpPoolClientUtil HTTP_POOL_CLIENT_UTIL_DEFAULT = HttpClientUtil.create(3000, 10000, 20, 100, 3, 3000);
 
 
     public static void sendMessage(String msg) {
@@ -35,7 +35,7 @@ public class LarkRobotUtil {
             JSONObject content = new JSONObject();
             content.put("text", msg);
             param.put("content", content);
-            httpPoolClientDefault.post(URL, param.toJSONString());
+            HTTP_POOL_CLIENT_UTIL_DEFAULT.post(URL, param.toJSONString());
         } catch (Exception e) {
             log.error("Lark sendMessage error", e);
         }