Spring Cloud Sleuth:链路跟踪

  Spring Cloud Sleuth是一种分布式追踪解决方案,它兼容Zipkin。微服务架构由于服务单元数量众多、调用关系杂乱、业务复杂,如果出现了错误和异常很难去定位,所以微服务架构中,必须实现分布式链路追踪,去跟进一个请求到底有哪些服务参与,参与的顺序又是怎样的,让每个请求步骤直观可见,而 Sleuth + Zipkin 便可以帮助我们做到这样的事情。

开发环境版本
IDEA2018.2.6
JDK1.8
Spring Boot2.0.6
Spring CloudFinchley.SR2
Docker18.09.0
Zipkin

特别注意:本系列纪要环环相扣,建议从第一节开始阅读 点击跳转

Zipkin 容器搭建

docker run -d -p 9411:9411 openzipkin/zipkin

  Docker下执行一个命令就能运行Zipkin容器,具体视个人PC情况而定,开启服务后访问http://localhost:9411/zipkin/可以看到如下界面。
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项目的调用链:
1
  点击某一行即可查看该调用的详细信息,包括其traceIdcde8ad1de4f90441
2

总结

  至此《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

附件

本系列纪要博客源码:跳转到github
本系列纪要博客配置文件:跳转到github

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐