springcloud项目微服务日志链路追踪

解决问题:开发排查系统问题用得最多的手段就是查看系统日志,在分布式环境中一般使用ELK来统一收集日志,但是在并发大时使用日志定位问题还是比较麻烦,无法追踪整条线路日志

实现的流程图如图所示:








1、在springcloud项目中pom.xml中添加以下依赖
 

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>

该Sleuth会自动创建traceId,以及会在各个微服务之间保持每次请求保持一个traceId,不需要使用程序在网关生成traceId,也不需要从网关将traceId传递到其他微服务中,这些工作服务链路追踪(Spring Cloud Sleuth)已经帮我们实现了

2、在logback-spring.xml中配置traceId占位符
上篇文章中已经给大家看过了配置方式
<property name="log.pattern" value="%d{yyyy-MM-dd HH:mm:ss:SSS} %-5level  %thread [%X{traceId}] %logger %msg%n"/>

3、在网关服务gateway中添加请求日志,如图所示:



4、在业务服务端question-spell-words代码中在Controller添加日志




5、将微服务通过docker部署上去后将日志采集到阿里云日志服务中
以前有博客说明操作

6、使用postman进行请求

最终展示如下:
路由网关gateway日志中心,如图所示:



通过路由网关转发到question-spell-words微服务的日志中心,如图所示:

Guess you like

Origin blog.csdn.net/qq_39291929/article/details/116850004