spring cloud Sleuth 使用笔记
git 源码:https://github.com/spring-cloud/spring-cloud-sleuth
1.traceId 和spanId生成机制
类brave.tracer:
brave.internal.Platform
最终发现生成id机制是:
java.util.concurrent.ThreadLocalRandom.current().nextLong();
是一个long类型的非零随机数(64位二进制,-9223372036854775808 ~ 9223372036854775807之间的非零数值。)
向下游传播和MDC中都是被转成16位十六进制字符串(负正数以无符号处理,不足16位左边补0)。
关于ThreadLocalRandom,可以参考别人的博客《并发包中ThreadLocalRandom类原理剖析》:https://www.jianshu.com/p/9c2198586f9b
2.sleuth使用
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${release.train.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
Spring Cloud Sleuth 2.0.0以后添加了很多组件的支持,比如dubbo,apache httpclient,kafka,rabbitmq等,具体更新的内容有:
Rewritten using Brave #829, migration guide https://github.com/spring-cloud/spring-cloud-sleuth/wiki/Spring-Cloud-Sleuth-2.0-Migration-Guide
Removed the sleuth-stream #555 and zipkin-stream #727 dependencies. Spans via messaging can be only sent to Zipkin via native Zipkin dependencies.
spring.zipkin.sender.type=kafka needs to explicitly be set to send spans over Kafka #985, #1013
Added WebClient.Builder support #779
Trace tags account for parametrized URL #802