当前位置:网站首页>Sleuth + Zipkin link tracking
Sleuth + Zipkin link tracking
2022-04-21 19:06:00 【Xiaoqi 7】
List of articles
Sleuth brief introduction
Sleuth yes Spring Cloud One of the components of , It's for Spring Cloud A distributed tracking solution is implemented , compatible Zipkin,HTrace And other log based tracking systems , for example ELK(Elasticsearch 、Logstash、 Kibana).
Related terms
Sleuth Introduced many Dapper Terms in :
-
Span: Basic unit of work . Whether it's sending a RPC Or to RPC Sending a response is a Span. every last Span Through one 64 position ID To uniquely identify , And through another 64 position ID Yes Span Where Trace Unique identification .
Span Be able to start and stop , They keep tracking their time information , When you create a Span, You have to stop it at some point in the future . Tips : Start a Trace The initialization Span It's called Root Span , its Span ID and Trace Id identical . -
TraceBy a series of Span A tree structure of . for example , If you want to perform a distributed big data storage operation , This Trace It may be up to you PUT Request to form . -
Annotation: Used to record the existence of an event in time . By introducing Brave library , We don't have to set up a series of special events , So that Zipkin Be able to know who the client and server are 、 Where did the request start 、 Where does it end . For the purpose of learning , Let's list these events here :cs (Client Sent)- Client initiates a request , This annotation indicates a Span The beginning of .
sr (Server Received)- The server receives the request and begins to process it , If you use sr Timestamp minus cs The timestamp shows how many network delays there are .
ss(Server Sent)- Comment request processing completed ( The response was sent to the client ), If you use ss Timestamp minus sr
The time stamp can get the time spent by the server processing the request .
cr(Client Received)- Heralded a Span The end of , The client successfully received the response from the server , If you use cr Timestamp minus cs
The time stamp can get the whole time that the client needs to get the response from the server .
The following figure shows the... In a system Span and Trace It looks like :

Use Sleuth+zipkin
Introduce dependencies
The premise is that SpringCloud Of Demo
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
To configure
# Collection type
spring.zipkin.sender.type=web
# zipkin The address of the server
spring.zipkin.base-url=http://localhost:9411/
# Set sampling collection to 100%, The default is 0.1, namely 10%
spring.sleuth.sampler.probability=1
Be careful :
spring-cloud-sleuth-stream Expired and incompatible with these targets . If you want to use RabbitMQ perhaps Kafka replace HTTP , We need to introduce spring-rabbit perhaps spring-kafka rely on . The default target name is zipkin . If you're using Kafka , You have to set the corresponding spring.zipkin.sender.type attribute
Download and enable zipkin
Download address :
https://search.maven.org/remote_content?g=io.zipkin.java&a=zipkin-server&v=LATEST&c=exec
Start with the following command Zipkin:
java -jar zipkin-server-2.12.2-exec.jar
visit http://localhost:9411/zipkin

Start the service and test
View the log after sending the message :

Sleuth take Trace Id and Span Id Add to Slf4J MDC(Mapped Diagnostic Context) And printed in the log , such , You can extract any given from the log aggregator Trace perhaps Span All the logs of .
Next , Focus on the explanation of [appname,traceId,spanId,exportable] The meaning of each part :
appname: The name of the application that logs , namelyspring.application.nameValue ;traceId:SleuthUnique data generated for a request linkID, OneTraceCan contain more than oneSpan;spanId: The basic unit of work of the request link , Represents the occurrence of a specific operation , for example : Send aHttprequest ;exportable: Do you need to export logs toZipkin;
Sleuth It provides an abstraction of common distributed link tracking data models :Trace、Span、Annotation And key value Annotation.Spring-Cloud-Sleuth Although based on htrace, But with Zipkin(dapper) Also compatible .
Sleuth Record time information to help with delay analysis . By using sleuth, You can find out the cause of the delay in your application .
When spring-cloud-sleuth-zipkin Included in classpath In the middle of the day , Generate and collect applications zipkin Compatible tracking records . By default , Will pass HTTP Send them to the local host ( port 9411) Upper Zipkin The server . You can set spring.zipkin.baseurl To configure the address of the service .
If you rely on spring-rabbit, Then the application will send the tracking record to Rabbit MQ agent , instead of HTTP.
If you rely on spring-kafka, And set up spring.zipkin.sender.type:kafka, Then the application will send the tracking record to Kafka Agency, not HTTP.
Be careful
-
If you're using
Zipkin, Please setspring.sleuth.sampler.probabilityTo configure the export Span Probability ( The default value is :0.1, namely10%). otherwise , You may think thatSleuthIt doesn't work , Because it omits someSpan. -
If you're using
SLF4J,TraceandSpanThe tracking record of will be recorded to by defaultMDC, So users of the log can immediately see . But if you use other logging systems , You also need to set the print format of the log to see the same results :
logging.pattern.level = %5p [${spring.zipkin.service.name:${spring.application.name:-}},%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-Span-Export:-}]
View the results after the request is sent :

Reference blog :https://blog.csdn.net/pengjunlee/article/details/87797969
版权声明
本文为[Xiaoqi 7]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211900323412.html
边栏推荐
- APM industry awareness series - eight - 25 advantages of Devops
- 哪个期货交易平台最安全呢?我该怎么选择?
- el-tabs中套用el-tooltip(气泡文字提示框)
- 2022.04.21(LC_56_合并区间)
- AlwaysInstallElevated提权
- APM industry awareness series - II
- 深度学习多机多卡batchsize和学习率的关系
- JVM专题(十一)-垃圾回收(二)
- Research Report on the development of auction industry in 2021
- 【C语言进阶】⑥函数指针详解
猜你喜欢
随机推荐
Alwaysinstallelevated
How to classify cosmetics in the management system?
SIGIR'22 "Ali" metacvr: meta learning alleviates the problem of data distribution fluctuation in small-scale recommendation
Svg series - 2, draw some shapes
How to set the TS slice file size for the new kernel version of easycvr?
Elementary mathematical modeling problem
Database advanced learning: storage engine
DVWA-Brute Force
PAT (Advanced Level) 1096——Consecutive连续的
Sleuth+Zipkin链路追踪
uniapp 运行到内置浏览器,怎么打开开发者工具
MKL库矩阵乘法
改进模型速度/精度的工程方法
"Actual combat" realizes linear regression with tensorflow
Webrtc video cannot be played. How to add UDP hole drilling program in easycvr?
Database advanced learning: index classification and creation syntax
U-Net网络变形综述
哪个期货交易平台最安全呢?我该怎么选择?
Wide application of medical robot in AI field
使用MCUXpresso开发RT1060(1)——驱动RGB接口LCD









