From 400795021ce677daf900e6c2818998222212f8a3 Mon Sep 17 00:00:00 2001
From: java110 <928255095@qq.com>
Date: 星期一, 30 一月 2023 18:31:46 +0800
Subject: [PATCH] modify url config

---
 springboot/src/main/java/com/java110/boot/smo/impl/ApiServiceSMOImpl.java |  233 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 223 insertions(+), 10 deletions(-)

diff --git a/springboot/src/main/java/com/java110/boot/smo/impl/ApiServiceSMOImpl.java b/springboot/src/main/java/com/java110/boot/smo/impl/ApiServiceSMOImpl.java
index e5736cd..030548a 100644
--- a/springboot/src/main/java/com/java110/boot/smo/impl/ApiServiceSMOImpl.java
+++ b/springboot/src/main/java/com/java110/boot/smo/impl/ApiServiceSMOImpl.java
@@ -2,6 +2,7 @@
 
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.aliyuncs.utils.StringUtils;
 import com.java110.boot.smo.IApiServiceSMO;
 import com.java110.core.client.RestTemplate;
 import com.java110.core.context.ApiDataFlow;
@@ -13,25 +14,30 @@
 import com.java110.core.log.LoggerFactory;
 import com.java110.core.smo.ISaveTransactionLogSMO;
 import com.java110.core.trace.Java110TraceLog;
+import com.java110.dto.order.OrderDto;
 import com.java110.entity.center.AppRoute;
 import com.java110.entity.center.AppService;
 import com.java110.entity.center.DataFlowLinksCost;
 import com.java110.po.transactionLog.TransactionLogPo;
 import com.java110.utils.cache.AppRouteCache;
 import com.java110.utils.cache.MappingCache;
-import com.java110.utils.constant.*;
+import com.java110.utils.constant.CommonConstant;
+import com.java110.utils.constant.KafkaConstant;
+import com.java110.utils.constant.MappingConstant;
+import com.java110.utils.constant.ResponseConstant;
 import com.java110.utils.exception.*;
 import com.java110.utils.kafka.KafkaFactory;
 import com.java110.utils.log.LoggerEngine;
+import com.java110.utils.util.BootReplaceUtil;
 import com.java110.utils.util.DateUtil;
 import com.java110.utils.util.StringUtil;
 import com.java110.vo.ResultVo;
 import org.apache.commons.lang3.math.NumberUtils;
 import org.slf4j.Logger;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.ResponseEntity;
+import org.springframework.http.*;
 import org.springframework.stereotype.Service;
+import org.springframework.web.client.HttpStatusCodeException;
 
 import java.util.Date;
 import java.util.List;
@@ -148,7 +154,7 @@
 
         String serviceCode = dataFlow.getRequestHeaders().get(CommonConstant.HTTP_SERVICE);
 
-        String logServiceCode = MappingCache.getValue(MappingCache.LOG_SERVICE_CODE);
+        String logServiceCode = MappingCache.getValue(MappingConstant.DOMAIN_SYSTEM_SWITCH,MappingCache.LOG_SERVICE_CODE);
 
         //鏃ュ織鏌ヨ涓嶈褰�
         if ("/transactionLog/queryTransactionLog".equals(serviceCode)
@@ -156,7 +162,7 @@
                 || "file.getFile".equals(serviceCode)
                 || "file.getFileByObjId".equals(serviceCode)
                 || "/machine/heartbeat".equals(serviceCode) // 蹇冭烦涔熶笉璁板綍
-        ) {
+                ) {
             return;
         }
 
@@ -377,7 +383,9 @@
                 throw new ListenerExecuteException(ResponseConstant.RESULT_CODE_ERROR,
                         "鏈嶅姟銆�" + appService.getServiceCode() + "銆戣皟鐢ㄦ柟寮忎笉瀵硅妫�鏌�,褰撳墠璇锋眰鏂瑰紡涓猴細" + httpMethod);
             }
-            dataFlow.setApiCurrentService(ServiceCodeConstant.SERVICE_CODE_DO_SERVICE_TRANSFER);
+            //dataFlow.setApiCurrentService(ServiceCodeConstant.SERVICE_CODE_DO_SERVICE_TRANSFER);
+            doNT(appService, dataFlow, dataFlow.getReqJson());
+            return;
         } else if ("T".equals(appService.getIsInstance())) {
             //濡傛灉鏄�忎紶绫� 璇锋眰鏂瑰紡蹇呴』涓庢帴鍙f彁渚涙柟璋冪敤鏂瑰紡涓�鑷�
             String httpMethod = dataFlow.getRequestCurrentHeaders().get(CommonConstant.HTTP_METHOD);
@@ -385,7 +393,8 @@
                 throw new ListenerExecuteException(ResponseConstant.RESULT_CODE_ERROR,
                         "鏈嶅姟銆�" + appService.getServiceCode() + "銆戣皟鐢ㄦ柟寮忎笉瀵硅妫�鏌�,褰撳墠璇锋眰鏂瑰紡涓猴細" + httpMethod);
             }
-            dataFlow.setApiCurrentService(ServiceCodeConstant.SERVICE_CODE_SYSTEM_TRANSFER);
+            doTransfer(appService, dataFlow, dataFlow.getReqJson());
+            return;
         } else if ("CMD".equals(appService.getIsInstance())) {
             //濡傛灉鏄�忎紶绫� 璇锋眰鏂瑰紡蹇呴』涓庢帴鍙f彁渚涙柟璋冪敤鏂瑰紡涓�鑷�
             String httpMethod = dataFlow.getRequestCurrentHeaders().get(CommonConstant.HTTP_METHOD);
@@ -393,12 +402,216 @@
                 throw new ListenerExecuteException(ResponseConstant.RESULT_CODE_ERROR,
                         "鏈嶅姟銆�" + appService.getServiceCode() + "銆戣皟鐢ㄦ柟寮忎笉瀵硅妫�鏌�,褰撳墠璇锋眰鏂瑰紡涓猴細" + httpMethod);
             }
-            dataFlow.setApiCurrentService(ServiceCodeConstant.SERVICE_CODE_SYSTEM_CMD);
+            dealCmd(appService, dataFlow, dataFlow.getReqJson());
+            return;
         } else {
             dataFlow.setApiCurrentService(dataFlow.getRequestHeaders().get(CommonConstant.HTTP_SERVICE));
         }
         ServiceDataFlowEventPublishing.multicastEvent(dataFlow, appService);
         DataFlowFactory.addCostTime(dataFlow, "invokeBusinessSystem", "璋冪敤涓嬫父绯荤粺鑰楁椂", startDate);
+    }
+
+    private void doNT(AppService service, ApiDataFlow dataFlow, JSONObject reqJson) {
+
+
+        HttpHeaders header = new HttpHeaders();
+        for (String key : dataFlow.getRequestCurrentHeaders().keySet()) {
+            header.add(key, dataFlow.getRequestCurrentHeaders().get(key));
+        }
+        HttpEntity<String> httpEntity = new HttpEntity<String>(reqJson.toJSONString(), header);
+        //http://user-service/test/sayHello
+
+        ResponseEntity responseEntity = null;
+        //閰嶇疆c_service 鏃惰娉ㄦ剰 濡傛灉鏄互out 寮�澶寸殑璋冪敤澶栭儴鐨勫湴鍧�
+
+        try {
+            if (CommonConstant.HTTP_METHOD_GET.equals(service.getMethod())) {
+                String requestUrl = dataFlow.getRequestHeaders().get("REQUEST_URL");
+                if (!StringUtil.isNullOrNone(requestUrl)) {
+                    String param = requestUrl.contains("?") ? requestUrl.substring(requestUrl.indexOf("?") + 1, requestUrl.length()) : "";
+                    if (service.getUrl().contains("?")) {
+                        requestUrl = service.getUrl() + "&" + param;
+                    } else {
+                        requestUrl = service.getUrl() + "?" + param;
+                    }
+                }
+
+                requestUrl = BootReplaceUtil.replaceServiceName(requestUrl);
+
+                responseEntity = outRestTemplate.exchange(requestUrl, HttpMethod.GET, httpEntity, String.class);
+            } else if (CommonConstant.HTTP_METHOD_PUT.equals(service.getMethod())) {
+                responseEntity = outRestTemplate.exchange(service.getUrl(), HttpMethod.PUT, httpEntity, String.class);
+            } else if (CommonConstant.HTTP_METHOD_DELETE.equals(service.getMethod())) {
+                String requestUrl = dataFlow.getRequestHeaders().get("REQUEST_URL");
+                if (!StringUtil.isNullOrNone(requestUrl)) {
+                    String param = requestUrl.contains("?") ? requestUrl.substring(requestUrl.indexOf("?"), requestUrl.length()) : "";
+                    if (service.getUrl().contains("?")) {
+                        requestUrl = service.getUrl() + "&" + param;
+                    } else {
+                        requestUrl = service.getUrl() + "?" + param;
+                    }
+                }
+                responseEntity = outRestTemplate.exchange(requestUrl, HttpMethod.DELETE, httpEntity, String.class);
+            } else {
+
+                String requestUrl = BootReplaceUtil.replaceServiceName(service.getUrl());
+                responseEntity = outRestTemplate.exchange(requestUrl, HttpMethod.POST, httpEntity, String.class);
+            }
+        } catch (HttpStatusCodeException e) { //杩欓噷spring 妗嗘灦 鍦�4XX 鎴� 5XX 鏃舵姏鍑� HttpServerErrorException 寮傚父锛岄渶瑕侀噸鏂板皝瑁呬竴涓�
+            responseEntity = new ResponseEntity<String>(e.getResponseBodyAsString(), e.getStatusCode());
+        }
+
+        logger.debug("API 鏈嶅姟璋冪敤涓嬫父鏈嶅姟璇锋眰锛歿}锛岃繑鍥炰负锛歿}", httpEntity, responseEntity);
+
+        dataFlow.setResponseEntity(responseEntity);
+    }
+
+    private void doTransfer(AppService appService, ApiDataFlow dataFlow, JSONObject reqJson) {
+
+        Map<String, String> reqHeader = dataFlow.getRequestCurrentHeaders();
+        HttpHeaders header = new HttpHeaders();
+        for (String key : dataFlow.getRequestCurrentHeaders().keySet()) {
+            header.add(key, reqHeader.get(key));
+        }
+        HttpEntity<String> httpEntity = new HttpEntity<String>(reqJson.toJSONString(), header);
+        String orgRequestUrl = dataFlow.getRequestHeaders().get("REQUEST_URL");
+
+        //String serviceCode = "/" + reqHeader.get(CommonConstant.HTTP_RESOURCE) + "/" + reqHeader.get(CommonConstant.HTTP_ACTION);
+        String serviceCode = appService.getServiceCode();
+        serviceCode = serviceCode.startsWith("/") ? serviceCode : ("/" + serviceCode);
+
+        String requestUrl = "http://127.0.0.1:8008" + serviceCode;
+
+        ResponseEntity responseEntity = null;
+        if (!StringUtil.isNullOrNone(orgRequestUrl)) {
+            String param = orgRequestUrl.contains("?") ? orgRequestUrl.substring(orgRequestUrl.indexOf("?") + 1, orgRequestUrl.length()) : "";
+            requestUrl += ("?" + param);
+        }
+        try {
+            if (CommonConstant.HTTP_METHOD_GET.equals(appService.getMethod())) {
+                responseEntity = outRestTemplate.exchange(requestUrl, HttpMethod.GET, httpEntity, String.class);
+            } else if (CommonConstant.HTTP_METHOD_PUT.equals(appService.getMethod())) {
+                responseEntity = outRestTemplate.exchange(requestUrl, HttpMethod.PUT, httpEntity, String.class);
+            } else if (CommonConstant.HTTP_METHOD_DELETE.equals(appService.getMethod())) {
+                responseEntity = outRestTemplate.exchange(requestUrl, HttpMethod.DELETE, httpEntity, String.class);
+            } else {
+                responseEntity = outRestTemplate.exchange(requestUrl, HttpMethod.POST, httpEntity, String.class);
+            }
+            HttpHeaders headers = responseEntity.getHeaders();
+            String oId = "-1";
+            if (headers.containsKey(OrderDto.O_ID)) {
+                oId = headers.get(OrderDto.O_ID).get(0);
+            }
+
+            //杩涘叆databus
+            if (!CommonConstant.HTTP_METHOD_GET.equals(appService.getMethod())) {
+
+                // dealDatabus(serviceCode, reqJson, oId);
+            }
+
+
+        } catch (HttpStatusCodeException e) { //杩欓噷spring 妗嗘灦 鍦�4XX 鎴� 5XX 鏃舵姏鍑� HttpServerErrorException 寮傚父锛岄渶瑕侀噸鏂板皝瑁呬竴涓�
+            logger.error("璇锋眰涓嬫父鏈嶅姟銆�" + requestUrl + "銆戝紓甯革紝鍙傛暟涓�" + httpEntity + e.getResponseBodyAsString(), e);
+            String body = e.getResponseBodyAsString();
+
+            if (StringUtil.isJsonObject(body)) {
+                JSONObject bodyObj = JSONObject.parseObject(body);
+                if (bodyObj.containsKey("message") && !StringUtil.isEmpty(bodyObj.getString("message"))) {
+                    body = bodyObj.getString("message");
+                }
+            }
+            responseEntity = new ResponseEntity<String>(body, e.getStatusCode());
+        }
+
+        logger.debug("API 鏈嶅姟璋冪敤涓嬫父鏈嶅姟璇锋眰锛歿}锛岃繑鍥炰负锛歿}", httpEntity, responseEntity);
+
+        if (responseEntity.getStatusCode() != HttpStatus.OK) {
+            responseEntity = ResultVo.createResponseEntity(ResultVo.CODE_ERROR, String.valueOf(responseEntity.getBody()));
+            dataFlow.setResponseEntity(responseEntity);
+
+            return;
+        }
+        if (StringUtils.isEmpty(responseEntity.getBody() + "")) {
+            responseEntity = ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "澶勭悊澶辫触");
+            dataFlow.setResponseEntity(responseEntity);
+            return;
+        }
+        JSONObject resParam = JSONObject.parseObject(responseEntity.getBody() + "");
+        if (resParam.containsKey("code") && resParam.containsKey("msg")) { // 璇存槑寰湇鍔¤繑鍥炵殑鏄� resultVo 瀵硅薄鐩存帴杩斿洖灏卞彲浠�
+            dataFlow.setResponseEntity(responseEntity);
+            return;
+        }
+        responseEntity = ResultVo.createResponseEntity(resParam);
+        dataFlow.setResponseEntity(responseEntity);
+    }
+
+    private void dealCmd(AppService appService, ApiDataFlow dataFlow, JSONObject reqJson) {
+        Map<String, String> reqHeader = dataFlow.getRequestCurrentHeaders();
+        HttpHeaders header = new HttpHeaders();
+        for (String key : dataFlow.getRequestCurrentHeaders().keySet()) {
+            header.add(key, reqHeader.get(key));
+        }
+        if (reqHeader.containsKey(CommonConstant.USER_ID)
+                && (!reqJson.containsKey("userId") || StringUtil.isEmpty(reqJson.getString("userId")))) {
+            reqJson.put("userId", reqHeader.get(CommonConstant.USER_ID));
+        }
+        if (reqHeader.containsKey(CommonConstant.USER_ID)
+                && (!reqJson.containsKey("loginUserId") || StringUtil.isEmpty(reqJson.getString("loginUserId")))) {
+            reqJson.put("loginUserId", reqHeader.get(CommonConstant.LOGIN_U_ID));
+        }
+        if (reqHeader.containsKey(CommonConstant.STORE_ID)
+                && (!reqJson.containsKey("storeId") || StringUtil.isEmpty(reqJson.getString("storeId")))) {
+            reqJson.put("storeId", reqHeader.get(CommonConstant.STORE_ID));
+        }
+        HttpEntity<String> httpEntity = new HttpEntity<String>(reqJson.toJSONString(), header);
+        String orgRequestUrl = dataFlow.getRequestHeaders().get("REQUEST_URL");
+
+        String serviceCode = appService.getServiceCode();
+
+        serviceCode = serviceCode.startsWith("/") ? serviceCode : ("/" + serviceCode);
+
+        String requestUrl = "http://127.0.0.1:8008/cmd" + serviceCode;
+        //
+        ResponseEntity responseEntity = null;
+        if (!StringUtil.isNullOrNone(orgRequestUrl)) {
+            String param = orgRequestUrl.contains("?") ? orgRequestUrl.substring(orgRequestUrl.indexOf("?") + 1, orgRequestUrl.length()) : "";
+            requestUrl += ("?" + param);
+        }
+        try {
+            responseEntity = outRestTemplate.exchange(requestUrl, HttpMethod.POST, httpEntity, String.class);
+            HttpHeaders headers = responseEntity.getHeaders();
+            String oId = "-1";
+            if (headers.containsKey(OrderDto.O_ID)) {
+                oId = headers.get(OrderDto.O_ID).get(0);
+            }
+
+        } catch (HttpStatusCodeException e) { //杩欓噷spring 妗嗘灦 鍦�4XX 鎴� 5XX 鏃舵姏鍑� HttpServerErrorException 寮傚父锛岄渶瑕侀噸鏂板皝瑁呬竴涓�
+            logger.error("璇锋眰涓嬫父鏈嶅姟銆�" + requestUrl + "銆戝紓甯革紝鍙傛暟涓�" + httpEntity + e.getResponseBodyAsString(), e);
+            String body = e.getResponseBodyAsString();
+
+            if (StringUtil.isJsonObject(body)) {
+                JSONObject bodyObj = JSONObject.parseObject(body);
+                if (bodyObj.containsKey("message") && !StringUtil.isEmpty(bodyObj.getString("message"))) {
+                    body = bodyObj.getString("message");
+                }
+            }
+            responseEntity = new ResponseEntity<String>(body, e.getStatusCode());
+        }
+
+        logger.debug("API 鏈嶅姟璋冪敤涓嬫父鏈嶅姟璇锋眰锛歿}锛岃繑鍥炰负锛歿}", httpEntity, responseEntity);
+
+        if (responseEntity.getStatusCode() != HttpStatus.OK) {
+            responseEntity = ResultVo.createResponseEntity(ResultVo.CODE_ERROR, String.valueOf(responseEntity.getBody()));
+            dataFlow.setResponseEntity(responseEntity);
+
+            return;
+        }
+        if (StringUtils.isEmpty(responseEntity.getBody() + "")) {
+            responseEntity = ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "澶勭悊澶辫触");
+            dataFlow.setResponseEntity(responseEntity);
+            return;
+        }
+        dataFlow.setResponseEntity(responseEntity);
     }
 
 
@@ -410,7 +623,7 @@
     private void saveLogMessage(String requestJson, String responseJson) {
 
         try {
-            if (MappingConstant.VALUE_ON.equals(MappingCache.getValue(MappingConstant.KEY_LOG_ON_OFF))) {
+            if (MappingConstant.VALUE_ON.equals(MappingCache.getValue(MappingConstant.DOMAIN_SYSTEM_SWITCH,MappingConstant.KEY_LOG_ON_OFF))) {
                 JSONObject log = new JSONObject();
                 log.put("request", requestJson);
                 log.put("response", responseJson);
@@ -428,7 +641,7 @@
      */
     private void saveCostTimeLogMessage(DataFlow dataFlow) {
         try {
-            if (MappingConstant.VALUE_ON.equals(MappingCache.getValue(MappingConstant.KEY_COST_TIME_ON_OFF))) {
+            if (MappingConstant.VALUE_ON.equals(MappingCache.getValue(MappingConstant.DOMAIN_SYSTEM_SWITCH,MappingConstant.KEY_COST_TIME_ON_OFF))) {
                 List<DataFlowLinksCost> dataFlowLinksCosts = dataFlow.getLinksCostDates();
                 JSONObject costDate = new JSONObject();
                 JSONArray costDates = new JSONArray();

--
Gitblit v1.8.0