每天學點SpringCloud(十二):Zipkin全鏈路監控
Zipkin是SpringCloud官方推薦的一款分散式鏈路監控的元件,使用它我們可以得知每一個請求所經過的節點以及耗時等資訊,並且它對程式碼無任何侵入,我們先來看一下Zipkin給我們提供的UI介面都是提供了哪些資訊。
- zipkin首頁為我們提供了對於呼叫鏈路的搜尋查詢及展示的功能
- 第二個選項卡里提供了歷史資料的匯入功能
- 第三個選項卡里展示了各個微服務之間的關係
- 我們再次回到首頁,我們點開一個呼叫鏈路之後就會看到此次鏈路呼叫的詳情
- 現在我們點開詳情中的一個service,可以看到此次呼叫在這個微服務中的詳細資訊。
如何使用Zipkin
雖然在SpringBoot2.0以後官方不推薦我們自定義Zipkin服務端,而是使用官方提供的jar包。但是作為開發者來說,如果不能去看一看原始碼,修改一些自定義的配置的話就好像生命掌握在別人手裡一樣,哪天碰到一個莫名奇妙的bug可就不開心了。所以本例中使用zipkin最新2.11.8release版本來構建一個服務端
1. 建立一個微服務 cloud-demo-zipkin-server
1. pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-server</artifactId> <version>2.11.8</version> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-autoconfigure-ui</artifactId> <version>2.11.8</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> |
2. yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
server: port: 9411 spring: application: name: zipkin-server management: metrics: web: server: auto-time-requests: false eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ instance: prefer-ip-address: true |
3. 啟動註解
1 2 3 |
@EnableEurekaClient @EnableZipkinServer @SpringBootApplication |
需要注意的是EnableZipkinServer註解是zipkin2.server.internal包下的。
2. 建立一個微服務 cloud-demo-zipkin-server
改造一下我們原先的兩個微服務:cloud-demo-consumer-feign-hystrix,cloud-demo-provider
首先為它們都加上zipkin客戶端的依賴
1 2 3 4 5 6 7 8 |
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> |
yml中指定zipkin-server的url,並且指定日誌傳送方式為web
1 2 3 4 5 |
spring: zipkin: base-url: http://localhost:19411 sender: type: web |
3. 檢視結果
此時我們所有的準備工作已經做好了,我們啟動Eureka、zipkin、cloud-demo-consumer-feign-hystrix、cloud-demo-provider幾個微服務,呼叫幾次cloud-demo-consumer-feign-hystrix的user/getUser介面。然後訪問http://localhost:9411應該就可以看到zipkin的UI介面。
這個時候可能有的童鞋看到有資料,有的沒有資料,這個不是你的配置錯了,而是因為zipkin客戶端預設收集10%的資料,你可以使用下方的配置更改zipkin的取樣頻率。
1 2 3 4 |
spring: sleuth: sampler: probability: 1.0 |
GitHub地址:https://github.com/2388386839/spring-cloud-demo。程式碼所在模組:cloud-demo-zipkin-server,cloud-demo-consumer-feign-hystrix,cloud-demo-provider
如果對您有所幫助,請記得幫忙點一個star哦
本文出自http://zhixiang.org.cn,轉載請保留。