Browse Source

无车优化,自有车接口返回

Administrator 3 years ago
parent
commit
39798708a0

+ 70 - 0
src/main/java/com/jkcredit/invoice/component/StatisRequestIdTimeComp.java

@@ -0,0 +1,70 @@
+package com.jkcredit.invoice.component;
+
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import java.util.Date;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+@Component
+@Slf4j
+public class StatisRequestIdTimeComp {
+    /**
+     * 关注的请求时间秒
+     */
+    private byte costTimeAtten = 3;
+    private int limitCount = 200;
+    /**
+     * requestId 及 时间
+     */
+    public  Map<String,Long> map = new ConcurrentHashMap<>();
+
+    public static volatile boolean isLimit = false;
+
+
+    ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
+
+    /**
+     * 放入requestid 及时间
+     * @param requestId
+     */
+    public void putReQuestIdAndTime(String requestId){
+        map.put(requestId,new Date().getTime());
+    }
+
+    /**
+     * 放入requestid 及时间
+     * @param requestId
+     */
+    public void removeReQuestIdAndTime(String requestId){
+        map.remove(requestId);
+    }
+    @PostConstruct
+    public void initScan(){
+        log.info("kaishilunxunjishu 啦啦啦");
+        service.scheduleAtFixedRate(new Runnable() {
+            @Override
+            public void run() {
+
+                int count = 0;
+                long currTime = new Date().getTime();
+                for(Map.Entry<String,Long> entry:map.entrySet()){
+                    if(currTime-entry.getValue()>costTimeAtten*1000){
+                        count++;
+                    }
+                }
+                if(count>limitCount){
+                    isLimit = true;
+                }else{
+                    isLimit=false;
+                }
+                log.info("dangqian请求数:"+map.size()+"当前耗时请求数:"+count+"是否限制:"+isLimit);
+            }
+        },1,1, TimeUnit.SECONDS);
+    }
+}

+ 57 - 7
src/main/java/com/jkcredit/invoice/credit/InterfaceCheckServer.java

@@ -1,6 +1,7 @@
 package com.jkcredit.invoice.credit;
 
 import com.jkcredit.invoice.common.DataResult;
+import com.jkcredit.invoice.component.StatisRequestIdTimeComp;
 import com.jkcredit.invoice.credit.custInterface.CustomerInterLowerService;
 import com.jkcredit.invoice.credit.custInterface.NoCarInterService;
 import com.jkcredit.invoice.credit.custInterface.SelfCarInterService;
@@ -19,12 +20,19 @@ public class InterfaceCheckServer {
      SelfCarInterService selfCarInterService;
      @Autowired
      NoCarInterService noCarInterService;
+     @Autowired
+     StatisRequestIdTimeComp statisRequestIdTimeComp;
 
     //跳转到对应的Service服务处理逻辑
     public  DataResult doJumpHandler(String appKey, String api, String data,String requestid ) {
 
         log.info("[-InterfaceCheckServer.doJumpHandler-] request appKey=" + appKey + " ,api=" + api + " ,data=" +data + " ,requestid=" +requestid);
 
+        DataResult resultTimeOut = new DataResult();
+        resultTimeOut.setData(3);
+        resultTimeOut.setCode(200);
+        resultTimeOut.setRequestid(requestid);
+        resultTimeOut.setMsg("网络异常");
 
         DataResult result;
         switch (api){
@@ -56,13 +64,31 @@ public class InterfaceCheckServer {
                 result = selfCarInterService.customerETCRecValid(appKey,api,data,requestid);
                 break;
             case "CARD_TRADE_V1"://自有车 交易查询接口 渠道通过此接口可以查询单张卡连续90天内的交易(待开票、开票中、已开票)
-                result = selfCarInterService.getTradeList(appKey,api,data,requestid);
+                if(StatisRequestIdTimeComp.isLimit){
+                    result = resultTimeOut;
+                }else{
+                    statisRequestIdTimeComp.putReQuestIdAndTime(requestid);
+                    result = selfCarInterService.getTradeList(appKey,api,data,requestid);
+                    statisRequestIdTimeComp.removeReQuestIdAndTime(requestid);
+                }
                 break;
             case "B2B_INVOICE_APPLY_V1"://自有车 申请开票接口 渠道通过此接口可以对该公司绑定的单张卡连续90天内的交易进行开票。
-                result = selfCarInterService.applInvoice(appKey,api,data,requestid);
+                if(StatisRequestIdTimeComp.isLimit){
+                    result = resultTimeOut;
+                }else{
+                    statisRequestIdTimeComp.putReQuestIdAndTime(requestid);
+                    result = selfCarInterService.applInvoice(appKey,api,data,requestid);
+                    statisRequestIdTimeComp.removeReQuestIdAndTime(requestid);
+                }
                 break;
             case "B2B_INVOICE_QUERY_V1"://自有车 已开发票查询接口 渠道通过此接口可以根据该公司绑定的单张卡查询此卡在某个月开具的发票。
-                result = selfCarInterService.getSelfCarInvoicesByTime(appKey,api,data,requestid);
+                if(StatisRequestIdTimeComp.isLimit){
+                    result = resultTimeOut;
+                }else{
+                    statisRequestIdTimeComp.putReQuestIdAndTime(requestid);
+                    result = selfCarInterService.getSelfCarInvoicesByTime(appKey,api,data,requestid);
+                    statisRequestIdTimeComp.removeReQuestIdAndTime(requestid);
+                }
                 break;
             case "B2B_INVOICE_PACKAGE_V1"://自有车 发票下载 渠道通过此接口可以下载某公司某个月份开具的发票。
                 result = selfCarInterService.getSelfCarInvoicePackage(appKey,api,data,requestid);
@@ -82,16 +108,40 @@ public class InterfaceCheckServer {
                 result = noCarInterService.customeRecUpperQuery(appKey,api,data,requestid);
                 break;
             case "WAY_BILL_START"://无车 实时运单开始指令
-                result = noCarInterService.noCarBillStart(appKey,api,data,requestid);
+                if(StatisRequestIdTimeComp.isLimit){
+                    result = resultTimeOut;
+                }else{
+                    statisRequestIdTimeComp.putReQuestIdAndTime(requestid);
+                    result = noCarInterService.noCarBillStart(appKey,api,data,requestid);
+                    statisRequestIdTimeComp.removeReQuestIdAndTime(requestid);
+                }
                 break;
             case "WAY_BILL_END"://无车 实时运单结束指令
-                result = noCarInterService.noCarBillEnd(appKey,api,data,requestid);
+                if(StatisRequestIdTimeComp.isLimit){
+                    result = resultTimeOut;
+                }else{
+                    statisRequestIdTimeComp.putReQuestIdAndTime(requestid);
+                    result = noCarInterService.noCarBillEnd(appKey,api,data,requestid);
+                    statisRequestIdTimeComp.removeReQuestIdAndTime(requestid);
+                }
                 break;
             case "WAY_BILL_HISTORY_START"://无车 历史运单开始指令
-                result = noCarInterService.noCarHisWaybillStart(appKey,api,data,requestid);
+                if(StatisRequestIdTimeComp.isLimit){
+                    result = resultTimeOut;
+                }else{
+                    statisRequestIdTimeComp.putReQuestIdAndTime(requestid);
+                    result = noCarInterService.noCarHisWaybillStart(appKey,api,data,requestid);
+                    statisRequestIdTimeComp.removeReQuestIdAndTime(requestid);
+                }
                 break;
             case "WAY_BILL_HISTORY_END"://无车 历史运单结束指令
-                result = noCarInterService.noCarHisWaybillEnd(appKey,api,data,requestid);
+                if(StatisRequestIdTimeComp.isLimit){
+                    result = resultTimeOut;
+                }else{
+                    statisRequestIdTimeComp.putReQuestIdAndTime(requestid);
+                    result = noCarInterService.noCarHisWaybillEnd(appKey,api,data,requestid);
+                    statisRequestIdTimeComp.removeReQuestIdAndTime(requestid);
+                }
                 break;
             case "WAY_BILL_NUM_FIND_INVOICE"://无车  运单号查询发票数据
                 result = noCarInterService.noCarVoiceQuery(appKey,api,data,requestid);

+ 5 - 1
src/main/resources/application.yml

@@ -7,7 +7,11 @@ server:
       directory: /home/app/logs/tomcat   #日志存储目录
       pattern: '%t %a %A %m %U%q %s %D %I %B'  #日志格式
       prefix: access        #日志文件前缀
-      rename-on-rotate: true     #是否启用日志轮转
+      rename-on-rotate: true #是否启用日志轮转
+    max-threads: 800 # 最大线程数
+    max-connections: 20000 # 最大链接数
+    min-spare-threads: 100 # 最小空闲线程数
+    accept-count: 200
 #  undertow:
 #    # 指定工作者线程的 I/0 线程数,默认为 2 或者 CPU 的个数
 #    io-threads: 2