mirror of
http://192.168.1.205:9980/cf_devdept2/cf_imes_server.git
synced 2026-08-12 21:02:08 +08:00
1、新增tracefilter中的trace-id,服务日志mdc打印和请求响应增加trace-id;2、恢复access_api_log和access_error_log的清理定时任务;3、补充服务中的线程池使用TransmittableThreadLocal;
This commit is contained in:
+14
@@ -0,0 +1,14 @@
|
||||
package com.cf.imes.framework.common.enums;
|
||||
|
||||
/**
|
||||
* 系统跟踪常量
|
||||
*
|
||||
* @author Gqr
|
||||
* @since 2025/7/7 10:30
|
||||
*/
|
||||
public class TraceConstants {
|
||||
/**
|
||||
* 链路追踪编号
|
||||
*/
|
||||
public static final String TRACE_ID = "trace-id";
|
||||
}
|
||||
+16
@@ -4,14 +4,19 @@ import com.cf.imes.framework.common.exception.ErrorCode;
|
||||
import com.cf.imes.framework.common.exception.ServiceException;
|
||||
import com.cf.imes.framework.common.exception.enums.GlobalErrorCodeConstants;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import lombok.Data;
|
||||
import org.slf4j.MDC;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
import java.util.Objects;
|
||||
|
||||
import static com.cf.imes.framework.common.enums.TraceConstants.TRACE_ID;
|
||||
|
||||
/**
|
||||
* 通用返回
|
||||
*
|
||||
@@ -37,6 +42,12 @@ public class CommonResult<T> implements Serializable {
|
||||
*/
|
||||
private String msg;
|
||||
|
||||
/**
|
||||
* 链路追踪编号
|
||||
*/
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
private String traceId;
|
||||
|
||||
/**
|
||||
* 将传入的 result 对象,转换成另外一个泛型结果的对象
|
||||
*
|
||||
@@ -55,6 +66,11 @@ public class CommonResult<T> implements Serializable {
|
||||
CommonResult<T> result = new CommonResult<>();
|
||||
result.code = code;
|
||||
result.msg = message;
|
||||
|
||||
String traceId = MDC.get(TRACE_ID);
|
||||
if (StringUtils.hasText(traceId)) {
|
||||
result.traceId = traceId;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
+15
-11
@@ -1,6 +1,6 @@
|
||||
package com.cf.imes.framework.tracer.core.filter;
|
||||
|
||||
import com.cf.imes.framework.common.util.monitor.TracerUtils;
|
||||
import org.slf4j.MDC;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
|
||||
import javax.servlet.FilterChain;
|
||||
@@ -8,6 +8,9 @@ import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
import static com.cf.imes.framework.common.enums.TraceConstants.TRACE_ID;
|
||||
|
||||
/**
|
||||
* Trace 过滤器,打印 traceId 到 header 中返回
|
||||
@@ -15,19 +18,20 @@ import java.io.IOException;
|
||||
* @author 晨丰科技
|
||||
*/
|
||||
public class TraceFilter extends OncePerRequestFilter {
|
||||
|
||||
/**
|
||||
* Header 名 - 链路追踪编号
|
||||
*/
|
||||
private static final String HEADER_NAME_TRACE_ID = "trace-id";
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
|
||||
throws IOException, ServletException {
|
||||
// 设置响应 traceId
|
||||
response.addHeader(HEADER_NAME_TRACE_ID, TracerUtils.getTraceId());
|
||||
// 继续过滤
|
||||
chain.doFilter(request, response);
|
||||
String traceId = UUID.randomUUID().toString();
|
||||
try {
|
||||
// 设置日志跟踪ID
|
||||
MDC.put(TRACE_ID, traceId);
|
||||
// 继续过滤
|
||||
chain.doFilter(request, response);
|
||||
} finally {
|
||||
System.out.println("traceId: " + traceId);
|
||||
// 清理跟踪ID
|
||||
MDC.remove(TRACE_ID);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+24
@@ -1,7 +1,12 @@
|
||||
package com.cf.imes.framework.async;
|
||||
|
||||
import com.alibaba.ttl.TtlRunnable;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
|
||||
/**
|
||||
* 异步任务 Configuration
|
||||
@@ -9,4 +14,23 @@ import org.springframework.scheduling.annotation.EnableAsync;
|
||||
@AutoConfiguration
|
||||
@EnableAsync
|
||||
public class ChenfengAsyncAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public BeanPostProcessor threadPoolTaskExecutorBeanPostProcessor() {
|
||||
return new BeanPostProcessor() {
|
||||
|
||||
@Override
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
||||
if (!(bean instanceof ThreadPoolTaskExecutor)) {
|
||||
return bean;
|
||||
}
|
||||
// 修改提交的任务,接入 TransmittableThreadLocal
|
||||
ThreadPoolTaskExecutor executor = (ThreadPoolTaskExecutor) bean;
|
||||
executor.setTaskDecorator(TtlRunnable::get);
|
||||
return executor;
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+3
-3
@@ -257,9 +257,9 @@ public class GlobalExceptionHandler {
|
||||
public CommonResult serviceExceptionHandler(ServiceException ex) {
|
||||
// 系统异常输出堆栈
|
||||
if(INTERNAL_SERVER_ERROR.getCode().equals(ex.getCode())) {
|
||||
log.info("[serviceExceptionHandler]", ex);
|
||||
log.error("[serviceExceptionHandler]", ex);
|
||||
} else {
|
||||
log.info(String.format("==========[serviceExceptionHandler]==========,%s:%s", ex.getCode(), ex.getMessage()));
|
||||
log.error(String.format("==========[serviceExceptionHandler]==========,%s:%s", ex.getCode(), ex.getMessage()));
|
||||
}
|
||||
return CommonResult.error(ex.getCode(), ex.getMessage());
|
||||
}
|
||||
@@ -272,7 +272,7 @@ public class GlobalExceptionHandler {
|
||||
*/
|
||||
@ExceptionHandler(value = PayException.class)
|
||||
public CommonResult payExceptionHandler(PayException ex) {
|
||||
log.info(String.format("==========[payExceptionHandler]==========,%s", ex.getMessage()));
|
||||
log.error(String.format("==========[payExceptionHandler]==========,%s", ex));
|
||||
return CommonResult.error(ex.getCode(), ex.getMessage());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user