From 73420a13a6d75c8be6163b193142b3954324869e Mon Sep 17 00:00:00 2001 From: jianjun Date: Wed, 26 Oct 2022 16:23:57 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=B0=83=E7=94=A8=E5=85=B0=E5=9B=BE?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=AF=94=E5=AF=B9=E6=8E=A5=E5=8F=A3=20?= =?UTF-8?q?=E9=99=90=E6=B5=812?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/tools/utils/YtHsResUtils.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/YtHsResUtils.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/YtHsResUtils.java index d30e1087cb..9a4ac14de1 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/YtHsResUtils.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/YtHsResUtils.java @@ -6,11 +6,14 @@ import com.epmet.commons.tools.constant.StrConstant; import com.epmet.commons.tools.dto.result.YtDataSyncResDTO; import com.epmet.commons.tools.dto.result.YtHscyResDTO; import com.epmet.commons.tools.dto.result.YtHsjcResDTO; +import com.google.common.util.concurrent.RateLimiter; import lombok.extern.slf4j.Slf4j; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; /** * @Description @@ -24,6 +27,11 @@ public class YtHsResUtils { private static final String CARD_NO = "card_no"; private static final String ROW_NUM = "ROWNUM"; private static final String PAGE_SIZE = "PAGESIZE"; + /** + * rate is "2 permits per second" + */ + private static final Map rateLimiterMap = new ConcurrentHashMap<>(); + /** * desc:核酸采样查询 @@ -32,6 +40,8 @@ public class YtHsResUtils { */ public static YtHscyResDTO hscy(String cardNo, Integer rowNum, Integer pageSize) { try { + tryAcquire("hscy"); + Map param = new HashMap<>(); param.put(APP_KEY, "tz45j4kuWcnnjoJOVSlzGWJgI"); param.put(CARD_NO, cardNo); @@ -60,6 +70,12 @@ public class YtHsResUtils { return resultResult; } + private static void tryAcquire(String hscy) { + RateLimiter rateLimiter = rateLimiterMap.getOrDefault(hscy, RateLimiter.create(2.0)); + rateLimiterMap.putIfAbsent(hscy,rateLimiter); + rateLimiter.tryAcquire(1, TimeUnit.SECONDS); + } + /** * desc:核酸结果查询 * @@ -67,6 +83,8 @@ public class YtHsResUtils { */ public static YtHsjcResDTO hsjc(String cardNo, Integer rowNum, Integer pageSize) { try { + tryAcquire("hsjc"); + Map param = new HashMap<>(); param.put(APP_KEY, "DR4jF5Be7sCsqDmCamq2tmYCl"); param.put(CARD_NO, cardNo); @@ -102,6 +120,7 @@ public class YtHsResUtils { */ public static YtDataSyncResDTO siWang(String cardNo, String userName) { try { + tryAcquire("siWang"); // 1)appkey秘钥 // 2)name姓名 必填 // 3)idcard身份证号 必填 @@ -159,6 +178,7 @@ public class YtHsResUtils { * @remark:默认失败 因为一旦成功没有数据 会影响残疾人的数据 接口失败数据不做任何处理 */ public static YtDataSyncResDTO canji(String idCard, String userName) { + tryAcquire("canji"); YtDataSyncResDTO failResult = new YtDataSyncResDTO(); failResult.setCode(500); From e6dbca7432f73ab3707f3ae626662858bc457d84 Mon Sep 17 00:00:00 2001 From: jianjun Date: Wed, 26 Oct 2022 23:10:36 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=AD=BB=E4=BA=A1=20=E6=AD=A3=E5=B8=B8?= =?UTF-8?q?=E7=9A=84=20=E6=98=AF=E9=94=99=E8=AF=AF=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../epmet/commons/tools/utils/YtHsResUtils.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/YtHsResUtils.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/YtHsResUtils.java index 9a4ac14de1..9010c9b84a 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/YtHsResUtils.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/YtHsResUtils.java @@ -63,7 +63,7 @@ public class YtHsResUtils { } } } catch (Exception e) { - log.error(String.format("烟台核酸检测结果查询异常cardNo:%s,异常信息:%s", cardNo, e.getMessage())); + log.error(String.format("烟台核酸检测结果查询异常cardNo:%s", cardNo), e); } YtHscyResDTO resultResult = new YtHscyResDTO(); resultResult.setData(new ArrayList<>()); @@ -106,7 +106,7 @@ public class YtHsResUtils { } } catch (Exception e) { - log.error(String.format("烟台核酸检测结果查询异常cardNo:%s,异常信息:%s", cardNo, e.getMessage())); + log.error(String.format("烟台核酸检测结果查询异常cardNo:%s", cardNo), e); } YtHsjcResDTO resultResult = new YtHsjcResDTO(); resultResult.setData(new ArrayList<>()); @@ -154,7 +154,7 @@ public class YtHsResUtils { //第一层 JSONObject secondData = JSON.parseObject(firstData.getString("data")); Object thirdData = ""; - if (secondData != null && secondData.getJSONArray("data") != null) { + if (secondData != null && secondData.getJSONArray("data") != null && secondData.getJSONArray("data").size() >0 ) { //第二层 data thirdData = secondData.getJSONArray("data").get(0); } @@ -166,7 +166,7 @@ public class YtHsResUtils { log.warn("siWang 调用蓝图接口败"); } } catch (Exception e) { - log.error(String.format("烟台siWang结果查询异常cardNo:%s,异常信息:%s", cardNo, e.getMessage())); + log.error(String.format("烟台siWang结果查询异常cardNo:%s", cardNo), e); } return new YtDataSyncResDTO(); } @@ -221,15 +221,15 @@ public class YtHsResUtils { } } catch (Exception e) { - log.error(String.format("烟台canji结果查询异常cardNo:%s,异常信息:%s", idCard, e.getMessage())); + log.error(String.format("烟台canji结果查询异常cardNo:%s,异常信息:%s", idCard), e); } return failResult; } public static void main(String[] args) { - YtDataSyncResDTO canji = canji("123", "123"); - System.out.println("残疾结果:" + JSON.toJSON(canji)); + //YtDataSyncResDTO canji = canji("123", "123"); + //System.out.println("残疾结果:" + JSON.toJSON(canji)); YtDataSyncResDTO siwang = siWang("1213", "!23");