mirror of https://gitee.com/xiaonuobase/snowy
【底座】启动配置增加应用名称,底座增加traceId机制;
parent
a4d45fab44
commit
936ce722e8
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* Copyright [2022] [https://www.xiaonuo.vip]
|
||||
*
|
||||
* Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
|
||||
*
|
||||
* 1.请不要删除和修改根目录下的LICENSE文件。
|
||||
* 2.请不要删除和修改Snowy源码头部的版权声明。
|
||||
* 3.本项目代码可免费商业使用,商业使用请保留源码和相关描述文件的项目出处,作者声明等。
|
||||
* 4.分发源码时候,请注明软件出处 https://www.xiaonuo.vip
|
||||
* 5.不可二次分发开源参与同类竞品,如有想法可联系团队xiaonuobase@qq.com商议合作。
|
||||
* 6.若您的项目无法满足以上几点,需要更多功能代码,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip
|
||||
*/
|
||||
package vip.xiaonuo.common.aspect;
|
||||
|
||||
import ch.qos.logback.classic.pattern.ClassicConverter;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import org.springframework.stereotype.Component;
|
||||
import vip.xiaonuo.common.util.CommonTraceIdUtil;
|
||||
|
||||
/**
|
||||
* 自定义traceId转换器
|
||||
*
|
||||
* @author dongxiayu
|
||||
* @date 2025/1/9 15:36
|
||||
*/
|
||||
@Component
|
||||
public class CustomTraceIdConverter extends ClassicConverter {
|
||||
|
||||
// 使用常量避免重复创建字符串
|
||||
private static final String LEFT_BRACKET = "[";
|
||||
private static final String RIGHT_BRACKET = "]";
|
||||
|
||||
// 缓存StringBuilder以减少对象创建
|
||||
private static final ThreadLocal<StringBuilder> bufferHolder =
|
||||
ThreadLocal.withInitial(() -> new StringBuilder(64));
|
||||
|
||||
@Override
|
||||
public String convert(ILoggingEvent event) {
|
||||
StringBuilder buffer = bufferHolder.get();
|
||||
buffer.setLength(0);
|
||||
String traceId = CommonTraceIdUtil.getTraceId();
|
||||
|
||||
buffer.append(LEFT_BRACKET);
|
||||
if (traceId != null && !traceId.isEmpty()) {
|
||||
buffer.append(traceId);
|
||||
} else {
|
||||
buffer.append(event.getThreadName());
|
||||
}
|
||||
buffer.append(RIGHT_BRACKET);
|
||||
return buffer.toString();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* Copyright [2022] [https://www.xiaonuo.vip]
|
||||
*
|
||||
* Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
|
||||
*
|
||||
* 1.请不要删除和修改根目录下的LICENSE文件。
|
||||
* 2.请不要删除和修改Snowy源码头部的版权声明。
|
||||
* 3.本项目代码可免费商业使用,商业使用请保留源码和相关描述文件的项目出处,作者声明等。
|
||||
* 4.分发源码时候,请注明软件出处 https://www.xiaonuo.vip
|
||||
* 5.不可二次分发开源参与同类竞品,如有想法可联系团队xiaonuobase@qq.com商议合作。
|
||||
* 6.若您的项目无法满足以上几点,需要更多功能代码,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip
|
||||
*/
|
||||
package vip.xiaonuo.common.interceptor;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import vip.xiaonuo.common.util.CommonTraceIdUtil;
|
||||
|
||||
/**
|
||||
* 公共链路追踪拦截器
|
||||
*
|
||||
* @author dongxiayu
|
||||
* @date 2025/1/10 11:42
|
||||
*/
|
||||
public class CommonTraceInterceptor implements HandlerInterceptor {
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
try {
|
||||
CommonTraceIdUtil.getTraceId(request);
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
||||
CommonTraceIdUtil.clear();
|
||||
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
|
||||
}
|
||||
}
|
|
@ -14,6 +14,7 @@ package vip.xiaonuo.common.pojo;
|
|||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Getter;
|
||||
import vip.xiaonuo.common.util.CommonTraceIdUtil;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
|
@ -47,6 +48,9 @@ public class CommonResult<T> implements Serializable{
|
|||
@Schema(description = "返回数据")
|
||||
private T data;
|
||||
|
||||
@Schema(description = "跟踪ID")
|
||||
private String traceId;
|
||||
|
||||
public CommonResult() {
|
||||
}
|
||||
|
||||
|
@ -54,6 +58,7 @@ public class CommonResult<T> implements Serializable{
|
|||
this.setCode(code);
|
||||
this.setMsg(msg);
|
||||
this.setData(data);
|
||||
this.setTraceId(CommonTraceIdUtil.getTraceId());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -64,6 +69,14 @@ public class CommonResult<T> implements Serializable{
|
|||
return this.code;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取traceId
|
||||
* @return traceId
|
||||
*/
|
||||
public String getTraceId() {
|
||||
return this.traceId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 给code赋值,连缀风格
|
||||
* @param code code
|
||||
|
@ -94,6 +107,16 @@ public class CommonResult<T> implements Serializable{
|
|||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 给traceId赋值,连缀风格
|
||||
* @param traceId traceId
|
||||
* @return 对象自身
|
||||
*/
|
||||
public CommonResult<T> setTraceId(String traceId) {
|
||||
this.traceId = traceId;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
// ============================ 构建 ==================================
|
||||
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
* Copyright [2022] [https://www.xiaonuo.vip]
|
||||
*
|
||||
* Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
|
||||
*
|
||||
* 1.请不要删除和修改根目录下的LICENSE文件。
|
||||
* 2.请不要删除和修改Snowy源码头部的版权声明。
|
||||
* 3.本项目代码可免费商业使用,商业使用请保留源码和相关描述文件的项目出处,作者声明等。
|
||||
* 4.分发源码时候,请注明软件出处 https://www.xiaonuo.vip
|
||||
* 5.不可二次分发开源参与同类竞品,如有想法可联系团队xiaonuobase@qq.com商议合作。
|
||||
* 6.若您的项目无法满足以上几点,需要更多功能代码,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip
|
||||
*/
|
||||
package vip.xiaonuo.common.util;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* TraceId工具类
|
||||
*
|
||||
* @author dongxiayu
|
||||
* @date 2025/1/9 17:07
|
||||
*/
|
||||
public class CommonTraceIdUtil {
|
||||
|
||||
public static final String TRACE_ID_STRING = "traceId";
|
||||
|
||||
private static final InheritableThreadLocal<String> TRACE_ID = new InheritableThreadLocal<>();
|
||||
|
||||
public static String generateTraceId(HttpServletRequest request) {
|
||||
String header = request.getHeader(TRACE_ID_STRING);
|
||||
if (header != null) {
|
||||
return header;
|
||||
}
|
||||
return UUID.randomUUID().toString().replace(StrUtil.DASHED, StrUtil.EMPTY);
|
||||
}
|
||||
|
||||
public static String getTraceId() {
|
||||
return TRACE_ID.get();
|
||||
}
|
||||
|
||||
public static String getTraceId(HttpServletRequest request) {
|
||||
String traceId = getTraceId();
|
||||
if (traceId == null) {
|
||||
traceId = generateTraceId(request);
|
||||
setTraceId(traceId);
|
||||
}
|
||||
return traceId;
|
||||
}
|
||||
|
||||
public static void setTraceId(String traceId) {
|
||||
TRACE_ID.set(traceId);
|
||||
}
|
||||
|
||||
public static void clear() {
|
||||
TRACE_ID.remove();
|
||||
}
|
||||
}
|
|
@ -66,6 +66,7 @@ import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|||
import org.springframework.jdbc.support.JdbcUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.ResourceUtils;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import vip.xiaonuo.auth.core.util.StpClientUtil;
|
||||
|
@ -74,6 +75,7 @@ import vip.xiaonuo.common.annotation.CommonWrapper;
|
|||
import vip.xiaonuo.common.cache.CommonCacheOperator;
|
||||
import vip.xiaonuo.common.enums.CommonDeleteFlagEnum;
|
||||
import vip.xiaonuo.common.exception.CommonException;
|
||||
import vip.xiaonuo.common.interceptor.CommonTraceInterceptor;
|
||||
import vip.xiaonuo.common.listener.CommonDataChangeEventCenter;
|
||||
import vip.xiaonuo.common.listener.CommonDataChangeListener;
|
||||
import vip.xiaonuo.common.pojo.CommonResult;
|
||||
|
@ -702,4 +704,14 @@ public class GlobalConfigure implements WebMvcConfigurer {
|
|||
public void registerListenerList(List<CommonDataChangeListener> dataChangeListenerList) {
|
||||
CommonDataChangeEventCenter.registerListenerList(dataChangeListenerList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加应用拦截器
|
||||
* @param registry
|
||||
*/
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
// 注册注解拦截器,并排除不需要注解鉴权的接口地址 (与登录拦截器无关,只是说明哪些接口不需要被拦截器拦截,此处都拦截)
|
||||
registry.addInterceptor(new CommonTraceInterceptor()).addPathPatterns("/**");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
# server configuration
|
||||
#########################################
|
||||
server.port=82
|
||||
spring.application.name=snowy
|
||||
|
||||
#########################################
|
||||
# spring allow-circular-references
|
||||
|
@ -27,9 +28,9 @@ spring.servlet.multipart.max-file-size=100MB
|
|||
|
||||
# mysql
|
||||
spring.datasource.dynamic.datasource.master.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||
spring.datasource.dynamic.datasource.master.url=jdbc:mysql://localhost:3306/snowy?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true&useInformationSchema=true&rewriteBatchedStatements=true
|
||||
spring.datasource.dynamic.datasource.master.url=jdbc:mysql://localhost:3306/snowy3-gitee?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true&useInformationSchema=true&rewriteBatchedStatements=true
|
||||
spring.datasource.dynamic.datasource.master.username=root
|
||||
spring.datasource.dynamic.datasource.master.password=12345678
|
||||
spring.datasource.dynamic.datasource.master.password=123456
|
||||
spring.datasource.dynamic.strict=true
|
||||
|
||||
# postgres
|
||||
|
|
|
@ -4,6 +4,10 @@
|
|||
<!--日志格式应用spring boot默认的格式,也可以自己更改-->
|
||||
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
|
||||
|
||||
<!-- 注册自定义转换器 -->
|
||||
<conversionRule conversionWord="tid" converterClass="vip.xiaonuo.common.aspect.CustomTraceIdConverter" />
|
||||
|
||||
|
||||
<!--定义日志存放的位置,默认存放在项目启动的相对路径的目录-->
|
||||
<springProperty scope="context" name="LOG_PATH" source="log.path" defaultValue="app-log"/>
|
||||
|
||||
|
|
Loading…
Reference in New Issue