取得領先
VMware 提供培訓和認證,助您加速進步。
瞭解更多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
配置服務的地址。
將 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。