- 引入依赖
<!--引入链路追踪-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
<version>1.3.0.RELEASE</version>
</dependency>
说明:注意版本号, 比如你用的boot是1.5.9的,那引入 2.1.1.RELEASE 的sleuth是跑不起来的。1.3.0的就可以跑起来。
- 修改logback配置文件根据需求加入traceid 和spanid
<!-- 控制台输出 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %ip [%thread] %-5level %logger{50} [%X{X-B3-TraceId:-}] - %msg%n</pattern>
</encoder>
</appender>
说明:这里的 [%X{X-B3-TraceId:-}] 就是traceID, spanid为:%X{X-B3-SpanId:-} ,和logback整合其实原理还是用了logback的 MDC。
- 效果:
2019-04-04 10:30:04.298 19.19.90.84 [http-nio-8081-exec-1] INFO com.jccfc.station.web.interceptor.LogRecordAspect [604e8b49ac506ed5] - [http://localhost:8081/example/getExampleByIdAndName] 执行耗时 : 574ms
2019-04-04 10:30:04.340 19.19.90.84 [http-nio-8081-exec-1] TRACE org.apache.shiro.util.ThreadContext [604e8b49ac506ed5] - get() - in thread [http-nio-8081-exec-1]
2019-04-04 10:30:04.340 19.19.90.84 [http-nio-8081-exec-1] TRACE org.apache.shiro.util.ThreadContext [604e8b49ac506ed5] - Retrieved value of type [org.apache.shiro.web.subject.support.WebDelegatingSubject] for key [org.apache.shiro.util.ThreadContext_SUBJECT_KEY] bound to thread [http-nio-8081-exec-1]
2019-04-04 10:30:04.340 19.19.90.84 [http-nio-8081-exec-1] TRACE org.apache.shiro.subject.support.DelegatingSubject [604e8b49ac506ed5] - attempting to get session; create = false; session is null = true; session has id = false
2019-04-04 10:30:04.342 19.19.90.84 [http-nio-8081-exec-1] TRACE org.apache.shiro.web.servlet.AdviceFilter [604e8b49ac506ed5] - Successfully invoked postHandle method
2019-04-04 10:30:04.342 19.19.90.84 [http-nio-8081-exec-1] TRACE org.apache.shiro.web.servlet.AdviceFilter [604e8b49ac506ed5] - Successfully invoked afterCompletion method.
这里的 604e8b49ac506ed5 就是打印出来的traceid
网友评论