领先一步
VMware 提供培训和认证,助您加速进步。
了解更多Spring Cloud Sleuth 的最后一个次要版本是 3.1。您可以查看 3.1.x 分支以获取最新的提交。该项目的核心已移至 Micrometer Tracing 项目,并且插桩将移至 Micrometer 和所有相应的项目(不再所有插桩都将在单个存储库中完成)。
Spring Cloud Sleuth 为分布式追踪提供了 Spring Boot 自动配置。
Sleuth 配置了您开始所需的一切。这包括追踪数据(span)报告到哪里、保留多少追踪(采样)、是否发送远程字段(baggage)以及追踪哪些库。
具体来说,Spring Cloud Sleuth…
将追踪 ID 和 span ID 添加到 Slf4J MDC,这样您就可以在日志聚合器中提取给定追踪或 span 的所有日志。
对 Spring 应用程序的常见入口和出口点进行插桩(servlet 过滤器、rest 模板、调度操作、消息通道、feign 客户端)。
如果 spring-cloud-sleuth-zipkin 可用,应用程序将通过 HTTP 生成并报告与 Zipkin 兼容的追踪。默认情况下,它将它们发送到 localhost(端口 9411)上的 Zipkin 收集器服务。使用 spring.zipkin.baseUrl 配置服务的位置。
将 Sleuth 添加到您的类路径
Maven
<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>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
</dependencies>
Gradle
buildscript {
dependencies {
classpath "io.spring.gradle:dependency-management-plugin:0.5.2.RELEASE"
}
}
apply plugin: "io.spring.dependency-management"
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${releaseTrainVersion}"
}
}
dependencies {
compile 'org.springframework.cloud:spring-cloud-starter-sleuth'
}
只要 Spring Cloud Sleuth 在类路径上,任何 Spring Boot 应用程序都将生成追踪数据
@SpringBootApplication
@RestController
public class Application {
private static Logger log = LoggerFactory.getLogger(DemoController.class);
@RequestMapping("/")
public String home() {
log.info("Handling home");
return "Hello World";
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
运行此应用程序,然后访问主页。您将看到日志中填充了 traceId 和 spanId。如果此应用程序调用另一个应用程序(例如使用 RestTemplate),它将在头中发送追踪数据,如果接收方是另一个 Sleuth 应用程序,您将看到追踪在那里继续。
与其在处理程序中明确记录请求,不如设置 logging.level.org.springframework.web.servlet.DispatcherServlet=DEBUG
Sleuth 默认为限速采样器。这意味着它每秒将采样多达 1000 个事务。
设置 spring.application.name=bar(例如)以查看服务名称以及追踪和 span ID。