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);
        }

猜你喜欢

转载自blog.csdn.net/C18298182575/article/details/85537100