From 8d62257c26dcba1f62e396e6aae30f164ec02b3b Mon Sep 17 00:00:00 2001
From: java110 <928255095@qq.com>
Date: 星期日, 20 二月 2022 16:04:46 +0800
Subject: [PATCH] 加入trace 测试
---
service-api/src/main/java/com/java110/api/smo/impl/ApiServiceSMOImpl.java | 116 +++++++++++++++++++++++++++++++++++++++++++++++++--------
1 files changed, 99 insertions(+), 17 deletions(-)
diff --git a/service-api/src/main/java/com/java110/api/smo/impl/ApiServiceSMOImpl.java b/service-api/src/main/java/com/java110/api/smo/impl/ApiServiceSMOImpl.java
old mode 100644
new mode 100755
index 8d79d84..eef207a
--- a/service-api/src/main/java/com/java110/api/smo/impl/ApiServiceSMOImpl.java
+++ b/service-api/src/main/java/com/java110/api/smo/impl/ApiServiceSMOImpl.java
@@ -10,9 +10,13 @@
import com.java110.core.factory.AuthenticationFactory;
import com.java110.core.factory.DataFlowFactory;
import com.java110.core.factory.GenerateCodeFactory;
+import com.java110.core.log.LoggerFactory;
+import com.java110.core.smo.ISaveTransactionLogSMO;
+import com.java110.core.trace.Java110TraceLog;
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.*;
@@ -21,9 +25,9 @@
import com.java110.utils.log.LoggerEngine;
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.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@@ -50,6 +54,9 @@
@Autowired
private RestTemplate outRestTemplate;
+ @Autowired
+ private ISaveTransactionLogSMO saveTransactionLogSMOImpl;
+
/**
* 鏈嶅姟璋冨害
@@ -60,11 +67,12 @@
* @throws SMOException
*/
@Override
+ @Java110TraceLog
public ResponseEntity<String> service(String reqJson, Map<String, String> headers) throws SMOException {
ApiDataFlow dataFlow = null;
- //JSONObject responseJson = null;
+ Date startDate = DateUtil.getCurrentDate();
ResponseEntity<String> responseEntity = null;
@@ -91,27 +99,21 @@
responseEntity = dataFlow.getResponseEntity();
} catch (DecryptException e) { //瑙e瘑寮傚父
- responseEntity = new ResponseEntity<String>("瑙e瘑寮傚父锛�" + e.getMessage(), HttpStatus.NON_AUTHORITATIVE_INFORMATION);
+ responseEntity = ResultVo.error("瑙e瘑寮傚父锛�" + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
} catch (BusinessException e) {
- responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.BAD_REQUEST);
+ responseEntity = ResultVo.error(e.getMessage(), HttpStatus.BAD_REQUEST);
} catch (NoAuthorityException e) {
- responseEntity = new ResponseEntity<String>("閴存潈澶辫触锛�" + e.getMessage(), HttpStatus.UNAUTHORIZED);
+ responseEntity = ResultVo.error("閴存潈澶辫触锛�" + e.getMessage(), HttpStatus.UNAUTHORIZED);
} catch (InitConfigDataException e) {
- responseEntity = new ResponseEntity<String>("鍒濆鍖栧け璐ワ細" + e.getMessage(), HttpStatus.BAD_REQUEST);
+ responseEntity = ResultVo.error("鍒濆鍖栧け璐ワ細" + e.getMessage(), HttpStatus.BAD_REQUEST);
} catch (Exception e) {
logger.error("鍐呴儴寮傚父锛�", e);
- responseEntity = new ResponseEntity<String>("鍐呴儴寮傚父锛�" + e.getMessage() + e.getLocalizedMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
-
+ responseEntity = ResultVo.error("鍐呴儴寮傚父锛�" + e.getMessage() + e.getLocalizedMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
} finally {
+ Date endDate = DateUtil.getCurrentDate();
if (dataFlow != null) {
//杩欓噷璁板綍鏃ュ織
- Date endDate = DateUtil.getCurrentDate();
-
dataFlow.setEndDate(endDate);
- //娣诲姞鑰楁椂
- //DataFlowFactory.addCostTime(dataFlow, "service", "涓氬姟澶勭悊鎬昏�楁椂", dataFlow.getStartDate(), dataFlow.getEndDate());
- //淇濆瓨鑰楁椂
- //saveCostTimeLogMessage(dataFlow);
//澶勭悊杩斿洖鎶ユ枃閴存潈
//AuthenticationFactory.putSign(dataFlow);
}
@@ -119,11 +121,83 @@
//resJson = encrypt(responseJson.toJSONString(),headers);
responseEntity = new ResponseEntity<String>(resJson, HttpStatus.OK);
}
- //杩欓噷淇濆瓨鑰楁椂锛屼互鍙婃棩蹇�
- return responseEntity;
+ //娣诲姞鑰楁椂
+ saveLog(dataFlow, startDate, endDate, reqJson, responseEntity);
+ //杩欓噷淇濆瓨鑰楁椂锛屼互鍙婃棩蹇�
+ }
+ return responseEntity;
+ }
+
+ /**
+ * 鏃ュ織璁板綍
+ *
+ * @param dataFlow
+ * @param startDate
+ * @param endDate
+ */
+ private void saveLog(ApiDataFlow dataFlow, Date startDate, Date endDate, String reqJson, ResponseEntity<String> responseEntity) {
+
+ if (dataFlow == null) {
+ return;
}
+ String serviceCode = dataFlow.getRequestHeaders().get(CommonConstant.HTTP_SERVICE);
+
+ String logServiceCode = MappingCache.getValue(MappingCache.LOG_SERVICE_CODE);
+
+ //鏃ュ織鏌ヨ涓嶈褰�
+ if ("/transactionLog/queryTransactionLog".equals(serviceCode)
+ || "/transactionLog/queryTransactionLogMessage".equals(serviceCode)
+ || "file.getFile".equals(serviceCode)
+ || "file.getFileByObjId".equals(serviceCode)
+ || "/machine/heartbeat".equals(serviceCode) // 蹇冭烦涔熶笉璁板綍
+ ) {
+ return;
+ }
+
+ if (StringUtil.isEmpty(logServiceCode)) {
+ return;
+ }
+ if (logServiceCode.contains("|")) {
+ String[] logServiceCodes = logServiceCode.split("|");
+
+ for (String lServiceCode : logServiceCodes) {
+ if (serviceCode.equals(lServiceCode.trim())) {
+ doSaveLog(dataFlow, startDate, endDate, serviceCode, reqJson, responseEntity);
+ return;
+ }
+ }
+ }
+
+ if ("all".equals(logServiceCode.trim().toLowerCase())) {
+ doSaveLog(dataFlow, startDate, endDate, serviceCode, reqJson, responseEntity);
+ return;
+ }
+
+ if (serviceCode.equals(logServiceCode.trim())) {
+ doSaveLog(dataFlow, startDate, endDate, serviceCode, reqJson, responseEntity);
+ }
+ }
+
+
+ private void doSaveLog(ApiDataFlow dataFlow, Date startDate, Date endDate, String serviceCode, String reqJson, ResponseEntity<String> responseEntity) {
+
+ TransactionLogPo transactionLogPo = new TransactionLogPo();
+ transactionLogPo.setAppId(dataFlow.getAppId());
+ transactionLogPo.setCostTime((endDate.getTime() - startDate.getTime()) + "");
+ transactionLogPo.setIp("");
+ transactionLogPo.setServiceCode(serviceCode);
+ transactionLogPo.setSrcIp(dataFlow.getRequestHeaders().get(CommonConstant.HTTP_SRC_IP));
+ transactionLogPo.setState(responseEntity.getStatusCode() != HttpStatus.OK ? "F" : "S");
+ transactionLogPo.setTimestamp(dataFlow.getRequestTime());
+ transactionLogPo.setUserId(dataFlow.getUserId());
+ transactionLogPo.setTransactionId(dataFlow.getTransactionId());
+ transactionLogPo.setRequestHeader(dataFlow.getRequestHeaders() != null ? dataFlow.getRequestHeaders().toString() : "");
+ transactionLogPo.setResponseHeader(responseEntity.getHeaders().toSingleValueMap().toString());
+ transactionLogPo.setRequestMessage(reqJson);
+ transactionLogPo.setResponseMessage(responseEntity.getBody());
+ saveTransactionLogSMOImpl.saveLog(transactionLogPo);
}
@@ -235,7 +309,7 @@
if (!StringUtil.isNullOrNone(dataFlow.getAppRoutes().get(0).getSecurityCode())) {
String sign = AuthenticationFactory.apiDataFlowMd5(dataFlow);
if (!sign.equals(dataFlow.getReqSign().toLowerCase())) {
- throw new NoAuthorityException(ResponseConstant.RESULT_CODE_NO_AUTHORITY_ERROR, "绛惧悕澶辫触" + sign);
+ throw new NoAuthorityException(ResponseConstant.RESULT_CODE_NO_AUTHORITY_ERROR, "绛惧悕澶辫触");
}
}
@@ -308,6 +382,14 @@
"鏈嶅姟銆�" + appService.getServiceCode() + "銆戣皟鐢ㄦ柟寮忎笉瀵硅妫�鏌�,褰撳墠璇锋眰鏂瑰紡涓猴細" + httpMethod);
}
dataFlow.setApiCurrentService(ServiceCodeConstant.SERVICE_CODE_SYSTEM_TRANSFER);
+ } else if ("CMD".equals(appService.getIsInstance())) {
+ //濡傛灉鏄�忎紶绫� 璇锋眰鏂瑰紡蹇呴』涓庢帴鍙f彁渚涙柟璋冪敤鏂瑰紡涓�鑷�
+ String httpMethod = dataFlow.getRequestCurrentHeaders().get(CommonConstant.HTTP_METHOD);
+ if (!appService.getMethod().equals(httpMethod)) {
+ throw new ListenerExecuteException(ResponseConstant.RESULT_CODE_ERROR,
+ "鏈嶅姟銆�" + appService.getServiceCode() + "銆戣皟鐢ㄦ柟寮忎笉瀵硅妫�鏌�,褰撳墠璇锋眰鏂瑰紡涓猴細" + httpMethod);
+ }
+ dataFlow.setApiCurrentService(ServiceCodeConstant.SERVICE_CODE_SYSTEM_CMD);
} else {
dataFlow.setApiCurrentService(dataFlow.getRequestHeaders().get(CommonConstant.HTTP_SERVICE));
}
--
Gitblit v1.8.0