前言:隨着微服務系統的增加,服務之間的調用關系變得會非常復雜,這給運維以及排查問題帶來了很大的麻煩,這時服務調用監控就顯得非常重要了。spring cloud sleuth實現了對分布式服務的監控解決方案。
前情回顧請參考:
Spring Cloud 微服務二:API網關spring cloud zuul
Spring Cloud 微服務三: API網關Spring cloud gateway
Spring Cloud 微服務四:熔斷器Spring cloud hystrix
Spring Cloud 微服務五:Spring cloud gateway限流
- 概覽 Spring cloud sleuth實現了分布式系統的鏈路監控,用戶可以通過可視化、交互式頁面查看服務調用情況,並能夠自動更關心服務調用狀態
- sleuth集成zipkin
- 搭建zipkin服務,可以通過docker或者jar包的方式啟動,具體可參考:https://github.com/openzipkin.本章主要使用spring的方式啟動。首先新建一個module sleuth-server,在pom中引入以下依賴,版本我使用的與springboot 2.0.8.RELEASE相對應的2.11.7版本,該版本已經在父工程中定義。
<artifactId>sleuth-server</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-server</artifactId> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-autoconfigure-ui</artifactId> </dependency> </dependencies>
- 在啟動類添加@EnableZipkinServer注解,表示該工程是一個服務器
@SpringBootApplication @EnableZipkinServer public class SleuthServerApplication { public static void main(String[] args) { SpringApplication.run(SleuthServerApplication.class, args); } }
- 配置application.yml,加入zipkin配置
server: port: 9411 debug: true spring: application: name: sleuth-server management: metrics: web: server: auto-time-requests: false
- 啟動sleuth-server,訪問http://localhost:9411進入主界面
- 接下來配置user-consumer,在該工程的pom文件中添加zipkin依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency>
- application.yml中添加sleuth以及zipkin配置
zipkin: base-url: http://localhost:9411/ sleuth: sampler: probability: 1.0
- 同樣配置user-service
- 啟動user-service,user-consumer,訪問服務,會在zipin頁面上查詢到。
- 搭建zipkin服務,可以通過docker或者jar包的方式啟動,具體可參考:https://github.com/openzipkin.本章主要使用spring的方式啟動。首先新建一個module sleuth-server,在pom中引入以下依賴,版本我使用的與springboot 2.0.8.RELEASE相對應的2.11.7版本,該版本已經在父工程中定義。