1. 程式人生 > >for迴圈中包含跨服務查詢優化

for迴圈中包含跨服務查詢優化

 

一,業務場景:匯出功能,資料量及耗時

1萬條:1.8min

2萬條:4.4min,大小為5.36M

3萬條:前端直接報錯,504 GetWay Time out

二,原因:java程式碼的For迴圈中包含一個跨服務查詢,執行了3萬次跨服務查詢導致

三,嘗試方式:

1,修改配置檔案:因為跨服務呼叫,inf服務呼叫auth服務,以為超時熔斷,延長了熔斷時間,由6min,改為12min。無效!

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 720000

2,修改配置檔案:springCloud,匯入檔案大小預設為1M,之前出現過檔案大小超過1M,導致匯入超時。類比推理,匯出超時是否此原因導致,修改了預設檔案大小,由1M-10M,再改為20M。其實3萬條資料達不到10M,無效!

  http:
    multipart:
      enabled: true
      max-file-size: 20MB
      max-request-size: 20MB
  tomcat: max-active:30 max-wait:1000 max-idle:20 remove-abandoned-timeout:180

3,優化程式碼,把跨服務查詢抽離到For迴圈外,總耗時2.min。有效

優化前: 

for (TransferTransactionVO item : list) {
            
            // 優化前
            Map<String, Object> dictMap = new HashMap<String, Object>();
            dictMap.put("dictValue", item.getProcessResult());
            dictMap.put("factoryAreaId", Long.valueOf(request.getFactoryAreaId()));
            dictMap.put("code", DictTypeE.WMS_INF_TYPE.getTypeCode());
            String processResultName = hystrixWrappedAuthServiceClient.findValueToKey(dictMap);
            
            Map<String, Object> modelMap = new ModelMap();
            modelMap.put("processResult", processResultName);
            
            modelMap.put("processDescription", item.getProcessMessage());
            String format = "";
            if (item.getProcessTime() != null) {
                format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(item.getProcessTime());
            }
            modelMap.put("processTime", format);
            modelMap.put("fileName", item.getFileName());
            modelMap.put("orderNo", item.getAufnr());
            modelMap.put("movementType", item.getBwlvs());
            modelMap.put("partNo", item.getMatnr());
            modelMap.put("qty", item.getMenge());
            modelMap.put("plant", item.getWerks1());
            modelMap.put("sourceStorage", item.getLgort1());
            modelMap.put("desStorage", item.getLgort2());
            modelMap.put("transferDate", item.getCpudt());
            modelMap.put("transferTime", item.getCputm());

            transferTransactionMaps.add(modelMap);
        }

 優化後:

  String processResult = DictTypeE.WMS_INF_TYPE.getTypeCode();
        Map<String, Object> map = new HashMap<>();
        map.put("factoryAreaId", factoryAreaId);
        map.put("orgId", orgId);
        map.put("code", processResult);
        List<Map<String, Object>> processResultMap = hystrixWrappedAuthServiceClient.findByCode(map);
     for (TransferTransactionVO item : list) {
            
//            Map<String, Object> dictMap = new HashMap<String, Object>();
//            dictMap.put("dictValue", item.getProcessResult());
//            dictMap.put("factoryAreaId", Long.valueOf(request.getFactoryAreaId()));
//            dictMap.put("code", DictTypeE.WMS_INF_TYPE.getTypeCode());
//            String processResultName = hystrixWrappedAuthServiceClient.findValueToKey(dictMap);
            
            //優化後
            Map<String, Object> modelMap = new ModelMap();
            for (Map<String, Object> m : processResultMap) {
                String dictKey = m.get("dictKey").toString();
                Integer dictValue = Integer.parseInt(m.get("dictValue").toString());
                if(dictValue.equals(item.getProcessResult())){
                    modelMap.put("processResult", dictKey);
                    break;
                }
            }
            // modelMap.put("processResult", processResultName);
            
            modelMap.put("processDescription", item.getProcessMessage());
            String format = "";
            if (item.getProcessTime() != null) {
                format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(item.getProcessTime());
            }
            modelMap.put("processTime", format);
            modelMap.put("fileName", item.getFileName());
            modelMap.put("orderNo", item.getAufnr());
            modelMap.put("movementType", item.getBwlvs());
            modelMap.put("partNo", item.getMatnr());
            modelMap.put("qty", item.getMenge());
            modelMap.put("plant", item.getWerks1());
            modelMap.put("sourceStorage", item.getLgort1());
            modelMap.put("desStorage", item.getLgort2());
            modelMap.put("transferDate", item.getCpudt());
            modelMap.put("transferTime", item.getCputm());

            transferTransactionMaps.add(modelMap);
        }