Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
zlt2000
microservices-platform
提交
e9004eb8
microservices-platform
项目概览
zlt2000
/
microservices-platform
9 个月 前同步成功
通知
16
Star
4
Fork
3
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
microservices-platform
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
e9004eb8
编写于
1月 27, 2022
作者:
zlt2000
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
spanId生成规则优化
上级
3dd2dd02
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
28 addition
and
18 deletion
+28
-18
zlt-commons/zlt-log-spring-boot-starter/src/main/java/com/central/log/trace/DubboTraceFilter.java
...src/main/java/com/central/log/trace/DubboTraceFilter.java
+1
-2
zlt-commons/zlt-log-spring-boot-starter/src/main/java/com/central/log/trace/FeignTraceConfig.java
...src/main/java/com/central/log/trace/FeignTraceConfig.java
+3
-4
zlt-commons/zlt-log-spring-boot-starter/src/main/java/com/central/log/trace/MDCTraceUtils.java
...er/src/main/java/com/central/log/trace/MDCTraceUtils.java
+19
-7
zlt-commons/zlt-log-spring-boot-starter/src/main/java/com/central/log/trace/WebTraceFilter.java
...r/src/main/java/com/central/log/trace/WebTraceFilter.java
+2
-2
zlt-commons/zlt-log-spring-boot-starter/src/main/resources/logback-spring.xml
...spring-boot-starter/src/main/resources/logback-spring.xml
+2
-2
zlt-gateway/sc-gateway/src/main/java/com/central/gateway/filter/TraceFilter.java
...src/main/java/com/central/gateway/filter/TraceFilter.java
+1
-1
未找到文件。
zlt-commons/zlt-log-spring-boot-starter/src/main/java/com/central/log/trace/DubboTraceFilter.java
浏览文件 @
e9004eb8
...
...
@@ -34,9 +34,8 @@ public class DubboTraceFilter implements Filter {
}
else
{
//服务消费者逻辑
String
traceId
=
MDCTraceUtils
.
getTraceId
();
if
(
StrUtil
.
isNotEmpty
(
traceId
))
{
String
spanId
=
MDCTraceUtils
.
getSpanId
();
invocation
.
setAttachment
(
MDCTraceUtils
.
KEY_TRACE_ID
,
traceId
);
invocation
.
setAttachment
(
MDCTraceUtils
.
KEY_SPAN_ID
,
spanId
);
invocation
.
setAttachment
(
MDCTraceUtils
.
KEY_SPAN_ID
,
MDCTraceUtils
.
getNextSpanId
()
);
}
}
try
{
...
...
zlt-commons/zlt-log-spring-boot-starter/src/main/java/com/central/log/trace/FeignTraceConfig.java
浏览文件 @
e9004eb8
package
com.central.log.trace
;
import
cn.hutool.core.util.StrUtil
;
import
com.central.log.properties.TraceProperties
;
import
feign.RequestInterceptor
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.util.StringUtils
;
import
javax.annotation.Resource
;
...
...
@@ -30,10 +30,9 @@ public class FeignTraceConfig {
if
(
traceProperties
.
getEnable
())
{
//传递日志traceId
String
traceId
=
MDCTraceUtils
.
getTraceId
();
if
(!
StringUtils
.
isEmpty
(
traceId
))
{
String
spanId
=
MDCTraceUtils
.
getSpanId
();
if
(
StrUtil
.
isNotEmpty
(
traceId
))
{
template
.
header
(
MDCTraceUtils
.
TRACE_ID_HEADER
,
traceId
);
template
.
header
(
MDCTraceUtils
.
SPAN_ID_HEADER
,
spanId
);
template
.
header
(
MDCTraceUtils
.
SPAN_ID_HEADER
,
MDCTraceUtils
.
getNextSpanId
()
);
}
}
};
...
...
zlt-commons/zlt-log-spring-boot-starter/src/main/java/com/central/log/trace/MDCTraceUtils.java
浏览文件 @
e9004eb8
package
com.central.log.trace
;
import
cn.hutool.core.util.IdUtil
;
import
cn.hutool.core.util.RandomUtil
;
import
cn.hutool.core.util.StrUtil
;
import
com.alibaba.ttl.TransmittableThreadLocal
;
import
org.slf4j.MDC
;
import
java.util.
UUID
;
import
java.util.
concurrent.atomic.AtomicInteger
;
/**
* 日志追踪工具类
...
...
@@ -47,13 +48,16 @@ public class MDCTraceUtils {
*/
public
static
final
int
FILTER_ORDER
=
-
1
;
private
static
final
TransmittableThreadLocal
<
AtomicInteger
>
spanNumber
=
new
TransmittableThreadLocal
<>();
/**
* 创建traceId并赋值MDC
*/
public
static
void
addTrace
()
{
String
traceId
=
createTraceId
();
MDC
.
put
(
KEY_TRACE_ID
,
traceId
);
MDC
.
put
(
KEY_SPAN_ID
,
traceId
);
MDC
.
put
(
KEY_SPAN_ID
,
"0"
);
initSpanNumber
();
}
/**
...
...
@@ -61,8 +65,8 @@ public class MDCTraceUtils {
*/
public
static
void
putTrace
(
String
traceId
,
String
spanId
)
{
MDC
.
put
(
KEY_TRACE_ID
,
traceId
);
MDC
.
put
(
KEY_
PARENT
_ID
,
spanId
);
MDC
.
put
(
KEY_SPAN_ID
,
createTraceId
()
);
MDC
.
put
(
KEY_
SPAN
_ID
,
spanId
);
initSpanNumber
(
);
}
/**
...
...
@@ -84,13 +88,21 @@ public class MDCTraceUtils {
public
static
void
removeTrace
()
{
MDC
.
remove
(
KEY_TRACE_ID
);
MDC
.
remove
(
KEY_SPAN_ID
);
MDC
.
remove
(
KEY_PARENT_ID
);
spanNumber
.
remove
(
);
}
/**
* 创建traceId
*/
public
static
String
createTraceId
()
{
return
RandomUtil
.
randomString
(
16
);
return
IdUtil
.
getSnowflake
().
nextIdStr
();
}
public
static
String
getNextSpanId
()
{
return
StrUtil
.
format
(
"{}.{}"
,
getSpanId
(),
spanNumber
.
get
().
incrementAndGet
());
}
private
static
void
initSpanNumber
()
{
spanNumber
.
set
(
new
AtomicInteger
(
0
));
}
}
zlt-commons/zlt-log-spring-boot-starter/src/main/java/com/central/log/trace/WebTraceFilter.java
浏览文件 @
e9004eb8
package
com.central.log.trace
;
import
cn.hutool.core.util.StrUtil
;
import
com.central.log.properties.TraceProperties
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
import
org.springframework.core.annotation.Order
;
import
org.springframework.stereotype.Component
;
import
org.springframework.util.StringUtils
;
import
org.springframework.web.filter.OncePerRequestFilter
;
import
javax.annotation.Resource
;
...
...
@@ -41,7 +41,7 @@ public class WebTraceFilter extends OncePerRequestFilter {
try
{
String
traceId
=
request
.
getHeader
(
MDCTraceUtils
.
TRACE_ID_HEADER
);
String
spanId
=
request
.
getHeader
(
MDCTraceUtils
.
SPAN_ID_HEADER
);
if
(
Str
ingUtils
.
isEmpty
(
traceId
))
{
if
(
Str
Util
.
isEmpty
(
traceId
))
{
MDCTraceUtils
.
addTrace
();
}
else
{
MDCTraceUtils
.
putTrace
(
traceId
,
spanId
);
...
...
zlt-commons/zlt-log-spring-boot-starter/src/main/resources/logback-spring.xml
浏览文件 @
e9004eb8
...
...
@@ -18,8 +18,8 @@
<!-- 彩色日志格式 -->
<property
name=
"CONSOLE_LOG_PATTERN"
value=
"[${APP_NAME}:${ServerIP}:${ServerPort}] %clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%level){blue} %clr(${PID}){magenta} %clr([%X{traceId}
,%X{spanId},%X{parent
Id}]){yellow} %clr([%thread]){orange} %clr(%-40.40logger{39}){cyan} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"
/>
<property
name=
"CONSOLE_LOG_PATTERN_NO_COLOR"
value=
"[${APP_NAME}:${ServerIP}:${ServerPort}] %d{yyyy-MM-dd HH:mm:ss.SSS} %level ${PID} [%X{traceId}
,%X{spanId},%X{parent
Id}] [%thread] %-40.40logger{39} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"
/>
value=
"[${APP_NAME}:${ServerIP}:${ServerPort}] %clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%level){blue} %clr(${PID}){magenta} %clr([%X{traceId}
-%X{span
Id}]){yellow} %clr([%thread]){orange} %clr(%-40.40logger{39}){cyan} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"
/>
<property
name=
"CONSOLE_LOG_PATTERN_NO_COLOR"
value=
"[${APP_NAME}:${ServerIP}:${ServerPort}] %d{yyyy-MM-dd HH:mm:ss.SSS} %level ${PID} [%X{traceId}
-%X{span
Id}] [%thread] %-40.40logger{39} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"
/>
<!-- 控制台日志 -->
<appender
name=
"StdoutAppender"
class=
"ch.qos.logback.core.ConsoleAppender"
>
...
...
zlt-gateway/sc-gateway/src/main/java/com/central/gateway/filter/TraceFilter.java
浏览文件 @
e9004eb8
...
...
@@ -34,7 +34,7 @@ public class TraceFilter implements GlobalFilter, Ordered {
ServerHttpRequest
serverHttpRequest
=
exchange
.
getRequest
().
mutate
()
.
headers
(
h
->
{
h
.
add
(
MDCTraceUtils
.
TRACE_ID_HEADER
,
MDCTraceUtils
.
getTraceId
());
h
.
add
(
MDCTraceUtils
.
SPAN_ID_HEADER
,
MDCTraceUtils
.
getSpanId
());
h
.
add
(
MDCTraceUtils
.
SPAN_ID_HEADER
,
MDCTraceUtils
.
get
Next
SpanId
());
})
.
build
();
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录