Spring Cloud SleuthSpring Cloud Sleuth3.1.9

Spring Cloud Sleuth 的最后一个次要版本是 3.1。您可以查看 3.1.x 分支以获取最新的提交。该项目的核心已移至 Micrometer Tracing 项目,并且其埋点(instrumentations)将移至 Micrometer 和所有相关项目(不再将所有埋点集中在一个仓库中完成)。

概述

Spring Cloud Sleuth 为分布式跟踪提供了 Spring Boot 自动配置。

特性

Sleuth 配置了开始所需的一切。这包括跟踪数据(span)报告到哪里、保留多少跟踪(采样)、是否发送远程字段(baggage),以及跟踪哪些库。

具体来说,Spring Cloud Sleuth...​

  • 将 trace 和 span id 添加到 Slf4J MDC 中,这样您就可以在日志聚合器中提取来自特定跟踪或 span 的所有日志。

  • 检测 Spring 应用中常见的入口点和出口点(servlet filter、rest template、scheduled actions、message channels、feign client)。

  • 如果 spring-cloud-sleuth-zipkin 可用,应用将通过 HTTP 生成并报告兼容 Zipkin 的跟踪数据。默认情况下,它将跟踪数据发送到 localhost 上的 Zipkin 收集服务(端口 9411)。使用 spring.zipkin.baseUrl 配置服务的地址。

Spring Boot 配置

将 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(例如)以查看服务名称以及 trace 和 span id。

Spring Initializr

快速入门您的项目

取得领先

VMware 提供培训和认证,助您加速进步。

了解更多

获取支持

Tanzu Spring 通过一个简单的订阅提供对 OpenJDK™、Spring 和 Apache Tomcat® 的支持和二进制文件。

了解更多

近期活动

查看 Spring 社区的所有近期活动。

查看全部