Spring Cloud Sleuth:鏈路跟蹤
Spring Cloud Sleuth是一種分佈式追蹤解決方案,它兼容Zipkin。微服務架構由於服務單元數量衆多、調用關係雜亂、業務複雜,如果出現了錯誤和異常很難去定位,所以微服務架構中,必須實現分佈式鏈路追蹤,去跟進一個請求到底有哪些服務參與,參與的順序又是怎樣的,讓每個請求步驟直觀可見,而 Sleuth + Zipkin 便可以幫助我們做到這樣的事情。
開發環境 | 版本 |
---|---|
IDEA | 2018.2.6 |
JDK | 1.8 |
Spring Boot | 2.0.6 |
Spring Cloud | Finchley.SR2 |
Docker | 18.09.0 |
Zipkin | – |
Zipkin 容器搭建
Docker下執行一個命令就好,開啓服務後訪問http://localhost:9411/zipkin/可以看到如下界面。
依賴添加
provider項目和consumer項目添加如下Maven依賴,特別注意:,因爲我這邊一直下載不到2.0.2的包,不知道是不是我的IDE的問題,將版本改爲2.1.0.M2。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
<version>2.1.0.M2</version>
</dependency>
配置文件修改
provider項目和consumer項目添加如下配置:
spring:
zipkin:
base-url: http://localhost:9411/
sender:
type: web
sleuth:
sampler:
probability: 1
logging:
level:
org.springframework.cloud.openfeign: debug
鏈路跟蹤測試
訪問consumer的接口http://localhost:8091/hello/consume?num=1,發現consumer項目控制檯會打印如下日誌,注意cde8ad1de4f90441
。
2018-11-26 21:26:23.946 INFO [consumer,cde8ad1de4f90441,e3c14617dff44d76,true] 12488 --- [trix-provider-1] s.c.a.AnnotationConfigApplicationContext : Refreshing SpringClientFactory-provider: startup date [Mon Nov 26 21:26:23 CST 2018]; parent: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@777d0bc3
2018-11-26 21:26:24.009 INFO [consumer,cde8ad1de4f90441,e3c14617dff44d76,true] 12488 --- [trix-provider-1] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2018-11-26 21:26:24.236 INFO [consumer,cde8ad1de4f90441,e3c14617dff44d76,true] 12488 --- [trix-provider-1] c.netflix.config.ChainedDynamicProperty : Flipping property: provider.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2018-11-26 21:26:24.254 INFO [consumer,cde8ad1de4f90441,e3c14617dff44d76,true] 12488 --- [trix-provider-1] c.n.u.concurrent.ShutdownEnabledTimer : Shutdown hook installed for: NFLoadBalancer-PingTimer-provider
2018-11-26 21:26:24.279 INFO [consumer,cde8ad1de4f90441,e3c14617dff44d76,true] 12488 --- [trix-provider-1] c.netflix.loadbalancer.BaseLoadBalancer : Client: provider instantiated a LoadBalancer: DynamicServerListLoadBalancer:{NFLoadBalancer:name=provider,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null
2018-11-26 21:26:24.284 INFO [consumer,cde8ad1de4f90441,e3c14617dff44d76,true] 12488 --- [trix-provider-1] c.n.l.DynamicServerListLoadBalancer : Using serverListUpdater PollingServerListUpdater
2018-11-26 21:26:24.313 INFO [consumer,cde8ad1de4f90441,e3c14617dff44d76,true] 12488 --- [trix-provider-1] c.netflix.config.ChainedDynamicProperty : Flipping property: provider.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2018-11-26 21:26:24.315 INFO [consumer,cde8ad1de4f90441,e3c14617dff44d76,true] 12488 --- [trix-provider-1] c.n.l.DynamicServerListLoadBalancer : DynamicServerListLoadBalancer for client provider initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=provider,current list of Servers=[192.168.99.1:8081],Load balancer stats=Zone stats: {defaultzone=[Zone:defaultzone; Instance count:1; Active connections count: 0; Circuit breaker tripped count: 0; Active connections per server: 0.0;]
},Server stats: [[Server:192.168.99.1:8081; Zone:defaultZone; Total Requests:0; Successive connection failure:0; Total blackout seconds:0; Last connection made:Thu Jan 01 08:00:00 CST 1970; First connection made: Thu Jan 01 08:00:00 CST 1970; Active Connections:0; total failure count in last (1000) msecs:0; average resp time:0.0; 90 percentile resp time:0.0; 95 percentile resp time:0.0; min resp time:0.0; max resp time:0.0; stddev resp time:0.0]
]}ServerList:org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList@7900120d
訪問Zipkin頁面,查詢consumer項目的調用鏈:
點擊某一行即可查看該調用的詳細信息,包括其traceIdcde8ad1de4f90441
。
總結
至此《Spring Cloud 學習紀要》系列博客就結束了,本系列博客介紹了包含服務治理組件Eureka、服務通信組件Feign、負載均衡組件Ribbon、配置中心組件Config、消息總線組件Bus、消息驅動組件Stream、統一網關組件Zuul、熔斷降級組件Hystrix、鏈路跟蹤組件Sleuth,此係列博客只適合入門小白,是一套極簡教程,更多知識待您親自探索。
組件 | 功能 | 同類框架 |
---|---|---|
Spring Cloud Eureka | 服務治理 | ZooKeeper、Consul |
Spring Cloud Feign | 服務通信 | Retrofit、OKHttp、HttpClient |
Spring Cloud Ribbon | 負載均衡 | Nginx |
Spring Cloud Config | 配置中心 | Consul |
Spring Cloud Bus | 消息總線 | |
Spring Cloud Stream | 消息驅動 | |
Spring Cloud Zuul | 統一網關 | Janus、Spring Cloud Gateway |
Spring Cloud Hystrix | 熔斷降級 | |
Spring Cloud Sleuth | 鏈路跟蹤 | Zipkin、Pinpoint |