Spring Cloud 应用篇 之 Spring Cloud Sleuth + Zipkin(一)链路监控

在微服务系统中,随着业务的发展,系统会变得越来越大,那么各个服务之间的调用关系也就变得越来越复杂。一个 HTTP 请求会调用多个不同的微服务来处理返回最后的结果,在这个调用过程中,可能会因为某个服务出现网络延迟过高或发送错误导致请求失败,这个时候,对请求调用的监控就显得尤为重要了。Spring Cloud Sleuth 提供了分布式服务链路监控的解决方案。下面介绍 Spring Cloud Sleuth 整合 Zipkin 的解决方案。

(一)简介

Zipkin 是 Twitter 的一个开源项目,它基于 Google Dapper 实现的。我们可以使用它来收集各个服务器上请求链路的跟踪数据,并通过它提供的 REST API 接口来辅助查询跟踪数据以实现对分布式系统的监控程序,从而及时发现系统中出现的延迟过高问题。除了面向开发的 API 接口之外,它还提供了方便的 UI 组件来帮助我们直观地搜索跟踪信息和分析请求链路明细,比如可以查询某段时间内各用户请求的处理时间等。

Zipkin 和 Config 结构类似,分为服务端 Server,客户端 Client,客户端就是各个微服务应用。

(二)搭建 Zipkin 服务端

在 Spring Boot 2.0 版本之后,官方已不推荐自己搭建定制了,而是直接提供了编译好的 jar 包。详情可以查看官网:https://zipkin.io/pages/quickstart.html

可以在终端使用以下命令:


  
  
  1. curl -sSL https: //zipkin.io/quickstart.sh | bash -s
  2. java -jar zipkin.jar

使用 docker 的方式

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

任一方式启动后,访问 http://localhost:9411,可以看到服务端已经搭建成功


(三)搭建 Zipkin 客户端

创建两个服务,spring-cloud-service1、spring-cloud-service2,service1 实现一个 REST 接口 /service1,该接口里调用 service2 应用。

3.1 创建 spring-cloud-service1
3.1.1 引入依赖,pom 文件:

  
  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>spring-cloud-componets </artifactId>
  7. <groupId>com.geny </groupId>
  8. <version>1.0-SNAPSHOT </version>
  9. </parent>
  10. <modelVersion>4.0.0 </modelVersion>
  11. <artifactId>spring-cloud-service1 </artifactId>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework.boot </groupId>
  15. <artifactId>spring-boot-starter-web </artifactId>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.springframework.cloud </groupId>
  19. <artifactId>spring-cloud-starter-netflix-eureka-client </artifactId>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.springframework.cloud </groupId>
  23. <artifactId>spring-cloud-starter-netflix-ribbon </artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.cloud </groupId>
  27. <artifactId>spring-cloud-starter-zipkin </artifactId>
  28. </dependency>
  29. </dependencies>
  30. <build>
  31. <plugins>
  32. <plugin>
  33. <groupId>org.springframework.boot </groupId>
  34. <artifactId>spring-boot-maven-plugin </artifactId>
  35. </plugin>
  36. </plugins>
  37. </build>
  38. </project>
3.1.2 配置文件:

  
  
  1. server:
  2. port: 8481
  3. spring:
  4. application:
  5. name: service1
  6. zipkin:
  7. base-url: http: //192.168.174.128:9411/
  8. eureka:
  9. client:
  10. service-url:
  11. defaultZone: http: //localhost:8761/eureka/
  12. logging:
  13. level:
  14. root: debug
3.1.3 启动类

  
  
  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. @RestController
  4. public class Service1Application {
  5. public static void main(String[] args) {
  6. SpringApplication.run(Service1Application.class,args);
  7. }
  8. @Bean
  9. @LoadBalanced
  10. RestTemplate restTemplate() {
  11. return new RestTemplate();
  12. }
  13. @RequestMapping(value = "service1", method = RequestMethod.GET)
  14. public String service1() {
  15. return restTemplate().getForObject( "http://service2/service2", String.class);
  16. }
  17. }
3.2 创建 spring-cloud-service2

service2 的 pom.xml 文件、配置文件是一样的,只需要修改相应的端口和应用名称即可。

启动类如下:


  
  
  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. @RestController
  4. public class Service2Application {
  5. public static void main(String[] args) {
  6. SpringApplication.run(Service2Application.class,args);
  7. }
  8. @RequestMapping(value = "service2", method = RequestMethod.GET)
  9. public String service2() {
  10. return "Hi,I'm Service2!";
  11. }
  12. }
3.3 验证

依次启动 eureka-server、spring-cloud-service1、spring-cloud-service2,访问 http://localhost:8481/service1


接口访问已经成功,此时,我们查看一下控制台的日志输出:


从上面的控制台输出内容中,我们可以看到多了一些如 [service1,450165b378a38236,92377ff04d8a9cfb,false] 的日志信息,而这些元素正是实现分布式服务跟踪的重要组成部分,每个值的含义如下:

  • 第一个值:service1,它记录了应用的名称
  • 第二个值:450165b378a38236,是 Spring Cloud Sleuth 生成的一个 ID,称为 Trace ID,它用来标识一条请求链路。一条请求链路中包含一个 Trace ID,多个 Span ID。
  • 第三个值:92377ff04d8a9cfb,是 Spring Cloud Sleuth 生成的另外一个 ID,称为 Span ID,它表示一个基本的工作单元,比如发送一个 HTTP 请求。
  • 第四个值:false,它表示是否要将该信息输出到 Zipkin Server 中来收集和展示。

上面四个值中的 Trace ID 和 Span ID 是 Spring Cloud Sleuth 实现分布式服务跟踪的核心。在一次请求中,会保持并传递同一个 Trance ID,从而将整个fenbu分布于不同微服务进程中的请求跟踪信息串联起来。

下面我们访问 Zipkin Server 端,http://192.168.174.128:9411/



发现服务名下并没有看到我们的应用,这是为什么呢?

这是因为 Spring Cloud Sleuth 采用了抽样收集的方式来为跟踪信息打上收集标记,也就是上面看到的第四个值。为什么要使用抽样收集呢?理论上应该是收集的跟踪信息越多越好,可以更好的反映出系统的实际运行情况,但是在高并发的分布式系统运行时,大量请求调用会产生海量的跟踪日志信息,如果过多的收集,会对系统性能造成一定的影响,所以 Spring Cloud Sleuth 采用了抽样收集的方式。

既然如此,那么我们就需要把上面第四个值改为 true,开发过程中,我们一般都是收集全部信息。

Sleuth 默认采样算法的实现是 Reservoir sampling,具体的实现类是 PercentageBasedSampler,默认的采样比例为: 0.1,即 10%。我们可以通过 spring.sleuth.sampler.probability 来设置,所设置的值介于 0 到 1 之间,1 则表示全部采集

修改配置文件如下:


  
  
  1. server:
  2. port: 8481
  3. spring:
  4. application:
  5. name: service1
  6. zipkin:
  7. base-url: http: //192.168.174.128:9411/<span style="color:#ff0000;">
  8.   sleuth:
  9. sampler:
  10. probability: 1.0</span>
  11. eureka:
  12. client:
  13. service-url:
  14. defaultZone: http: //localhost:8761/eureka/
  15. logging:
  16. level:
  17. root: debug

再次启动 spring-cloud-service1、spring-cloud-service2,访问 http://localhost:8481/service1

查看后台日志:


这个时候,第四个值已经为true了,再访问 http://192.168.174.128:9411/


可以看到已经有我们的服务调用记录了

选择 service1,点击 Find Trances,可以看到我们的服务调用记录

点击记录就可以清楚的看到我们的服务调用链路关系,可以看到每一个服务所所耗费的时间


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



源码下载:https://github.com/shmilyah/spring-cloud-componets
转载自:https://blog.csdn.net/hubo_88/article/details/80878632

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值