Spring Cloud 微服務六:調用鏈跟蹤Spring cloud sleuth +zipkin

前言:隨着微服務系統的增長,服務之間的調用關係變得會很是複雜,這給運維以及排查問題帶來了很大的麻煩,這時服務調用監控就顯得很是重要了。spring cloud sleuth實現了對分佈式服務的監控解決方案。html

前情回顧請參考:java

Spring Cloud 微服務一:Consul註冊中心git

Spring Cloud 微服務二:API網關spring cloud zuulgithub

Spring Cloud 微服務三: API網關Spring cloud gatewayweb

Spring Cloud 微服務四:熔斷器Spring cloud hystrixspring

Spring Cloud 微服務五:Spring cloud gateway限流docker

 

  • 概覽
  • 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頁面上查詢到。
相關文章
相關標籤/搜索