Commit db8a61d7 authored by 赵啸非's avatar 赵啸非

修改网关日志

parent 356c858d
package com.mortals.xhx.common.pdu.access; package com.mortals.xhx.common.pdu.access;
import com.mortals.framework.model.BaseEntityLong;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import com.mortals.framework.model.BaseEntityLong;
/** /**
* 访问日志Pdu对象 * 访问日志Pdu对象
* *
* @author zxfei * @author zxfei
* @date 2022-08-17 * @date 2022-08-19
*/ */
public class AccessLogPdu extends BaseEntityLong { public class AccessLogPdu extends BaseEntityLong {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
/** /**
* 追踪Id * TraceId
*/ */
private String traceID; private String traceID;
/**
* 父服务调用id
*/
private Integer pspanId;
/**
* 当前服务跨度Id
*/
private Integer spanId;
/** /**
* 应用名称 * 应用名称
*/ */
...@@ -27,6 +33,10 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -27,6 +33,10 @@ public class AccessLogPdu extends BaseEntityLong {
* 实例IP * 实例IP
*/ */
private String hostName; private String hostName;
/**
* 目标服务
*/
private String targetServer;
/** /**
* 日志等级 * 日志等级
*/ */
...@@ -39,6 +49,10 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -39,6 +49,10 @@ public class AccessLogPdu extends BaseEntityLong {
* 请求方式 * 请求方式
*/ */
private String method; private String method;
/**
* 协议
*/
private String schema;
/** /**
* 用户浏览器UA * 用户浏览器UA
*/ */
...@@ -50,41 +64,76 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -50,41 +64,76 @@ public class AccessLogPdu extends BaseEntityLong {
/** /**
* 请求参数,json序列化 * 请求参数,json序列化
*/ */
private String requestParam; private String requestData;
/** /**
* 响应结果 * 响应结果,
*/ */
private String response; private String responseData;
/** /**
* 日志产生时间 * 日志产生时间
*/ */
private Date logTime; private Date logTime;
/**
* 请求时间
*/
private Date requestTime;
/**
* 响应时间
*/
private Date responseTime;
/** /**
* 持续时间,单位毫秒 * 持续时间,单位毫秒
*/ */
private Integer duration; private Long duration;
/** 开始 主键ID,主键,自增长 */ /** 开始 主键ID */
private Long idStart; private Long idStart;
/** 结束 主键ID,主键,自增长 */ /** 结束 主键ID */
private Long idEnd; private Long idEnd;
/** 增加 主键ID,主键,自增长 */ /** 增加 主键ID */
private Long idIncrement; private Long idIncrement;
/** 主键ID,主键,自增长列表 */ /** 主键ID列表 */
private List <Long> idList; private List <Long> idList;
/** 追踪Id */ /** TraceId */
private List<String> traceIDList; private List<String> traceIDList;
/** 开始 父服务调用id */
private Integer pspanIdStart;
/** 结束 父服务调用id */
private Integer pspanIdEnd;
/** 增加 父服务调用id */
private Integer pspanIdIncrement;
/** 父服务调用id列表 */
private List <Integer> pspanIdList;
/** 开始 当前服务跨度Id */
private Integer spanIdStart;
/** 结束 当前服务跨度Id */
private Integer spanIdEnd;
/** 增加 当前服务跨度Id */
private Integer spanIdIncrement;
/** 当前服务跨度Id列表 */
private List <Integer> spanIdList;
/** 应用名称 */ /** 应用名称 */
private List<String> appNameList; private List<String> appNameList;
/** 实例IP */ /** 实例IP */
private List<String> hostNameList; private List<String> hostNameList;
/** 目标服务 */
private List<String> targetServerList;
/** 日志等级 */ /** 日志等级 */
private List<String> logLevelList; private List<String> logLevelList;
...@@ -94,6 +143,9 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -94,6 +143,9 @@ public class AccessLogPdu extends BaseEntityLong {
/** 请求方式 */ /** 请求方式 */
private List<String> methodList; private List<String> methodList;
/** 协议 */
private List<String> schemaList;
/** 用户浏览器UA */ /** 用户浏览器UA */
private List<String> uaList; private List<String> uaList;
...@@ -101,10 +153,10 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -101,10 +153,10 @@ public class AccessLogPdu extends BaseEntityLong {
private List<String> uriList; private List<String> uriList;
/** 请求参数,json序列化 */ /** 请求参数,json序列化 */
private List<String> requestParamList; private List<String> requestDataList;
/** 响应结果 */ /** 响应结果, */
private List<String> responseList; private List<String> responseDataList;
/** 开始 日志产生时间 */ /** 开始 日志产生时间 */
private String logTimeStart; private String logTimeStart;
...@@ -112,17 +164,29 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -112,17 +164,29 @@ public class AccessLogPdu extends BaseEntityLong {
/** 结束 日志产生时间 */ /** 结束 日志产生时间 */
private String logTimeEnd; private String logTimeEnd;
/** 开始 请求时间 */
private String requestTimeStart;
/** 结束 请求时间 */
private String requestTimeEnd;
/** 开始 响应时间 */
private String responseTimeStart;
/** 结束 响应时间 */
private String responseTimeEnd;
/** 开始 持续时间,单位毫秒 */ /** 开始 持续时间,单位毫秒 */
private Integer durationStart; private Long durationStart;
/** 结束 持续时间,单位毫秒 */ /** 结束 持续时间,单位毫秒 */
private Integer durationEnd; private Long durationEnd;
/** 增加 持续时间,单位毫秒 */ /** 增加 持续时间,单位毫秒 */
private Integer durationIncrement; private Long durationIncrement;
/** 持续时间,单位毫秒列表 */ /** 持续时间,单位毫秒列表 */
private List <Integer> durationList; private List <Long> durationList;
/** 开始 创建用户 */ /** 开始 创建用户 */
private Long createUserIdStart; private Long createUserIdStart;
...@@ -151,19 +215,47 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -151,19 +215,47 @@ public class AccessLogPdu extends BaseEntityLong {
public AccessLogPdu(){} public AccessLogPdu(){}
/** /**
* 获取 追踪Id * 获取 TraceId
* @return String * @return String
*/ */
public String getTraceID(){ public String getTraceID(){
return traceID; return traceID;
} }
/** /**
* 设置 追踪Id * 设置 TraceId
* @param traceID * @param traceID
*/ */
public void setTraceID(String traceID){ public void setTraceID(String traceID){
this.traceID = traceID; this.traceID = traceID;
} }
/**
* 获取 父服务调用id
* @return Integer
*/
public Integer getPspanId(){
return pspanId;
}
/**
* 设置 父服务调用id
* @param pspanId
*/
public void setPspanId(Integer pspanId){
this.pspanId = pspanId;
}
/**
* 获取 当前服务跨度Id
* @return Integer
*/
public Integer getSpanId(){
return spanId;
}
/**
* 设置 当前服务跨度Id
* @param spanId
*/
public void setSpanId(Integer spanId){
this.spanId = spanId;
}
/** /**
* 获取 应用名称 * 获取 应用名称
* @return String * @return String
...@@ -192,6 +284,20 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -192,6 +284,20 @@ public class AccessLogPdu extends BaseEntityLong {
public void setHostName(String hostName){ public void setHostName(String hostName){
this.hostName = hostName; this.hostName = hostName;
} }
/**
* 获取 目标服务
* @return String
*/
public String getTargetServer(){
return targetServer;
}
/**
* 设置 目标服务
* @param targetServer
*/
public void setTargetServer(String targetServer){
this.targetServer = targetServer;
}
/** /**
* 获取 日志等级 * 获取 日志等级
* @return String * @return String
...@@ -234,6 +340,20 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -234,6 +340,20 @@ public class AccessLogPdu extends BaseEntityLong {
public void setMethod(String method){ public void setMethod(String method){
this.method = method; this.method = method;
} }
/**
* 获取 协议
* @return String
*/
public String getSchema(){
return schema;
}
/**
* 设置 协议
* @param schema
*/
public void setSchema(String schema){
this.schema = schema;
}
/** /**
* 获取 用户浏览器UA * 获取 用户浏览器UA
* @return String * @return String
...@@ -266,29 +386,29 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -266,29 +386,29 @@ public class AccessLogPdu extends BaseEntityLong {
* 获取 请求参数,json序列化 * 获取 请求参数,json序列化
* @return String * @return String
*/ */
public String getRequestParam(){ public String getRequestData(){
return requestParam; return requestData;
} }
/** /**
* 设置 请求参数,json序列化 * 设置 请求参数,json序列化
* @param requestParam * @param requestData
*/ */
public void setRequestParam(String requestParam){ public void setRequestData(String requestData){
this.requestParam = requestParam; this.requestData = requestData;
} }
/** /**
* 获取 响应结果 * 获取 响应结果,
* @return String * @return String
*/ */
public String getResponse(){ public String getResponseData(){
return response; return responseData;
} }
/** /**
* 设置 响应结果 * 设置 响应结果,
* @param response * @param responseData
*/ */
public void setResponse(String response){ public void setResponseData(String responseData){
this.response = response; this.responseData = responseData;
} }
/** /**
* 获取 日志产生时间 * 获取 日志产生时间
...@@ -304,23 +424,51 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -304,23 +424,51 @@ public class AccessLogPdu extends BaseEntityLong {
public void setLogTime(Date logTime){ public void setLogTime(Date logTime){
this.logTime = logTime; this.logTime = logTime;
} }
/**
* 获取 请求时间
* @return Date
*/
public Date getRequestTime(){
return requestTime;
}
/**
* 设置 请求时间
* @param requestTime
*/
public void setRequestTime(Date requestTime){
this.requestTime = requestTime;
}
/**
* 获取 响应时间
* @return Date
*/
public Date getResponseTime(){
return responseTime;
}
/**
* 设置 响应时间
* @param responseTime
*/
public void setResponseTime(Date responseTime){
this.responseTime = responseTime;
}
/** /**
* 获取 持续时间,单位毫秒 * 获取 持续时间,单位毫秒
* @return Integer * @return Long
*/ */
public Integer getDuration(){ public Long getDuration(){
return duration; return duration;
} }
/** /**
* 设置 持续时间,单位毫秒 * 设置 持续时间,单位毫秒
* @param duration * @param duration
*/ */
public void setDuration(Integer duration){ public void setDuration(Long duration){
this.duration = duration; this.duration = duration;
} }
/** /**
* 获取 开始 主键ID,主键,自增长 * 获取 开始 主键ID
* @return idStart * @return idStart
*/ */
public Long getIdStart(){ public Long getIdStart(){
...@@ -328,7 +476,7 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -328,7 +476,7 @@ public class AccessLogPdu extends BaseEntityLong {
} }
/** /**
* 设置 开始 主键ID,主键,自增长 * 设置 开始 主键ID
* @param idStart * @param idStart
*/ */
public void setIdStart(Long idStart){ public void setIdStart(Long idStart){
...@@ -336,7 +484,7 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -336,7 +484,7 @@ public class AccessLogPdu extends BaseEntityLong {
} }
/** /**
* 获取 结束 主键ID,主键,自增长 * 获取 结束 主键ID
* @return $idEnd * @return $idEnd
*/ */
public Long getIdEnd(){ public Long getIdEnd(){
...@@ -344,7 +492,7 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -344,7 +492,7 @@ public class AccessLogPdu extends BaseEntityLong {
} }
/** /**
* 设置 结束 主键ID,主键,自增长 * 设置 结束 主键ID
* @param idEnd * @param idEnd
*/ */
public void setIdEnd(Long idEnd){ public void setIdEnd(Long idEnd){
...@@ -352,7 +500,7 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -352,7 +500,7 @@ public class AccessLogPdu extends BaseEntityLong {
} }
/** /**
* 获取 增加 主键ID,主键,自增长 * 获取 增加 主键ID
* @return idIncrement * @return idIncrement
*/ */
public Long getIdIncrement(){ public Long getIdIncrement(){
...@@ -360,7 +508,7 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -360,7 +508,7 @@ public class AccessLogPdu extends BaseEntityLong {
} }
/** /**
* 设置 增加 主键ID,主键,自增长 * 设置 增加 主键ID
* @param idIncrement * @param idIncrement
*/ */
public void setIdIncrement(Long idIncrement){ public void setIdIncrement(Long idIncrement){
...@@ -368,7 +516,7 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -368,7 +516,7 @@ public class AccessLogPdu extends BaseEntityLong {
} }
/** /**
* 获取 主键ID,主键,自增长 * 获取 主键ID
* @return idList * @return idList
*/ */
public List<Long> getIdList(){ public List<Long> getIdList(){
...@@ -376,7 +524,7 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -376,7 +524,7 @@ public class AccessLogPdu extends BaseEntityLong {
} }
/** /**
* 设置 主键ID,主键,自增长 * 设置 主键ID
* @param idList * @param idList
*/ */
public void setIdList(List<Long> idList){ public void setIdList(List<Long> idList){
...@@ -384,7 +532,7 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -384,7 +532,7 @@ public class AccessLogPdu extends BaseEntityLong {
} }
/** /**
* 获取 追踪Id * 获取 TraceId
* @return traceIDList * @return traceIDList
*/ */
public List<String> getTraceIDList(){ public List<String> getTraceIDList(){
...@@ -392,12 +540,140 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -392,12 +540,140 @@ public class AccessLogPdu extends BaseEntityLong {
} }
/** /**
* 设置 追踪Id * 设置 TraceId
* @param traceIDList * @param traceIDList
*/ */
public void setTraceIDList(List<String> traceIDList){ public void setTraceIDList(List<String> traceIDList){
this.traceIDList = traceIDList; this.traceIDList = traceIDList;
} }
/**
* 获取 开始 父服务调用id
* @return pspanIdStart
*/
public Integer getPspanIdStart(){
return this.pspanIdStart;
}
/**
* 设置 开始 父服务调用id
* @param pspanIdStart
*/
public void setPspanIdStart(Integer pspanIdStart){
this.pspanIdStart = pspanIdStart;
}
/**
* 获取 结束 父服务调用id
* @return $pspanIdEnd
*/
public Integer getPspanIdEnd(){
return this.pspanIdEnd;
}
/**
* 设置 结束 父服务调用id
* @param pspanIdEnd
*/
public void setPspanIdEnd(Integer pspanIdEnd){
this.pspanIdEnd = pspanIdEnd;
}
/**
* 获取 增加 父服务调用id
* @return pspanIdIncrement
*/
public Integer getPspanIdIncrement(){
return this.pspanIdIncrement;
}
/**
* 设置 增加 父服务调用id
* @param pspanIdIncrement
*/
public void setPspanIdIncrement(Integer pspanIdIncrement){
this.pspanIdIncrement = pspanIdIncrement;
}
/**
* 获取 父服务调用id
* @return pspanIdList
*/
public List<Integer> getPspanIdList(){
return this.pspanIdList;
}
/**
* 设置 父服务调用id
* @param pspanIdList
*/
public void setPspanIdList(List<Integer> pspanIdList){
this.pspanIdList = pspanIdList;
}
/**
* 获取 开始 当前服务跨度Id
* @return spanIdStart
*/
public Integer getSpanIdStart(){
return this.spanIdStart;
}
/**
* 设置 开始 当前服务跨度Id
* @param spanIdStart
*/
public void setSpanIdStart(Integer spanIdStart){
this.spanIdStart = spanIdStart;
}
/**
* 获取 结束 当前服务跨度Id
* @return $spanIdEnd
*/
public Integer getSpanIdEnd(){
return this.spanIdEnd;
}
/**
* 设置 结束 当前服务跨度Id
* @param spanIdEnd
*/
public void setSpanIdEnd(Integer spanIdEnd){
this.spanIdEnd = spanIdEnd;
}
/**
* 获取 增加 当前服务跨度Id
* @return spanIdIncrement
*/
public Integer getSpanIdIncrement(){
return this.spanIdIncrement;
}
/**
* 设置 增加 当前服务跨度Id
* @param spanIdIncrement
*/
public void setSpanIdIncrement(Integer spanIdIncrement){
this.spanIdIncrement = spanIdIncrement;
}
/**
* 获取 当前服务跨度Id
* @return spanIdList
*/
public List<Integer> getSpanIdList(){
return this.spanIdList;
}
/**
* 设置 当前服务跨度Id
* @param spanIdList
*/
public void setSpanIdList(List<Integer> spanIdList){
this.spanIdList = spanIdList;
}
/** /**
* 获取 应用名称 * 获取 应用名称
* @return appNameList * @return appNameList
...@@ -428,6 +704,21 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -428,6 +704,21 @@ public class AccessLogPdu extends BaseEntityLong {
public void setHostNameList(List<String> hostNameList){ public void setHostNameList(List<String> hostNameList){
this.hostNameList = hostNameList; this.hostNameList = hostNameList;
} }
/**
* 获取 目标服务
* @return targetServerList
*/
public List<String> getTargetServerList(){
return this.targetServerList;
}
/**
* 设置 目标服务
* @param targetServerList
*/
public void setTargetServerList(List<String> targetServerList){
this.targetServerList = targetServerList;
}
/** /**
* 获取 日志等级 * 获取 日志等级
* @return logLevelList * @return logLevelList
...@@ -473,6 +764,21 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -473,6 +764,21 @@ public class AccessLogPdu extends BaseEntityLong {
public void setMethodList(List<String> methodList){ public void setMethodList(List<String> methodList){
this.methodList = methodList; this.methodList = methodList;
} }
/**
* 获取 协议
* @return schemaList
*/
public List<String> getSchemaList(){
return this.schemaList;
}
/**
* 设置 协议
* @param schemaList
*/
public void setSchemaList(List<String> schemaList){
this.schemaList = schemaList;
}
/** /**
* 获取 用户浏览器UA * 获取 用户浏览器UA
* @return uaList * @return uaList
...@@ -505,33 +811,33 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -505,33 +811,33 @@ public class AccessLogPdu extends BaseEntityLong {
} }
/** /**
* 获取 请求参数,json序列化 * 获取 请求参数,json序列化
* @return requestParamList * @return requestDataList
*/ */
public List<String> getRequestParamList(){ public List<String> getRequestDataList(){
return this.requestParamList; return this.requestDataList;
} }
/** /**
* 设置 请求参数,json序列化 * 设置 请求参数,json序列化
* @param requestParamList * @param requestDataList
*/ */
public void setRequestParamList(List<String> requestParamList){ public void setRequestDataList(List<String> requestDataList){
this.requestParamList = requestParamList; this.requestDataList = requestDataList;
} }
/** /**
* 获取 响应结果 * 获取 响应结果,
* @return responseList * @return responseDataList
*/ */
public List<String> getResponseList(){ public List<String> getResponseDataList(){
return this.responseList; return this.responseDataList;
} }
/** /**
* 设置 响应结果 * 设置 响应结果,
* @param responseList * @param responseDataList
*/ */
public void setResponseList(List<String> responseList){ public void setResponseDataList(List<String> responseDataList){
this.responseList = responseList; this.responseDataList = responseDataList;
} }
/** /**
* 获取 开始 日志产生时间 * 获取 开始 日志产生时间
...@@ -565,11 +871,75 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -565,11 +871,75 @@ public class AccessLogPdu extends BaseEntityLong {
this.logTimeEnd = logTimeEnd; this.logTimeEnd = logTimeEnd;
} }
/**
* 获取 开始 请求时间
* @return requestTimeStart
*/
public String getRequestTimeStart(){
return this.requestTimeStart;
}
/**
* 设置 开始 请求时间
* @param requestTimeStart
*/
public void setRequestTimeStart(String requestTimeStart){
this.requestTimeStart = requestTimeStart;
}
/**
* 获取 结束 请求时间
* @return requestTimeEnd
*/
public String getRequestTimeEnd(){
return this.requestTimeEnd;
}
/**
* 设置 结束 请求时间
* @param requestTimeEnd
*/
public void setRequestTimeEnd(String requestTimeEnd){
this.requestTimeEnd = requestTimeEnd;
}
/**
* 获取 开始 响应时间
* @return responseTimeStart
*/
public String getResponseTimeStart(){
return this.responseTimeStart;
}
/**
* 设置 开始 响应时间
* @param responseTimeStart
*/
public void setResponseTimeStart(String responseTimeStart){
this.responseTimeStart = responseTimeStart;
}
/**
* 获取 结束 响应时间
* @return responseTimeEnd
*/
public String getResponseTimeEnd(){
return this.responseTimeEnd;
}
/**
* 设置 结束 响应时间
* @param responseTimeEnd
*/
public void setResponseTimeEnd(String responseTimeEnd){
this.responseTimeEnd = responseTimeEnd;
}
/** /**
* 获取 开始 持续时间,单位毫秒 * 获取 开始 持续时间,单位毫秒
* @return durationStart * @return durationStart
*/ */
public Integer getDurationStart(){ public Long getDurationStart(){
return this.durationStart; return this.durationStart;
} }
...@@ -577,7 +947,7 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -577,7 +947,7 @@ public class AccessLogPdu extends BaseEntityLong {
* 设置 开始 持续时间,单位毫秒 * 设置 开始 持续时间,单位毫秒
* @param durationStart * @param durationStart
*/ */
public void setDurationStart(Integer durationStart){ public void setDurationStart(Long durationStart){
this.durationStart = durationStart; this.durationStart = durationStart;
} }
...@@ -585,7 +955,7 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -585,7 +955,7 @@ public class AccessLogPdu extends BaseEntityLong {
* 获取 结束 持续时间,单位毫秒 * 获取 结束 持续时间,单位毫秒
* @return $durationEnd * @return $durationEnd
*/ */
public Integer getDurationEnd(){ public Long getDurationEnd(){
return this.durationEnd; return this.durationEnd;
} }
...@@ -593,7 +963,7 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -593,7 +963,7 @@ public class AccessLogPdu extends BaseEntityLong {
* 设置 结束 持续时间,单位毫秒 * 设置 结束 持续时间,单位毫秒
* @param durationEnd * @param durationEnd
*/ */
public void setDurationEnd(Integer durationEnd){ public void setDurationEnd(Long durationEnd){
this.durationEnd = durationEnd; this.durationEnd = durationEnd;
} }
...@@ -601,7 +971,7 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -601,7 +971,7 @@ public class AccessLogPdu extends BaseEntityLong {
* 获取 增加 持续时间,单位毫秒 * 获取 增加 持续时间,单位毫秒
* @return durationIncrement * @return durationIncrement
*/ */
public Integer getDurationIncrement(){ public Long getDurationIncrement(){
return this.durationIncrement; return this.durationIncrement;
} }
...@@ -609,7 +979,7 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -609,7 +979,7 @@ public class AccessLogPdu extends BaseEntityLong {
* 设置 增加 持续时间,单位毫秒 * 设置 增加 持续时间,单位毫秒
* @param durationIncrement * @param durationIncrement
*/ */
public void setDurationIncrement(Integer durationIncrement){ public void setDurationIncrement(Long durationIncrement){
this.durationIncrement = durationIncrement; this.durationIncrement = durationIncrement;
} }
...@@ -617,7 +987,7 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -617,7 +987,7 @@ public class AccessLogPdu extends BaseEntityLong {
* 获取 持续时间,单位毫秒 * 获取 持续时间,单位毫秒
* @return durationList * @return durationList
*/ */
public List<Integer> getDurationList(){ public List<Long> getDurationList(){
return this.durationList; return this.durationList;
} }
...@@ -625,7 +995,7 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -625,7 +995,7 @@ public class AccessLogPdu extends BaseEntityLong {
* 设置 持续时间,单位毫秒 * 设置 持续时间,单位毫秒
* @param durationList * @param durationList
*/ */
public void setDurationList(List<Integer> durationList){ public void setDurationList(List<Long> durationList){
this.durationList = durationList; this.durationList = durationList;
} }
...@@ -726,7 +1096,7 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -726,7 +1096,7 @@ public class AccessLogPdu extends BaseEntityLong {
} }
/** /**
* 设置 主键ID,主键,自增长 * 设置 主键ID
* @param id * @param id
*/ */
public AccessLogPdu id(Long id){ public AccessLogPdu id(Long id){
...@@ -735,7 +1105,7 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -735,7 +1105,7 @@ public class AccessLogPdu extends BaseEntityLong {
} }
/** /**
* 设置 开始 主键ID,主键,自增长 * 设置 开始 主键ID
* @param idStart * @param idStart
*/ */
public AccessLogPdu idStart(Long idStart){ public AccessLogPdu idStart(Long idStart){
...@@ -744,7 +1114,7 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -744,7 +1114,7 @@ public class AccessLogPdu extends BaseEntityLong {
} }
/** /**
* 设置 结束 主键ID,主键,自增长 * 设置 结束 主键ID
* @param idEnd * @param idEnd
*/ */
public AccessLogPdu idEnd(Long idEnd){ public AccessLogPdu idEnd(Long idEnd){
...@@ -753,7 +1123,7 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -753,7 +1123,7 @@ public class AccessLogPdu extends BaseEntityLong {
} }
/** /**
* 设置 增加 主键ID,主键,自增长 * 设置 增加 主键ID
* @param idIncrement * @param idIncrement
*/ */
public AccessLogPdu idIncrement(Long idIncrement){ public AccessLogPdu idIncrement(Long idIncrement){
...@@ -762,7 +1132,7 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -762,7 +1132,7 @@ public class AccessLogPdu extends BaseEntityLong {
} }
/** /**
* 设置 主键ID,主键,自增长 * 设置 主键ID
* @param idList * @param idList
*/ */
public AccessLogPdu idList(List<Long> idList){ public AccessLogPdu idList(List<Long> idList){
...@@ -772,7 +1142,7 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -772,7 +1142,7 @@ public class AccessLogPdu extends BaseEntityLong {
/** /**
* 设置 追踪Id * 设置 TraceId
* @param traceID * @param traceID
*/ */
public AccessLogPdu traceID(String traceID){ public AccessLogPdu traceID(String traceID){
...@@ -781,7 +1151,7 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -781,7 +1151,7 @@ public class AccessLogPdu extends BaseEntityLong {
} }
/** /**
* 设置 追踪Id * 设置 TraceId
* @param traceIDList * @param traceIDList
*/ */
public AccessLogPdu traceIDList(List<String> traceIDList){ public AccessLogPdu traceIDList(List<String> traceIDList){
...@@ -789,6 +1159,96 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -789,6 +1159,96 @@ public class AccessLogPdu extends BaseEntityLong {
return this; return this;
} }
/**
* 设置 父服务调用id
* @param pspanId
*/
public AccessLogPdu pspanId(Integer pspanId){
setPspanId(pspanId);
return this;
}
/**
* 设置 开始 父服务调用id
* @param pspanIdStart
*/
public AccessLogPdu pspanIdStart(Integer pspanIdStart){
this.pspanIdStart = pspanIdStart;
return this;
}
/**
* 设置 结束 父服务调用id
* @param pspanIdEnd
*/
public AccessLogPdu pspanIdEnd(Integer pspanIdEnd){
this.pspanIdEnd = pspanIdEnd;
return this;
}
/**
* 设置 增加 父服务调用id
* @param pspanIdIncrement
*/
public AccessLogPdu pspanIdIncrement(Integer pspanIdIncrement){
this.pspanIdIncrement = pspanIdIncrement;
return this;
}
/**
* 设置 父服务调用id
* @param pspanIdList
*/
public AccessLogPdu pspanIdList(List<Integer> pspanIdList){
this.pspanIdList = pspanIdList;
return this;
}
/**
* 设置 当前服务跨度Id
* @param spanId
*/
public AccessLogPdu spanId(Integer spanId){
setSpanId(spanId);
return this;
}
/**
* 设置 开始 当前服务跨度Id
* @param spanIdStart
*/
public AccessLogPdu spanIdStart(Integer spanIdStart){
this.spanIdStart = spanIdStart;
return this;
}
/**
* 设置 结束 当前服务跨度Id
* @param spanIdEnd
*/
public AccessLogPdu spanIdEnd(Integer spanIdEnd){
this.spanIdEnd = spanIdEnd;
return this;
}
/**
* 设置 增加 当前服务跨度Id
* @param spanIdIncrement
*/
public AccessLogPdu spanIdIncrement(Integer spanIdIncrement){
this.spanIdIncrement = spanIdIncrement;
return this;
}
/**
* 设置 当前服务跨度Id
* @param spanIdList
*/
public AccessLogPdu spanIdList(List<Integer> spanIdList){
this.spanIdList = spanIdList;
return this;
}
/** /**
* 设置 应用名称 * 设置 应用名称
...@@ -828,6 +1288,25 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -828,6 +1288,25 @@ public class AccessLogPdu extends BaseEntityLong {
} }
/**
* 设置 目标服务
* @param targetServer
*/
public AccessLogPdu targetServer(String targetServer){
setTargetServer(targetServer);
return this;
}
/**
* 设置 目标服务
* @param targetServerList
*/
public AccessLogPdu targetServerList(List<String> targetServerList){
this.targetServerList = targetServerList;
return this;
}
/** /**
* 设置 日志等级 * 设置 日志等级
* @param logLevel * @param logLevel
...@@ -885,6 +1364,25 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -885,6 +1364,25 @@ public class AccessLogPdu extends BaseEntityLong {
} }
/**
* 设置 协议
* @param schema
*/
public AccessLogPdu schema(String schema){
setSchema(schema);
return this;
}
/**
* 设置 协议
* @param schemaList
*/
public AccessLogPdu schemaList(List<String> schemaList){
this.schemaList = schemaList;
return this;
}
/** /**
* 设置 用户浏览器UA * 设置 用户浏览器UA
* @param ua * @param ua
...@@ -925,47 +1423,49 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -925,47 +1423,49 @@ public class AccessLogPdu extends BaseEntityLong {
/** /**
* 设置 请求参数,json序列化 * 设置 请求参数,json序列化
* @param requestParam * @param requestData
*/ */
public AccessLogPdu requestParam(String requestParam){ public AccessLogPdu requestData(String requestData){
setRequestParam(requestParam); setRequestData(requestData);
return this; return this;
} }
/** /**
* 设置 请求参数,json序列化 * 设置 请求参数,json序列化
* @param requestParamList * @param requestDataList
*/ */
public AccessLogPdu requestParamList(List<String> requestParamList){ public AccessLogPdu requestDataList(List<String> requestDataList){
this.requestParamList = requestParamList; this.requestDataList = requestDataList;
return this; return this;
} }
/** /**
* 设置 响应结果 * 设置 响应结果,
* @param response * @param responseData
*/ */
public AccessLogPdu response(String response){ public AccessLogPdu responseData(String responseData){
setResponse(response); setResponseData(responseData);
return this; return this;
} }
/** /**
* 设置 响应结果 * 设置 响应结果,
* @param responseList * @param responseDataList
*/ */
public AccessLogPdu responseList(List<String> responseList){ public AccessLogPdu responseDataList(List<String> responseDataList){
this.responseList = responseList; this.responseDataList = responseDataList;
return this; return this;
} }
/** /**
* 设置 持续时间,单位毫秒 * 设置 持续时间,单位毫秒
* @param duration * @param duration
*/ */
public AccessLogPdu duration(Integer duration){ public AccessLogPdu duration(Long duration){
setDuration(duration); setDuration(duration);
return this; return this;
} }
...@@ -974,7 +1474,7 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -974,7 +1474,7 @@ public class AccessLogPdu extends BaseEntityLong {
* 设置 开始 持续时间,单位毫秒 * 设置 开始 持续时间,单位毫秒
* @param durationStart * @param durationStart
*/ */
public AccessLogPdu durationStart(Integer durationStart){ public AccessLogPdu durationStart(Long durationStart){
this.durationStart = durationStart; this.durationStart = durationStart;
return this; return this;
} }
...@@ -983,7 +1483,7 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -983,7 +1483,7 @@ public class AccessLogPdu extends BaseEntityLong {
* 设置 结束 持续时间,单位毫秒 * 设置 结束 持续时间,单位毫秒
* @param durationEnd * @param durationEnd
*/ */
public AccessLogPdu durationEnd(Integer durationEnd){ public AccessLogPdu durationEnd(Long durationEnd){
this.durationEnd = durationEnd; this.durationEnd = durationEnd;
return this; return this;
} }
...@@ -992,7 +1492,7 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -992,7 +1492,7 @@ public class AccessLogPdu extends BaseEntityLong {
* 设置 增加 持续时间,单位毫秒 * 设置 增加 持续时间,单位毫秒
* @param durationIncrement * @param durationIncrement
*/ */
public AccessLogPdu durationIncrement(Integer durationIncrement){ public AccessLogPdu durationIncrement(Long durationIncrement){
this.durationIncrement = durationIncrement; this.durationIncrement = durationIncrement;
return this; return this;
} }
...@@ -1001,7 +1501,7 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -1001,7 +1501,7 @@ public class AccessLogPdu extends BaseEntityLong {
* 设置 持续时间,单位毫秒 * 设置 持续时间,单位毫秒
* @param durationList * @param durationList
*/ */
public AccessLogPdu durationList(List<Integer> durationList){ public AccessLogPdu durationList(List<Long> durationList){
this.durationList = durationList; this.durationList = durationList;
return this; return this;
} }
...@@ -1053,7 +1553,7 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -1053,7 +1553,7 @@ public class AccessLogPdu extends BaseEntityLong {
/** /**
* 获取 OR条件集合,列表项之间是OR,项内容之间是AND,如:(list[0].1 and list[0].2) or (list[1].3 and list[1].4) * 获取 OR条件集合,列表项之间是OR,项内容之间是AND,如:(list[0].1 and list[0].2) or (list[1].3 and list[1].4)
* @return orConditionList * @return orConditionList
*/ */
...@@ -1105,26 +1605,38 @@ public class AccessLogPdu extends BaseEntityLong { ...@@ -1105,26 +1605,38 @@ public class AccessLogPdu extends BaseEntityLong {
this.traceID = ""; this.traceID = "";
this.pspanId = 0;
this.spanId = null;
this.appName = ""; this.appName = "";
this.hostName = ""; this.hostName = "";
this.logLevel = ""; this.targetServer = "";
this.logLevel = "INFO";
this.requestIp = ""; this.requestIp = "";
this.method = ""; this.method = "";
this.schema = "http";
this.ua = ""; this.ua = "";
this.uri = ""; this.uri = "";
this.requestParam = ""; this.requestData = "";
this.response = ""; this.responseData = "";
this.logTime = null; this.logTime = null;
this.requestTime = null;
this.responseTime = null;
this.duration = null; this.duration = null;
} }
} }
\ No newline at end of file
package com.mortals.xhx.base.framework.filter;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.mortals.framework.util.StringUtils;
import com.mortals.xhx.base.framework.service.MessageProducer;
import com.mortals.xhx.common.pdu.access.AccessLogPdu;
import com.mortals.xhx.common.utils.IpUtils;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.reactivestreams.Publisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.cloud.gateway.filter.factory.rewrite.CachedBodyOutputMessage;
import org.springframework.cloud.gateway.route.Route;
import org.springframework.cloud.gateway.support.BodyInserterContext;
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.codec.HttpMessageReader;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpRequestDecorator;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.BodyInserter;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.HandlerStrategies;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.util.WebUtils;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* @author: zxfei
* @date: 2022/8/18 23:52
* @description:
**/
@Slf4j
@Component
public class AccessLogFilter implements GlobalFilter, Ordered {
// @Autowired
// private AccessLogService accessLogService;
@Value("${spring.application.name:gateway}")
private String appName;
@Autowired
private MessageProducer messageProducer;
private static final String TRACE_ID = "traceId";
private static final String SPAN_ID = "spanId";
private static final String PSPAN_ID = "pspanId";
private final List<HttpMessageReader<?>> messageReaders = HandlerStrategies.withDefaults().messageReaders();
/**
* 顺序必须是<-1,否则标准的NettyWriteResponseFilter将在您的过滤器得到一个被调用的机会之前发送响应
* 也就是说如果不小于 -1 ,将不会执行获取后端响应的逻辑
*
* @return
*/
@Override
public int getOrder() {
return -2;
}
@Override
@SuppressWarnings("unchecked")
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpRequest request = exchange.getRequest();
// 请求路径
String requestPath = request.getPath().pathWithinApplication().value();
Route route = getGatewayRoute(exchange);
// String ipAddress = WebUtils.get(request);
String ipAddress = IpUtils.getRealIpAddress(exchange.getRequest());
Map<String, String> headers = exchange.getRequest().getHeaders().toSingleValueMap();
AccessLogPdu accessLogPdu = new AccessLogPdu();
accessLogPdu.initAttrValue();
accessLogPdu.setAppName(appName);
accessLogPdu.setTraceID(headers.getOrDefault(TRACE_ID,""));
accessLogPdu.setPspanId(Integer.parseInt(headers.getOrDefault(PSPAN_ID,"0")));
accessLogPdu.setSpanId(Integer.parseInt(headers.getOrDefault(SPAN_ID,"0")));
accessLogPdu.setSchema(request.getURI().getScheme());
accessLogPdu.setHostName(NetUtil.getLocalHostName());
accessLogPdu.setUri(requestPath);
accessLogPdu.setTargetServer(route.getId());
accessLogPdu.setRequestIp(IpUtils.getRealIpAddress(exchange.getRequest()));
accessLogPdu.setRequestTime(new Date());
accessLogPdu.setLogTime(new Date());
accessLogPdu.setMethod(request.getMethodValue());
//accessLogPdu.setRequestData(paramStr);
// GatewayLog gatewayLog = new GatewayLog();
// gatewayLog.setSchema(request.getURI().getScheme());
// gatewayLog.setRequestMethod(request.getMethodValue());
// gatewayLog.setRequestPath(requestPath);
// gatewayLog.setTargetServer(route.getId());
// gatewayLog.setRequestTime(new Date());
// gatewayLog.setIp(ipAddress);
MediaType mediaType = request.getHeaders().getContentType();
if (MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(mediaType) || MediaType.APPLICATION_JSON.isCompatibleWith(mediaType)) {
return writeBodyLog(exchange, chain, accessLogPdu);
} else {
return writeBasicLog(exchange, chain, accessLogPdu);
}
}
/**
* form参数形式
*
* @param exchange
* @param chain
* @param accessLog
* @return
*/
private Mono<Void> writeBasicLog(ServerWebExchange exchange, GatewayFilterChain chain, AccessLogPdu accessLog) {
StringBuilder builder = new StringBuilder();
MultiValueMap<String, String> queryParams = exchange.getRequest().getQueryParams();
for (Map.Entry<String, List<String>> entry : queryParams.entrySet()) {
builder.append(entry.getKey()).append("=").append(StringUtils.join(entry.getValue(), ","));
}
accessLog.setRequestData(builder.toString());
//获取响应体
ServerHttpResponseDecorator decoratedResponse = recordResponseLog(exchange, accessLog);
return chain.filter(exchange.mutate().response(decoratedResponse).build())
.then(Mono.fromRunnable(() -> {
// 打印日志 发送
writeAccessLog(accessLog);
}));
}
/**
* 解决 request body 只能读取一次问题,
* 参考: org.springframework.cloud.gateway.filter.factory.rewrite.ModifyRequestBodyGatewayFilterFactory
*
* @param exchange
* @param chain
* @param accessLogPdu
* @return
*/
@SuppressWarnings("unchecked")
private Mono writeBodyLog(ServerWebExchange exchange, GatewayFilterChain chain, AccessLogPdu accessLogPdu) {
ServerRequest serverRequest = ServerRequest.create(exchange, messageReaders);
Mono<String> modifiedBody = serverRequest.bodyToMono(String.class)
.flatMap(body -> {
accessLogPdu.setRequestData(body);
return Mono.just(body);
});
// 通过 BodyInserter 插入 body(支持修改body), 避免 request body 只能获取一次
BodyInserter bodyInserter = BodyInserters.fromPublisher(modifiedBody, String.class);
HttpHeaders headers = new HttpHeaders();
headers.putAll(exchange.getRequest().getHeaders());
// the new content type will be computed by bodyInserter
// and then set in the request decorator
headers.remove(HttpHeaders.CONTENT_LENGTH);
CachedBodyOutputMessage outputMessage = new CachedBodyOutputMessage(exchange, headers);
return bodyInserter.insert(outputMessage, new BodyInserterContext())
.then(Mono.defer(() -> {
// 重新封装请求
ServerHttpRequest decoratedRequest = requestDecorate(exchange, headers, outputMessage);
// 记录响应日志
ServerHttpResponseDecorator decoratedResponse = recordResponseLog(exchange, accessLogPdu);
// 记录普通的
return chain.filter(exchange.mutate().request(decoratedRequest).response(decoratedResponse).build())
.then(Mono.fromRunnable(() -> {
// 打印日志 并发送
writeAccessLog(accessLogPdu);
}));
}));
}
private void writeAccessLog(AccessLogPdu accessLogPdu) {
// log.info("accessLog:{}",JSON.toJSONString(accessLogPdu));
messageProducer.syncAccessSend(accessLogPdu);
}
private Route getGatewayRoute(ServerWebExchange exchange) {
return exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR);
}
/**
* 请求装饰器,重新计算 headers
*
* @param exchange
* @param headers
* @param outputMessage
* @return
*/
private ServerHttpRequestDecorator requestDecorate(ServerWebExchange exchange, HttpHeaders headers,
CachedBodyOutputMessage outputMessage) {
return new ServerHttpRequestDecorator(exchange.getRequest()) {
@Override
public HttpHeaders getHeaders() {
long contentLength = headers.getContentLength();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.putAll(super.getHeaders());
if (contentLength > 0) {
httpHeaders.setContentLength(contentLength);
} else {
// TODO: this causes a 'HTTP/1.1 411 Length Required' // on
// httpbin.org
httpHeaders.set(HttpHeaders.TRANSFER_ENCODING, "chunked");
}
return httpHeaders;
}
@Override
public Flux<DataBuffer> getBody() {
return outputMessage.getBody();
}
};
}
/**
* 记录响应日志
* 通过 DataBufferFactory 解决响应体分段传输问题。
*/
private ServerHttpResponseDecorator recordResponseLog(ServerWebExchange exchange, AccessLogPdu accessLogPdu) {
ServerHttpResponse response = exchange.getResponse();
DataBufferFactory bufferFactory = response.bufferFactory();
return new ServerHttpResponseDecorator(response) {
@Override
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
if (body instanceof Flux) {
Date responseTime = new Date();
// accessLogPdu.setResponseTime(responseTime);
// 计算执行时间
long executeTime = (responseTime.getTime() - accessLogPdu.getRequestTime().getTime());
accessLogPdu.setDuration(executeTime);
// 获取响应类型,如果是 json 就打印
String originalResponseContentType = exchange.getAttribute(ServerWebExchangeUtils.ORIGINAL_RESPONSE_CONTENT_TYPE_ATTR);
if (ObjectUtil.equal(this.getStatusCode(), HttpStatus.OK)
&& StringUtil.isNotBlank(originalResponseContentType)
&& originalResponseContentType.contains("application/json")) {
Flux<? extends DataBuffer> fluxBody = Flux.from(body);
return super.writeWith(fluxBody.buffer().map(dataBuffers -> {
// 合并多个流集合,解决返回体分段传输
DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
DataBuffer join = dataBufferFactory.join(dataBuffers);
byte[] content = new byte[join.readableByteCount()];
join.read(content);
// 释放掉内存
DataBufferUtils.release(join);
String responseResult = new String(content, StandardCharsets.UTF_8);
accessLogPdu.setResponseData(responseResult);
return bufferFactory.wrap(content);
}));
}
}
// if body is not a flux. never got there.
return super.writeWith(body);
}
};
}
}
...@@ -39,7 +39,7 @@ import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.O ...@@ -39,7 +39,7 @@ import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.O
* @date: 2022/8/18 13:48 * @date: 2022/8/18 13:48
* @description: * @description:
**/ **/
@Component //@Component
@Slf4j @Slf4j
public class GatewayResponseFilter implements GlobalFilter, Ordered { public class GatewayResponseFilter implements GlobalFilter, Ordered {
......
...@@ -21,7 +21,7 @@ import reactor.core.publisher.Mono; ...@@ -21,7 +21,7 @@ import reactor.core.publisher.Mono;
* @author: zxfei * @author: zxfei
* @date: 2022/6/20 16:59 * @date: 2022/6/20 16:59
*/ */
@Component //@Component
@Slf4j @Slf4j
public class GlobalCacheRequestFilter implements GlobalFilter, Ordered { public class GlobalCacheRequestFilter implements GlobalFilter, Ordered {
...@@ -51,6 +51,6 @@ public class GlobalCacheRequestFilter implements GlobalFilter, Ordered { ...@@ -51,6 +51,6 @@ public class GlobalCacheRequestFilter implements GlobalFilter, Ordered {
@Override @Override
public int getOrder() { public int getOrder() {
return HIGHEST_PRECEDENCE; return -3;
} }
} }
...@@ -61,57 +61,63 @@ public class GlobalLogFilter implements GlobalFilter, Ordered { ...@@ -61,57 +61,63 @@ public class GlobalLogFilter implements GlobalFilter, Ordered {
private static final String START_TIME = "startTime"; private static final String START_TIME = "startTime";
private static final String TRACE_ID = "traceId"; private static final String TRACE_ID = "traceId";
private static final String SPAN_ID = "spanId";
private static final String PSPAN_ID = "pspanId";
@Override @Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
log.info("GlobalLogFilter:{}",getOrder()); log.info("GlobalLogFilter:{}",getOrder());
MDC.put("startTime", String.valueOf(System.currentTimeMillis()));
//保存请求链路,设置数据追踪id到header中 //保存请求链路,设置数据追踪id到header中
Integer pspanId = 0;
if (!customGatewayProperties.getRequestLog()) { Integer spanId = 1;
return chain.filter(exchange);
}
ServerHttpRequest request = exchange.getRequest();
String path = request.getPath().toString();
String method = request.getMethod().name();
//判断请求类型
String paramStr ="";
if (isJsonRequest(request)) {
//读取请求体后 内容需要重新设置进去
paramStr = resolveBodyFromRequest(request);
} else {
//非json类型,
MultiValueMap<String, String> parameterMap = request.getQueryParams();
paramStr=JSON.toJSONString(parameterMap);
}
String traceId = IdUtil.objectId(); String traceId = IdUtil.objectId();
AccessLogPdu accessLogPdu = new AccessLogPdu();
accessLogPdu.initAttrValue();
accessLogPdu.setAppName(appName);
accessLogPdu.setHostName(NetUtil.getLocalHostName());
accessLogPdu.setLogLevel("INFO");
accessLogPdu.setRequestIp(IpUtils.getRealIpAddress(exchange.getRequest()));
accessLogPdu.setRequestParam(paramStr);
accessLogPdu.setLogTime(new Date());
accessLogPdu.setMethod(method);
accessLogPdu.setUri(path);
accessLogPdu.setTraceID(traceId);
messageProducer.syncAccessSend(accessLogPdu);
exchange.getAttributes().put(START_TIME, System.currentTimeMillis());
ServerHttpRequest newRequest = exchange.getRequest().mutate() ServerHttpRequest newRequest = exchange.getRequest().mutate()
.header(TRACE_ID, traceId) .header(TRACE_ID, traceId)
.header(SPAN_ID, pspanId.toString())
.header(PSPAN_ID, spanId.toString())
.build(); .build();
return chain.filter(exchange.mutate().request(newRequest).build()); return chain.filter(exchange.mutate().request(newRequest).build());
//判断请求类型
// String paramStr ="";
// if (isJsonRequest(request)) {
// //读取请求体后 内容需要重新设置进去
// paramStr = resolveBodyFromRequest(request);
// } else {
// //非json类型,
// MultiValueMap<String, String> parameterMap = request.getQueryParams();
// paramStr=JSON.toJSONString(parameterMap);
// }
// AccessLogPdu accessLogPdu = new AccessLogPdu();
// accessLogPdu.initAttrValue();
// accessLogPdu.setAppName(appName);
// accessLogPdu.setHostName(NetUtil.getLocalHostName());
// accessLogPdu.setLogLevel("INFO");
// accessLogPdu.setRequestIp(IpUtils.getRealIpAddress(exchange.getRequest()));
// accessLogPdu.setRequestData(paramStr);
// accessLogPdu.setLogTime(new Date());
// accessLogPdu.setMethod(method);
// accessLogPdu.setUri(path);
// accessLogPdu.setTraceID(traceId);
// messageProducer.syncAccessSend(accessLogPdu);
//exchange.getAttributes().put(START_TIME, System.currentTimeMillis());
// ServerHttpRequest newRequest = exchange.getRequest().mutate()
// .header(TRACE_ID, traceId)
// .header(TRACE_ID, traceId)
// .header(TRACE_ID, traceId)
// .build();
//
// return chain.filter(exchange.mutate().request(newRequest).build());
} }
@Override @Override
public int getOrder() { public int getOrder() {
return 0; return -3;
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment