diff --git a/cf-framework/cf-common/src/main/java/com/cf/imes/framework/common/enums/TraceConstants.java b/cf-framework/cf-common/src/main/java/com/cf/imes/framework/common/enums/TraceConstants.java new file mode 100644 index 000000000..f376039c2 --- /dev/null +++ b/cf-framework/cf-common/src/main/java/com/cf/imes/framework/common/enums/TraceConstants.java @@ -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"; +} diff --git a/cf-framework/cf-common/src/main/java/com/cf/imes/framework/common/pojo/CommonResult.java b/cf-framework/cf-common/src/main/java/com/cf/imes/framework/common/pojo/CommonResult.java index 79d416ae0..ea847ec33 100644 --- a/cf-framework/cf-common/src/main/java/com/cf/imes/framework/common/pojo/CommonResult.java +++ b/cf-framework/cf-common/src/main/java/com/cf/imes/framework/common/pojo/CommonResult.java @@ -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 implements Serializable { */ private String msg; + /** + * 链路追踪编号 + */ + @JsonInclude(JsonInclude.Include.NON_NULL) + private String traceId; + /** * 将传入的 result 对象,转换成另外一个泛型结果的对象 * @@ -55,6 +66,11 @@ public class CommonResult implements Serializable { CommonResult result = new CommonResult<>(); result.code = code; result.msg = message; + + String traceId = MDC.get(TRACE_ID); + if (StringUtils.hasText(traceId)) { + result.traceId = traceId; + } return result; } diff --git a/cf-framework/cf-spring-boot-starter-monitor/src/main/java/com/cf/imes/framework/tracer/core/filter/TraceFilter.java b/cf-framework/cf-spring-boot-starter-monitor/src/main/java/com/cf/imes/framework/tracer/core/filter/TraceFilter.java index a39af427a..556751060 100644 --- a/cf-framework/cf-spring-boot-starter-monitor/src/main/java/com/cf/imes/framework/tracer/core/filter/TraceFilter.java +++ b/cf-framework/cf-spring-boot-starter-monitor/src/main/java/com/cf/imes/framework/tracer/core/filter/TraceFilter.java @@ -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); + } } } diff --git a/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/async/ChenfengAsyncAutoConfiguration.java b/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/async/ChenfengAsyncAutoConfiguration.java index 607662af3..b3c0f8468 100644 --- a/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/async/ChenfengAsyncAutoConfiguration.java +++ b/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/async/ChenfengAsyncAutoConfiguration.java @@ -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; + } + + }; + } + } diff --git a/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/web/core/handler/GlobalExceptionHandler.java b/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/web/core/handler/GlobalExceptionHandler.java index 3f7ebfd18..a6669623a 100644 --- a/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/web/core/handler/GlobalExceptionHandler.java +++ b/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/web/core/handler/GlobalExceptionHandler.java @@ -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()); } diff --git a/cf-module-infra/cf-module-infra-biz/src/main/java/com/cf/imes/module/infra/job/logger/AccessLogCleanJob.java b/cf-module-infra/cf-module-infra-biz/src/main/java/com/cf/imes/module/infra/job/logger/AccessLogCleanJob.java index ec825523d..3aeaa8804 100644 --- a/cf-module-infra/cf-module-infra-biz/src/main/java/com/cf/imes/module/infra/job/logger/AccessLogCleanJob.java +++ b/cf-module-infra/cf-module-infra-biz/src/main/java/com/cf/imes/module/infra/job/logger/AccessLogCleanJob.java @@ -33,7 +33,7 @@ public class AccessLogCleanJob { /* @XxlJob("accessLogCleanJob")*/ @OrganIgnore - /* @Scheduled(cron = "0 0 * * * ?")*/ + @Scheduled(cron = "0 0 * * * ?") public void execute() { Integer count = apiAccessLogService.cleanAccessLog(JOB_CLEAN_RETAIN_DAY, DELETE_LIMIT); log.info("[execute][定时执行清理访问日志数量 ({}) 个]", count); diff --git a/cf-module-infra/cf-module-infra-biz/src/main/java/com/cf/imes/module/infra/job/logger/ErrorLogCleanJob.java b/cf-module-infra/cf-module-infra-biz/src/main/java/com/cf/imes/module/infra/job/logger/ErrorLogCleanJob.java index 3d98a41b3..9f4b2da2d 100644 --- a/cf-module-infra/cf-module-infra-biz/src/main/java/com/cf/imes/module/infra/job/logger/ErrorLogCleanJob.java +++ b/cf-module-infra/cf-module-infra-biz/src/main/java/com/cf/imes/module/infra/job/logger/ErrorLogCleanJob.java @@ -33,7 +33,7 @@ public class ErrorLogCleanJob { /*@XxlJob("errorLogCleanJob")*/ @OrganIgnore - /*@Scheduled(cron = "0 0 2 * * ?")*/ + @Scheduled(cron = "0 0 2 * * ?") public void execute() { Integer count = apiErrorLogService.cleanErrorLog(JOB_CLEAN_RETAIN_DAY,DELETE_LIMIT); log.info("[execute][定时执行清理错误日志数量 ({}) 个]", count); diff --git a/cf-module-infra/cf-module-infra-biz/src/main/resources/logback-spring.xml b/cf-module-infra/cf-module-infra-biz/src/main/resources/logback-spring.xml index a76c18e07..65860d60a 100644 --- a/cf-module-infra/cf-module-infra-biz/src/main/resources/logback-spring.xml +++ b/cf-module-infra/cf-module-infra-biz/src/main/resources/logback-spring.xml @@ -5,6 +5,7 @@ +       @@ -49,7 +50,7 @@ - ${PATTERN_DEFAULT} + ${ERROR_PATTERN} diff --git a/cf-module-prod-executor/cf-module-prod-executor-biz/src/main/resources/logback-spring.xml b/cf-module-prod-executor/cf-module-prod-executor-biz/src/main/resources/logback-spring.xml index 6eced8a01..5b4c63590 100644 --- a/cf-module-prod-executor/cf-module-prod-executor-biz/src/main/resources/logback-spring.xml +++ b/cf-module-prod-executor/cf-module-prod-executor-biz/src/main/resources/logback-spring.xml @@ -5,6 +5,7 @@ +       @@ -49,7 +50,7 @@ - ${PATTERN_DEFAULT} + ${ERROR_PATTERN} diff --git a/cf-module-prod-plan/cf-module-prod-plan-biz/src/main/resources/logback-spring.xml b/cf-module-prod-plan/cf-module-prod-plan-biz/src/main/resources/logback-spring.xml index 6eced8a01..5b4c63590 100644 --- a/cf-module-prod-plan/cf-module-prod-plan-biz/src/main/resources/logback-spring.xml +++ b/cf-module-prod-plan/cf-module-prod-plan-biz/src/main/resources/logback-spring.xml @@ -5,6 +5,7 @@ +       @@ -49,7 +50,7 @@ - ${PATTERN_DEFAULT} + ${ERROR_PATTERN} diff --git a/cf-module-report/cf-module-report-biz/src/main/resources/logback-spring.xml b/cf-module-report/cf-module-report-biz/src/main/resources/logback-spring.xml index 710e79302..64260a568 100644 --- a/cf-module-report/cf-module-report-biz/src/main/resources/logback-spring.xml +++ b/cf-module-report/cf-module-report-biz/src/main/resources/logback-spring.xml @@ -5,6 +5,7 @@ +       @@ -50,7 +51,7 @@ - ${PATTERN_DEFAULT} + ${ERROR_PATTERN} diff --git a/cf-module-system/cf-module-system-biz/src/main/resources/logback-spring.xml b/cf-module-system/cf-module-system-biz/src/main/resources/logback-spring.xml index a76c18e07..65860d60a 100644 --- a/cf-module-system/cf-module-system-biz/src/main/resources/logback-spring.xml +++ b/cf-module-system/cf-module-system-biz/src/main/resources/logback-spring.xml @@ -5,6 +5,7 @@ +       @@ -49,7 +50,7 @@ - ${PATTERN_DEFAULT} + ${ERROR_PATTERN}