推荐阅读:

  1. Spring全家桶笔记:Spring+Spring Boot+Spring Cloud+Spring MVC
  2. 一个SpringBoot问题就干趴下了?我却凭着这份PDF文档吊打面试官.

前言

Spring Cloud Sleuth 的主要功能就是为 分布式系统 提供 追踪解决方案,并且兼容支持了 Zipkin,只需要在 pom.xml 文件中引入相应的 依赖 即可。本文主要讲述 服务追踪组件 Zipkin,Spring Cloud Sleuth 集成了 Zipkin 组件。它主要用于 聚集 来自各个 异构系统实时监控数据,用来追踪 微服务架构 下的 系统延时问题




springcloud 链路执行时间_springcloud上传文件


正文

1. 相关术语

1.1. Span

Span 是一个基本的 工作单元,用于描述一次 RPC 调用,Span 通过一个 64 位的 spanId 作为 唯一标识。Zipkin 中的 Span 还有其他数据信息,比如 摘要时间戳事件关键值注释 (tags) 以及 进度 ID (通常是 IP 地址)。Span 在不断的启动和停止,同时记录了 时间信息,一个 Span 创建后,必须在未来的某个时刻停止它。

1.2. Trace

一系列 Span 组成的一个 树状结构。例如,如果你正在跑一个大型 分布式系统,可能需要创建一个 Trace。

1.3. Annotation

表示 基本标注列表,一个 Annotation 可以理解成 Span 生命周期中 重要时刻数据快照,比如一个 Annotation 中一般包含 发生时刻(timestamp)、事件类型(value)、端点(endpoint)等信息。其中 Annotation 的 事件类型 包含以下四类:

  • cs - Client Sent

客户端 发起一个请求,这个 Annotion 描述了这个 Span 的开始。

  • sr - Server Received

服务端 获得请求并 准备开始 处理它,如果将 sr 减去 cs 的 时间戳 便可得到 网络延迟

  • ss - Server Sent

服务端 完成请求处理,如果将 ss 减去 sr 的 时间戳,便可得到 服务端 处理请求消耗的时间。

  • cr - Client Received

客户端 成功接收到 服务端 的响应,如果将 cr 减去 cs 的 时间戳,便可得到 整个请求 所消耗的 总时间

2. 项目结构

本文案例主要由 四个模块 组成:

  • eureka-server:作为 服务注册中心
  • zipkin-server:作为 链路追踪服务中心,负责存储 链路数据
  • service-hi:对外暴露一个 测试接口,同时作为 链路追踪服务端,负责 产生链路数据
  • service-zuul:作为 路由网关,负责 请求转发,同时作为 链路追踪客户端,产生 链路数据,并上传至 zipkin-server。

在 8761 端口 开启 eureka-server 服务注册中心,参考前面的文章即可,这里不再演示创建。

3. 构建zipkin-server

新建一个 Spring Boot 应用模块 zipkin-server,它的 pom.xml 完整依赖如下:

4.0.0  org.springframework.boot spring-boot-starter-parent 1.5.3.RELEASE    io.github.ostenant.springcloud zipkin-server 0.0.1-SNAPSHOT zipkin-server Demo project for Spring Boot  1.8 Dalston.RELEASE    org.springframework.cloud spring-cloud-starter-eureka   io.zipkin.java zipkin-server   io.zipkin.java zipkin-autoconfigure-ui   org.springframework.boot spring-boot-starter-test test      org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import       org.springframework.boot spring-boot-maven-plugin

在应用的入口类上, 加上注解 @EnableZipkinServer,开启 Zipkin Server 的功能。

@EnableZipkinServer@EnableEurekaClient@SpringBootApplicationpublic class ZipkinServerApplication { public static void main(String[] args) { SpringApplication.run(ZipkinServerApplication.class, args); }}

配置文件 application.yml 中指定服务端口号为 9411,并向 Eureka 注册中心进行 服务注册

eureka: client: service-url: defaultZone: http://localhost:8761/eureka/server: port: 9411spring: application: name: zipkin-server

4. 构建service-hi

新建一个 Spring Boot 应用模块 service-hi,在它的 pom.xml 中引入 引入起步依赖 spring-cloud-starter-zipkin,完整依赖如下:

4.0.0  org.springframework.boot spring-boot-starter-parent 1.5.3.RELEASE    io.github.ostenant.springcloud service-hi 0.0.1-SNAPSHOT eureka-client Demo project for Spring Boot  1.8 Dalston.SR1    org.springframework.cloud spring-cloud-starter-eureka   org.springframework.cloud spring-cloud-starter-zipkin   org.springframework.boot spring-boot-starter-web   org.springframework.boot spring-boot-starter-test test      org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import       org.springframework.boot spring-boot-maven-plugin

在它的 配置文件 application.yml 中通过配置项 spring.zipkin.base-url 指定 zipkin server 的地址。

eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/server: port: 8763spring: application: name: service-hi zipkin: # base-url: http://localhost:9411/ # 若在同一个注册中心的话可以启用自动发现,省略base-url locator: discovery: enabled: true #自动发现 sleuth: sampler: percentage: 1.0

到此为止 ZipKin 客户端 已经整合完毕,最后在 应用启动类 上对外暴露一个 API 接口 方便测试

@SpringBootApplication@EnableEurekaClient@RestControllerpublic class ServiceHiApplication { public static void main(String[] args) { SpringApplication.run(ServiceHiApplication.class, args); } @Value("${server.port}") private String port; @RequestMapping("/hi") public String home(@RequestParam String name) { return "Hi " + name +