一、背景

随着业务的发展,系统规模越来越大,各微服务直接的调用关系也变得越来越复杂。通常一个由客户端发起的请求在后端系统中会经过多个不同的微服务调用协同产生最后的请求结果,几乎每一个前端请求都会形成一条复杂的分布式服务调用链路,对每个请求实现全链路跟踪,可以帮助我们快速发现错误根源以及监控分析每条请求链路上性能瓶颈。
针对分布式服务跟踪,Spring Cloud Sleuth提供了一套完整的解决方案。

二、原理

参考《SpringCloud微服务实战》第11章。
先查看跟踪日志了解每项的含义

2019-02-27 20:40:56.419  INFO [service-a,e79b41414a56743d,e79b41414a56743d,true] 8276 --- [nio-9001-exec-5] cn.sp.controller.ServiceController       : ==<Call Service-a>==
  1. 第一个值service-a:记录了应用的名称,也就是spring.application.name的值。
  2. 第二个值e79b41414a56743d:SpringCloudSleuth生成的一个ID,叫TraceID,它用来标识一条请求链路。一条请求链路中包含一个TraceID,多个 SpanID。
  3. 第三个值e79b41414a56743d,SpringCloudSleuth生成的另外一个ID,叫SpanID,它表示一个基本的工作单元,比如发送一个HTTP请求。
  4. 第四个值true:表示是否要将该信息输出到Zipkin等服务中收集和展示。

三、编码

3.1创建一个Eureka注册中心

这个之前的文章有,故省略

3.2调用者service-a

1.创建项目引入依赖

  1. <dependency> 

  2. <groupId>org.springframework.boot</groupId> 

  3. <artifactId>spring-boot-starter-web</artifactId> 

  4. </dependency> 

  5. <dependency> 

  6. <groupId>org.springframework.cloud</groupId> 

  7. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 

  8. </dependency> 

  9. <dependency> 

  10. <groupId>org.springframework.cloud</groupId> 

  11. <artifactId>spring-cloud-starter-sleuth</artifactId> 

  12. </dependency> 

  13. <dependency> 

  14. <groupId>org.springframework.cloud</groupId> 

  15. <artifactId>spring-cloud-starter-zipkin</artifactId> 

  16. </dependency> 

其实spring-cloud-starter-zipkin中已经有了sleuth的依赖,spring-cloud-starter-sleuth可以省略。
2. 启动类

@EnableEurekaClient
@SpringBootApplication
public class ServiceAApplication { public static void main(String[] args) {
SpringApplication.run(ServiceAApplication.class, args);
} @Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
  1. ServiceController
@RestController
public class ServiceController { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired
private RestTemplate restTemplate; @GetMapping("/service-a")
public String test(){
logger.info("==<Call Service-a>==");
return restTemplate.getForEntity("http://service-b/test",String.class).getBody();
}
}

这里的url(http://service-b/test)用的是服务名当虚拟域名而不是ip+端口号的形式,至于为什么可以访问可以查看这篇文章【Spring Cloud中restTemplate是如何通过服务名主求到具体服务的】。

4.配置文件application.yml

  1. spring: 

  2. application: 

  3. name: service-a 

  4. zipkin: 

  5. base-url: http://localhost:9411 

  6. # 抽样收集的百分比,默认10% 

  7. sleuth: 

  8. sampler: 

  9. probability: 1 

  10. eureka: 

  11. client: 

  12. serviceUrl: 

  13. defaultZone: http://localhost:8761/eureka/ 

  14. server: 

  15. port: 9001 

3.3被调用者service-b

1.创建项目引入依赖pom文件同上
2.启动类添加注解 @EnableEurekaClient
3.ServiceController

@RestController
public class ServiceController { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @GetMapping("/test")
public String test(){
logger.info("==<Call Service-b>==");
return "OK";
}
}

4.配置文件application.yml

  1. spring: 

  2. application: 

  3. name: service-b 

  4. zipkin: 

  5. base-url: http://localhost:9411 

  6. # 抽样收集的百分比,默认10% 

  7. sleuth: 

  8. sampler: 

  9. probability: 1 

  10. eureka: 

  11. client: 

  12. serviceUrl: 

  13. defaultZone: http://localhost:8761/eureka/ 

  14. server: 

  15. port: 9002 

3.4创建zipkin-server

  1. pom.xml
  1. <dependency> 

  2. <groupId>io.zipkin.java</groupId> 

  3. <artifactId>zipkin-server</artifactId> 

  4. <version>2.12.2</version> 

  5. <exclusions> 

  6. <exclusion> 

  7. <groupId>org.springframework.boot</groupId> 

  8. <artifactId>spring-boot-starter-log4j2</artifactId> 

  9. </exclusion> 

  10. </exclusions> 

  11. </dependency> 

  12. <!-- https://mvnrepository.com/artifact/io.zipkin.java/zipkin-autoconfigure-ui --> 

  13. <dependency> 

  14. <groupId>io.zipkin.java</groupId> 

  15. <artifactId>zipkin-autoconfigure-ui</artifactId> 

  16. <version>2.12.2</version> 

  17. </dependency> 

2.启动类添加注解 @EnableZipkinServer
注意: 这里我排除了log4j的依赖,因为已经有了相同的类文件启动会报错。

四、测试

  1. 依次启动eureka注册中,service-a,service-b,zipkin-server
  2. 访问http://localhost:8761/看到有两个注册实例,说明启动成功。
  3. 游览器访问http://localhost:9001/service-a请求服务A
    如果报错日志打印java.lang.IllegalStateException: No instances available for service-b,那可能是服务B还没注册完成等会儿再试下即可。
  4. 正常情况会看到游览器显示 OK
    服务A的日志如下:
2019-02-27 21:18:05.119  INFO [service-a,3e487761c9b13a2e,3e487761c9b13a2e,true] 8276 --- [nio-9001-exec-9] cn.sp.controller.ServiceController       : ==<Call Service-a>==

服务B的日志如下:

2019-02-27 21:18:05.128  INFO [service-b,3e487761c9b13a2e,8d49352c6645c6f9,true] 13860 --- [nio-9002-exec-8] cn.sp.controller.ServiceController       : ==<Call Service-b>==
  1. 访问http://localhost:9411/zipkin/进入可视化界面

点击查找,服务名选择service-a即可看到最近的统计数据,还可以根据条件进行筛选。

点击依赖,依赖分析就可以看到服务之前的依赖关系。

完整代码地址。

五、总结

基本是根据《SpringCloud微服务实战》这本书来的,但是中间还是踩了几个坑,比如开始报No instances available for service-b的时候我一直以为是代码哪里出了问题,浪费了很多时间。

SpringCloudSleuth除了把请求链路数据整合到Zipkin中外,还可以保存到消息队列,ELK等,还算比较强大。但是除了HTTP请求,不知道对于GRPC这样的通信方式是否也能实现链路跟踪。

查了下资料支持Grpc的,demo地址:https://github.com/tkvangorder/sleuth-grpc-sample

官方文档:https://cloud.spring.io/spring-cloud-sleuth/single/spring-cloud-sleuth.html#_grpc

【SpringCloud构建微服务系列】分布式链路跟踪Spring Cloud Sleuth的更多相关文章

  1. Spring cloud系列十四 分布式链路监控Spring Cloud Sleuth

    1. 概述 Spring Cloud Sleuth实现对Spring cloud 分布式链路监控 本文介绍了和Sleuth相关的内容,主要内容如下: Spring Cloud Sleuth中的重要术语 ...

  2. 【SpringCloud构建微服务系列】微服务网关Zuul

    一.为什么要用微服务网关 在微服务架构中,一般不同的微服务有不同的网络地址,而外部客户端(如手机APP)可能需要调用多个接口才能完成一次业务需求.例如一个电影购票的手机APP,可能会调用多个微服务的接 ...

  3. 【SpringCloud构建微服务系列】Feign的使用详解

    一.简介 在微服务中,服务消费者需要请求服务生产者的接口进行消费,可以使用SpringBoot自带的RestTemplate或者HttpClient实现,但是都过于麻烦. 这时,就可以使用Feign了 ...

  4. 【SpringCloud构建微服务系列】使用Spring Cloud Config统一管理服务配置

    一.为什么要统一管理微服务配置 对于传统的单体应用而言,常使用配置文件来管理所有配置,比如SpringBoot的application.yml文件,但是在微服务架构中全部手动修改的话很麻烦而且不易维护 ...

  5. 【SpringCloud构建微服务系列】学习断路器Hystrix

    一.Hystrix简介 在微服务架构中经常包括多个服务层,比如A为B提供服务,B为C和D提供服务,如果A出故障了就会导致B也不可用,最终导致C和D也不可用,这就形成了雪崩效应. 所以为了应对这种情况, ...

  6. SpringCloud(7)服务链路追踪Spring Cloud Sleuth

    1.简介 Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案,并且兼容支持了 zipkin,你只需要在pom文件中引入相应的依赖即可.本文主要讲述服务追踪组件zipki ...

  7. 史上最简单的SpringCloud教程 | 第九篇: 服务链路追踪(Spring Cloud Sleuth)(Finchley版本)

    转载请标明出处: 原文首发于:>https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f9-sleuth/ 本文出自方志朋的博客 这篇文章主 ...

  8. 史上最简单的SpringCloud教程 | 第九篇: 服务链路追踪(Spring Cloud Sleuth)

    这篇文章主要讲述服务追踪组件zipkin,Spring Cloud Sleuth集成了zipkin组件. 注意情况: 该案例使用的spring-boot版本1.5.x,没使用2.0.x, 另外本文图3 ...

  9. SpringCloud教程 | 第九篇: 服务链路追踪(Spring Cloud Sleuth)

    版权声明:本文为博主原创文章,欢迎转载,转载请注明作者.原文超链接 ,博主地址:http://blog.csdn.net/forezp. http://blog.csdn.net/forezp/art ...

随机推荐

  1. 虚拟化(五):vsphere高可用群集与容错(存储DRS是一种可用于将多个数据存储作为单个数据存储群集进行管理的功能)

    vsphere高级功能需要vcenter server和共享存储的支持才能实现.vsphere的高级功能有 vmotion.storage vmotion.vsphere HA.vsphere DRS ...

  2. hdu 3415 单调队列

    Max Sum of Max-K-sub-sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  3. ABAP 日期栏函数

    在SZC这个中有很多日期函数可以研究借鉴   ABAP - 日期格式转换 现在提供以下一些日期格式转换的函数: Below are several FMs which can be used to c ...

  4. HTML中级教程 自定义列表

    在HTML初级教程中我们教授了无序列表和有序列表,很不幸,很像Peter Cushing的博士Who,自定义列表很容易被忽略.可能是因为自定义列表需要比无序列表和有序列表更多的设置和似乎更少用.当遭遇 ...

  5. system调用命令行命令而不显示命令行窗口

    system调用命令行命令而不显示命令行窗口 通常用system调用命令行命令时都会弹出黑底白字的命令行窗口,下面的代码可以不显示弹出的命令行窗口. 代码如下 #pragma comment( lin ...

  6. linux应用之用户管理相关命令

    1. useradd useradd 命令可以创建一个新的用户帐号,其最基本用法为: useradd 用户名 如输入以下命令: useradd newuser 系统将创建一个新用户 newuser,该 ...

  7. spark uniq 本质上就是单词计数

    粗体部分示例: # dns_domain_info_list_rdd ==> [(src_ip, domain, domain_ip, timestamp, metadataid), ....] ...

  8. ACM应该学什么(知乎学长)

    网络上流传的答案有很多,估计提问者也曾经去网上搜过.所以根据自己微薄的经验提点看法. 我ACM初期是训练编码能力,以水题为主(就是没有任何算法,自己靠动脑筋能够实现的),这种题目特点是麻烦,但是不难, ...

  9. 书写优雅的shell脚本(七)- ${COLUMN:-}

    ${COLUMN:-} 如果COLUMN是空变量,或者变量不存在,返回-后面的内容,如果变量有值返回这个值.

  10. Linux-awk和shell编程初步

    1 awk 格式: awk -选项 '处理' 输入 awk -F : '{print $1}' file -F指定分隔符, 默认是空格 $1 分割后的第一部分 $0 获得所有部分 NF 表示以分隔符分 ...