Spring Cloud(Finchley.RELEASE版本)微服務學習實踐:6.2全鏈路追蹤監控-Zipkin
環境:
jdk1.8;spring boot2.0.3;spring cloud(Finchley.RELEASE版本);Maven3.3
摘要說明:
Zipkin:Zipkin是一個分散式追蹤系統。它有助於收集解決微服務架構中的延遲問題所需的時序資料。它管理這些資料的收集和查詢。Zipkin的設計基於 Google Dapper實現。應用程式用於向Zipkin報告時序資料。Zipkin使用者介面還提供了一個依賴關係圖,顯示每個應用程式有多少跟蹤請求。如果要解決延遲問題或錯誤,可以根據應用程式,跟蹤長度,註釋或時間戳對所有跟蹤進行篩選或排序。選擇跟蹤後,您可以看到每個跨度所需的總跟蹤時間百分比,從而可以識別問題應用程式。
步驟:
1.建立zipkinServer服務
建立一個新的zipkinServer子專案,搭建一個zipkinServer伺服器;
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.tit</groupId> <artifactId>springCloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>zipkinServer</artifactId> <name>全鏈路追蹤監控</name> <description>zipkinServer</description> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-server</artifactId> <version>2.9.4</version> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-autoconfigure-ui</artifactId> <version>2.9.4</version> </dependency> </dependencies> </project>
2.配置zipkinServer服務
使用@EnableZipkinServer註解開啟zipkin配置:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import zipkin.server.internal.EnableZipkinServer; @SpringBootApplication @EnableZipkinServer public class ZipkinServerApplication { public static void main(String[] args) { SpringApplication.run(ZipkinServerApplication.class, args); } }
配置application.properties
#配置服務及埠(預設9411)
spring.application.name=zipkin-server
server.port=9411
#去除控制檯異常
management.metrics.web.server.auto-time-requests=false
3.配置sleuthRibbon(sleuth服務消費者)和sleuthEurekaDiscovery(服務提供至)子專案
pom.xml新增spring-cloud-starter-zipkin
<!-- 新增sleuth全鏈路追蹤監控依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
配置application.properties
#指定zipkin中心
spring.zipkin.base-url=http://localhost:9411
4.實踐先後啟動
zipkin中心(zipkinServer):zipkin-server(9411)
服務註冊中心(eurekaServer):eureka-server(1001)
服務提供者(sleuthEurekaDiscovery):sleuth-eureka-client(2101)
服務消費者(sleuthRibbon):sleuth-ribbon-consumer(3101)
多次呼叫http://localhost:3101/infoLog-2;
再訪問zipkin中心:http://localhost:9411/zipkin/;點選Find Traces;
點選藍色條可進入詳情:
在客戶端配置spring.application.sleuth.sampler.probability:預設為0.1;表示呼叫10次才會上傳一次;
#指定抽樣比例
spring.sleuth.sampler.probability=0.5
可以觀察日誌true表示上傳成功:
2018-07-02 17:08:09.142 INFO [sleuth-ribbon-consumer,1bb9844c33135a5e,1bb9844c33135a5e,false] 7776 --- [nio-3101-exec-5] c.t.s.s.controller.TestLogController : ------infoLog-2------
2018-07-02 17:08:09.370 INFO [sleuth-ribbon-consumer,a668db0b14877584,a668db0b14877584,false] 7776 --- [nio-3101-exec-7] c.t.s.s.controller.TestLogController : ------infoLog-2------
2018-07-02 17:08:09.638 INFO [sleuth-ribbon-consumer,dd549e99c53cacaf,dd549e99c53cacaf,true] 7776 --- [nio-3101-exec-9] c.t.s.s.controller.TestLogController : ------infoLog-2------
2018-07-02 17:08:26.656 INFO [sleuth-ribbon-consumer,596669c8c06daae0,596669c8c06daae0,true] 7776 --- [nio-3101-exec-2] c.t.s.s.controller.TestLogController : ------infoLog-2------
2018-07-02 17:08:26.869 INFO [sleuth-ribbon-consumer,27e8fdf8cfdbb8d6,27e8fdf8cfdbb8d6,true] 7776 --- [nio-3101-exec-4] c.t.s.s.controller.TestLogController : ------infoLog-2------
2018-07-02 17:08:27.097 INFO [sleuth-ribbon-consumer,6b171dbc2cabde6e,6b171dbc2cabde6e,false] 7776 --- [nio-3101-exec-6] c.t.s.s.controller.TestLogController : ------infoLog-2------
2018-07-02 17:08:27.433 INFO [sleuth-ribbon-consumer,3dd4b2d43d3dbe3b,3dd4b2d43d3dbe3b,true] 7776 --- [nio-3101-exec-8] c.t.s.s.controller.TestLogController : ------infoLog-2------
2018-07-02 17:08:28.079 INFO [sleuth-ribbon-consumer,768078a9de38f453,768078a9de38f453,true] 7776 --- [io-3101-exec-10] c.t.s.s.controller.TestLogController : ------infoLog-2------
2018-07-02 17:08:28.436 INFO [sleuth-ribbon-consumer,ba9111b2d767a737,ba9111b2d767a737,true] 7776 --- [nio-3101-exec-2] c.t.s.s.controller.TestLogController : ------infoLog-2------
2018-07-02 17:08:28.934 INFO [sleuth-ribbon-consumer,fb2ef82867df9c71,fb2ef82867df9c71,false] 7776 --- [nio-3101-exec-4] c.t.s.s.controller.TestLogController : ------infoLog-2------
6.原始碼地址