Skip to content

Commit 37c6fc4

Browse files
author
wuxw7
committed
完成 java110-logAgent 功能
1 parent 28441b3 commit 37c6fc4

8 files changed

Lines changed: 190 additions & 42 deletions

File tree

CenterService/src/main/java/com/java110/center/smo/impl/CenterServiceSMOImpl.java

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.java110.entity.center.DataFlowLinksCost;
2222
import com.java110.event.center.DataFlowEventPublishing;
2323

24+
import com.java110.log.agent.LogAgent;
2425
import com.java110.service.smo.IQueryServiceSMO;
2526
import org.apache.commons.lang3.math.NumberUtils;
2627
import org.springframework.beans.factory.annotation.Autowired;
@@ -132,12 +133,14 @@ public String service(String reqJson, Map<String, String> headers) throws SMOExc
132133
//DataFlowFactory.addCostTime(dataFlow, "service", "业务处理总耗时", dataFlow.getStartDate(), dataFlow.getEndDate());
133134

134135
//这里保存耗时,以及日志
135-
//saveLogMessage(dataFlow.getReqJson(), dataFlow.getResJson());
136+
saveLogMessage(dataFlow,LogAgent.createLogMessage(dataFlow.getRequestHeaders(),dataFlow.getReqJson().toJSONString()),
137+
LogAgent.createLogMessage(dataFlow.getResponseHeaders(),dataFlow.getResJson().toJSONString()));
136138

137139
//保存耗时
138140
//saveCostTimeLogMessage(dataFlow);
139141
//处理返回报文鉴权
140142
AuthenticationFactory.putSign(dataFlow, responseJson);
143+
141144
}
142145
resJson = encrypt(responseJson.toJSONString(),headers);
143146
/*LogAgent.sendLog(dataFlow.reBuilder(dataFlow.getTransactionId(),
@@ -211,7 +214,8 @@ public String serviceTransfer(String reqJson, Map<String, String> headers) throw
211214
DataFlowFactory.addCostTime(dataFlow, "service", "业务处理总耗时", dataFlow.getStartDate(), dataFlow.getEndDate());
212215

213216
//这里保存耗时,以及日志
214-
saveLogMessage(dataFlow.getReqJson(), dataFlow.getResJson());
217+
saveLogMessage(dataFlow,LogAgent.createLogMessage(dataFlow.getRequestHeaders(),dataFlow.getReqJson().toJSONString()),
218+
LogAgent.createLogMessage(dataFlow.getResponseHeaders(),dataFlow.getResJson().toJSONString()));
215219

216220
//保存耗时
217221
saveCostTimeLogMessage(dataFlow);
@@ -579,7 +583,10 @@ private void invalidCompletedBusinessSystem(DataFlow dataFlow) throws Exception{
579583
//发起撤单
580584
KafkaFactory.sendKafkaMessage(appRoute.getAppService().getMessageQueueName(),"",
581585
DataFlowFactory.getDeleteInstanceTableJson(dataFlow,completedBusiness,appRoute.getAppService()).toJSONString());
582-
saveLogMessage(DataFlowFactory.getDeleteInstanceTableJson(dataFlow,completedBusiness,appRoute.getAppService()),null);
586+
//saveLogMessage(DataFlowFactory.getDeleteInstanceTableJson(dataFlow,completedBusiness,appRoute.getAppService()),null);
587+
588+
saveLogMessage(dataFlow,LogAgent.createLogMessage(dataFlow.getRequestCurrentHeaders(),DataFlowFactory.getDeleteInstanceTableJson(dataFlow,completedBusiness,appRoute.getAppService()).toJSONString()),
589+
LogAgent.createLogMessage(dataFlow.getResponseCurrentHeaders(),ResponseConstant.RESULT_CODE_SUCCESS));
583590
}
584591
}
585592
}
@@ -716,7 +723,8 @@ public void receiveBusinessSystemNotifyMessage(String receiveJson) throws SMOExc
716723
updateBusinessNotifyError(dataFlow);
717724
}finally{
718725
DataFlowFactory.addCostTime(dataFlow, "receiveBusinessSystemNotifyMessage", "接受业务系统通知消息耗时", startDate);
719-
saveLogMessage(dataFlow.getReqJson(),null);
726+
saveLogMessage(dataFlow,LogAgent.createLogMessage(dataFlow.getRequestCurrentHeaders(),dataFlow.getReqJson().toJSONString()),
727+
LogAgent.createLogMessage(dataFlow.getResponseCurrentHeaders(),ResponseConstant.RESULT_CODE_SUCCESS));
720728
}
721729
}
722730

@@ -864,7 +872,8 @@ private void notifyBusinessSystemSuccessMessage(DataFlow dataFlow) throws Except
864872
KafkaFactory.sendKafkaMessage(
865873
DataFlowFactory.getService(dataFlow,dataFlow.getBusinesses().get(0).getServiceCode()).getMessageQueueName(),"",DataFlowFactory.getNotifyBusinessSuccessJson(dataFlow).toJSONString());
866874

867-
saveLogMessage(DataFlowFactory.getNotifyBusinessSuccessJson(dataFlow),null);
875+
saveLogMessage(dataFlow,LogAgent.createLogMessage(dataFlow.getRequestCurrentHeaders(),DataFlowFactory.getNotifyBusinessSuccessJson(dataFlow).toJSONString()),
876+
LogAgent.createLogMessage(dataFlow.getResponseCurrentHeaders(),ResponseConstant.RESULT_CODE_SUCCESS));
868877
}
869878

870879
/**
@@ -878,7 +887,8 @@ private void notifyBusinessSystemErrorMessage(DataFlow dataFlow) throws Exceptio
878887
KafkaFactory.sendKafkaMessage(
879888
DataFlowFactory.getService(dataFlow,dataFlow.getBusinesses().get(0).getServiceCode()).getMessageQueueName(),"",
880889
DataFlowFactory.getNotifyBusinessErrorJson(dataFlow).toJSONString());
881-
saveLogMessage(DataFlowFactory.getNotifyBusinessErrorJson(dataFlow),null);
890+
saveLogMessage(dataFlow,LogAgent.createLogMessage(dataFlow.getRequestCurrentHeaders(),DataFlowFactory.getNotifyBusinessErrorJson(dataFlow).toJSONString()),
891+
LogAgent.createLogMessage(dataFlow.getResponseCurrentHeaders(),ResponseConstant.RESULT_CODE_ERROR));
882892
}
883893

884894
/**
@@ -970,7 +980,8 @@ private void doComplateOrderAndBusiness(DataFlow dataFlow,List<Business> synchro
970980
JSONObject responseJson = doRequestBusinessSystem(dataFlow, service, requestBusinessJson);
971981

972982
DataFlowFactory.addCostTime(dataFlow, business.getServiceCode(), "调用"+business.getServiceName()+"-doComplete耗时", businessStartDate);
973-
saveLogMessage(requestBusinessJson,responseJson);
983+
saveLogMessage(dataFlow,LogAgent.createLogMessage(dataFlow.getRequestCurrentHeaders(),requestBusinessJson.toJSONString()),
984+
LogAgent.createLogMessage(dataFlow.getResponseCurrentHeaders(),responseJson.toJSONString()));
974985
}
975986

976987
}
@@ -999,7 +1010,8 @@ private void doBusinessTableDataInfoToInstanceTable(DataFlow dataFlow, List<Busi
9991010

10001011
updateBusinessStatusCdByBId(business.getbId(),StatusConstant.STATUS_CD_COMPLETE);
10011012
DataFlowFactory.addCostTime(dataFlow, business.getServiceCode(), "调用"+business.getServiceName()+"耗时", businessStartDate);
1002-
saveLogMessage(requestBusinessJson,responseJson);
1013+
saveLogMessage(dataFlow,LogAgent.createLogMessage(dataFlow.getRequestCurrentHeaders(),requestBusinessJson.toJSONString()),
1014+
LogAgent.createLogMessage(dataFlow.getResponseCurrentHeaders(),responseJson.toJSONString()));
10031015
}
10041016

10051017
if(dataFlow.getCurrentBusiness() == null){
@@ -1029,7 +1041,8 @@ private void doDeleteBusinessSystemInstanceData(DataFlow dataFlow, List<Business
10291041
requestBusinessJson = DataFlowFactory.getDeleteInstanceTableJson(dataFlow,business);
10301042
JSONObject responseJson = doRequestBusinessSystem(dataFlow, service, requestBusinessJson);
10311043
DataFlowFactory.addCostTime(dataFlow, business.getServiceCode(), "调用"+business.getServiceName()+"-撤单 耗时", businessStartDate);
1032-
saveLogMessage(requestBusinessJson,responseJson);
1044+
saveLogMessage(dataFlow,LogAgent.createLogMessage(dataFlow.getRequestCurrentHeaders(),requestBusinessJson.toJSONString()),
1045+
LogAgent.createLogMessage(dataFlow.getResponseCurrentHeaders(),responseJson.toJSONString()));
10331046
}
10341047
}
10351048

@@ -1066,8 +1079,8 @@ private String doTransferRequestBusinessSystem(DataFlow dataFlow, AppService ser
10661079
if(service.getMethod() == null || "".equals(service.getMethod())) {//post方式
10671080
//http://user-service/test/sayHello
10681081
HttpHeaders header = new HttpHeaders();
1069-
for(String key : dataFlow.getHeaders().keySet()){
1070-
header.add(key,dataFlow.getHeaders().get(key));
1082+
for(String key : dataFlow.getRequestCurrentHeaders().keySet()){
1083+
header.add(key,dataFlow.getRequestCurrentHeaders().get(key));
10711084
}
10721085
HttpEntity<String> httpEntity = new HttpEntity<String>(reqData, header);
10731086
responseMessage = restTemplateNoLoadBalanced.postForObject(service.getUrl(),httpEntity,String.class);
@@ -1109,7 +1122,8 @@ private void doSaveDataInfoToBusinessTable(DataFlow dataFlow, List<Business> syn
11091122
responseBusinesses.add(dataFlow.getResponseBusinessJson());
11101123

11111124
DataFlowFactory.addCostTime(dataFlow, business.getServiceCode(), "调用"+business.getServiceName()+"耗时", businessStartDate);
1112-
saveLogMessage(dataFlow.getRequestBusinessJson(),dataFlow.getResponseBusinessJson());
1125+
saveLogMessage(dataFlow,LogAgent.createLogMessage(dataFlow.getRequestCurrentHeaders(),dataFlow.getRequestBusinessJson().toJSONString()),
1126+
LogAgent.createLogMessage(dataFlow.getResponseCurrentHeaders(),dataFlow.getResponseBusinessJson().toJSONString()));
11131127
}
11141128
}
11151129

@@ -1141,26 +1155,25 @@ private void doAsynchronousBusinesses(DataFlow dataFlow) throws BusinessExceptio
11411155
dataFlow.setResponseBusinessJson(DataTransactionFactory.createOrderResponseJson(dataFlow.getTransactionId(),
11421156
ResponseConstant.RESULT_CODE_SUCCESS, "成功"));
11431157
DataFlowFactory.addCostTime(dataFlow, "doSynchronousBusinesses", "异步调用业务系统总耗时", startDate);
1144-
saveLogMessage(dataFlow.getRequestBusinessJson(),dataFlow.getResponseBusinessJson());
1158+
saveLogMessage(dataFlow,dataFlow.getRequestBusinessJson(),dataFlow.getResponseBusinessJson());
11451159
}
11461160

11471161

11481162
/**
11491163
* 保存日志信息
1150-
* @param requestJson
1164+
* @param dataFlow 数据流对象 封装用户请求的信息
1165+
*
1166+
* @param requestJson 请求报文 格式为
1167+
* {"headers":"",
1168+
* "body":""
1169+
* }
1170+
* @param responseJson 请求报文 格式为
1171+
* {"headers":"",
1172+
* "body":""
1173+
* }
11511174
*/
1152-
private void saveLogMessage(JSONObject requestJson,JSONObject responseJson){
1153-
1154-
try{
1155-
if(MappingConstant.VALUE_ON.equals(MappingCache.getValue(MappingConstant.KEY_LOG_ON_OFF))){
1156-
JSONObject log = new JSONObject();
1157-
log.put("request",requestJson);
1158-
log.put("response",responseJson);
1159-
KafkaFactory.sendKafkaMessage(KafkaConstant.TOPIC_LOG_NAME,"",log.toJSONString());
1160-
}
1161-
}catch (Exception e){
1162-
logger.error("报错日志出错了,",e);
1163-
}
1175+
private void saveLogMessage(DataFlow dataFlow,JSONObject requestJson,JSONObject responseJson){
1176+
LogAgent.sendLog(dataFlow,requestJson,responseJson);
11641177
}
11651178

11661179
/**

java110-core/src/main/java/com/java110/core/context/AbstractDataFlowContext.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ public abstract class AbstractDataFlowContext extends AbstractTransactionLog imp
5656

5757
private List<DataFlowLog> logDatas = new ArrayList<DataFlowLog>();
5858

59-
protected Map<String,String> headers = new HashMap<String,String>();
59+
protected Map<String,String> requestHeaders = new HashMap<String,String>();
60+
protected Map<String,String> requestCurrentHeaders = new HashMap<String,String>();
61+
protected Map<String,String> responseHeaders = new HashMap<String,String>();
62+
protected Map<String,String> responseCurrentHeaders = new HashMap<String,String>();
6063

6164
//请求开始时间
6265
private Date startDate;
@@ -187,8 +190,20 @@ public void setEndDate(Date endDate) {
187190
this.endDate = endDate;
188191
}
189192

190-
public Map<String, String> getHeaders() {
191-
return headers;
193+
public Map<String, String> getRequestHeaders() {
194+
return requestHeaders;
195+
}
196+
197+
public Map<String, String> getResponseHeaders() {
198+
return responseHeaders;
199+
}
200+
201+
public Map<String, String> getRequestCurrentHeaders() {
202+
return requestHeaders;
203+
}
204+
205+
public Map<String, String> getResponseCurrentHeaders() {
206+
return responseHeaders;
192207
}
193208

194209
public String getReqData() {

java110-core/src/main/java/com/java110/core/context/BusinessServiceDataFlow.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ public BusinessServiceDataFlow doBuilder(String reqInfo, Map<String, String> hea
5353
businesses.add(business);
5454
this.setCurrentBusiness(business);
5555
if (headerAll != null){
56-
this.headers.putAll(headerAll);
56+
this.requestCurrentHeaders.putAll(headerAll);
57+
this.requestHeaders.putAll(headerAll);
5758
}
5859
}catch (Exception e){
5960
throw new InitDataFlowContextException(ResponseConstant.RESULT_PARAM_ERROR,"初始化对象 BusinessServiceDataFlow 失败 "+reqInfo);

java110-core/src/main/java/com/java110/core/context/CodeDataFlow.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public CodeDataFlow doBuilder(String reqInfo, Map<String, String> headerAll) thr
5959
this.setRequestTime(reqInfoObj.getString("requestTime"));
6060

6161
if (headerAll != null && !headerAll.isEmpty()){
62-
this.headers.putAll(headerAll);
62+
this.requestCurrentHeaders.putAll(headerAll);
63+
this.requestHeaders.putAll(headerAll);
6364
}
6465

6566
if(headerAll != null && headerAll.containsKey("hostName")) {

java110-core/src/main/java/com/java110/core/context/DataFlow.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,10 @@ public DataFlow doBuilder(String reqInfo, Map<String,String> headerAll) throws E
241241
}
242242

243243
if (headerAll != null){
244-
this.headers.putAll(headerAll);
245-
this.setRequestURL(headers.get("REQUEST_URL"));
246-
this.setIp(headers.get("IP"));
244+
this.requestHeaders.putAll(headerAll);
245+
this.requestCurrentHeaders.putAll(headerAll);
246+
this.setRequestURL(requestHeaders.get("REQUEST_URL"));
247+
this.setIp(requestHeaders.get("IP"));
247248
}
248249

249250

@@ -286,9 +287,10 @@ public DataFlow builderTransfer(String reqInfo, Map<String,String> headerAll) th
286287
}
287288

288289
if (headerAll != null){
289-
this.headers.putAll(headerAll);
290-
this.setRequestURL(headers.get("REQUEST_URL"));
291-
this.setIp(headers.get("IP"));
290+
this.requestHeaders.putAll(headerAll);
291+
this.requestCurrentHeaders.putAll(headerAll);
292+
this.setRequestURL(requestHeaders.get("REQUEST_URL"));
293+
this.setIp(requestHeaders.get("IP"));
292294
}
293295

294296

java110-core/src/main/java/com/java110/core/context/DataFlowContext.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,28 @@ public interface DataFlowContext {
4444

4545
public List<Business> getBusinesses();
4646

47-
public Map<String, String> getHeaders();
47+
/**
48+
* 源请求头信息
49+
* @return
50+
*/
51+
public Map<String, String> getRequestHeaders();
52+
/**
53+
* 终返回头信息
54+
* @return
55+
*/
56+
public Map<String, String> getResponseHeaders();
57+
58+
/**
59+
* 当前请求头信息
60+
* @return
61+
*/
62+
public Map<String, String> getRequestCurrentHeaders();
63+
64+
/**
65+
* 当前返回头信息
66+
* @return
67+
*/
68+
public Map<String, String> getResponseCurrentHeaders();
4869

4970

5071
public Orders getOrder();

java110-core/src/main/java/com/java110/core/context/TransactionLog.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ public interface TransactionLog extends Serializable {
8181

8282
/**
8383
* 重新构建 TransactionLog 对象 主要用于服务提供方
84+
* @param requestMessage 请求数据
85+
* @param responseMessage 返回数据
86+
* @param logStatus 数据交互状态
8487
* @return
8588
*/
8689
public TransactionLog reBuilder(String requestMessage,String responseMessage,String logStatus);

0 commit comments

Comments
 (0)