1. 概述

Spring Cloud Sleuth实现对Spring cloud 分布式链路监控
本文介绍了和Sleuth相关的内容,主要内容如下:

  • Spring Cloud Sleuth中的重要术语和意义:Span、Trance、Annotation
  • Zipkin中图形化展示分布式链接监控数据并说明字段意义
  • Spring Cloud集成Sleuth + Zipkin 的代码demo: Sleuth集成Zipkin, Zipkin数据持久化等

2. 术语

Span
Span是基本的工作单元。Span包括一个64位的唯一ID,一个64位trace码,描述信息,时间戳事件,key-value 注解(tags),span处理者的ID(通常为IP)。
最开始的初始Span称为根span,此span中span id和 trace id值相同。

Trance
包含一系列的span,它们组成了一个树型结构

Annotation
用于及时记录存在的事件。常用的Annotation如下

  • cs - Client Sent:客户端发送一个请求,表示span的开始
  • sr - Server Received:服务端接收请求并开始处理它。(sr-cs)等于网络的延迟
  • ss - Server Sent:服务端处理请求完成,开始返回结束给服务端。(ss-sr)表示服务端处理请求的时间
  • cr - Client Received:客户端完成接受返回结果,此时span结束。(cr-sr)表示客户端接收服务端数据的时间

如果一个服务的调用关系如下:

那么此时将Span和Trace在一个系统中使用Zipkin注解的过程图形化:

每个颜色的表明一个span(总计7个spans,从A到G),每个span有类似的信息

Trace Id = X
Span Id = D
Client Sent
  • 1
  • 2
  • 3

此span表示span的Trance Id是X,Span Id是D,同时它发送一个Client Sent事件

spans 的parent/child关系图形化如下:

3. 在Zipkin中图形化展示分布式链接监控数据

3.1 spans在zipkin界面的信息解读

在Zipkin中展示了上图的跟踪信息,红框里是对上图调用span的跟踪

但是你点击这个trace时,我们只看到4个span

为什么两个界面显示的span数量不同,一个是7,一个4.

  • 1 个 spans:来自servier1的接口http:/start 被调用,分别是Server Received (SR) 和 Server Sent (SS) annotations.
  • 2 个 spans:来自 service1 调用service2 的 http:/foo 接口。service1
    端有两个span,分别为 Client Sent (CS) 和 Client Received (CR)
    annotations。service2 端也有两个span,分别为Server Received (SR) 和Server Sent (SS)
    。物理上他有2个span,但是从逻辑上说这个他们组成一个RPC调用的span。
  • 2个span:来自 service2 调用 service3 的 http:/bar 接口,service2
    端有两个span,分别为Client Sent (CS) 和 Client Received (CR) annotations。service3
    端也有两个span,分别为Server Received (SR) 和Server Sent (SS)
    。物理上他有2个span,但是从逻辑上说它们都是同一个RPC调用的span。
  • 2个span:来自 service2 调用 service4 的 http:/bar 接口,service2
    端有两个span,分别为Client Sent (CS) 和 Client Received (CR) annotations。service4
    端也有两个span,分别为Server Received (SR) and Server Sent (SS)
    。物理上他有2个span,但是从逻辑上说这个它们都是同一个RPC调用的span。

所以我们计算物理spans有7个:

  • 1个来自 http:/start 被请求
  • 2个来自 service1调用service2
  • 2个来自 service2调用service3
  • 2个来自 service2调用service4.

从逻辑上说,我们只看到4个span:

  • 1个来自 service1 的接口http:/start 被请求
  • 3个来自 服务之前的RCP接口调用

3.2. Zipkin可视化错误

如果调用链路中发生接口调用失败,zipkin会默认使用红色展示信息,如下图:

点击红色的span,可以看到详细的失败信息:

4. Spring Cloud集成Sleuth + Zipkin

本节演示在Spring Cloud中集成Sleuth并将链路监控数据传送到Zipkin,使用Zipkin展示数据,并配置mysql进行数据持久化

4.1. 相关工程

cloud-registration-center
注册中心

cloud-service-zipkin
提供测试服务接口,对外提供有两个接口:
- 调用成功后,马上返回一个结果: http://127.0.0.1:17602//zipkin/simple
- 调用成功后,服务sleep 5s后再返回: http://127.0.0.1:17602//zipkin/sleep

cloud-consumer-zipkin
消费服务
此服务会通过Feign调用cloud-service-zipkin里提供的两个接口(/zipkin/sleep和/zipkin/simple),我们访问如下URL会调用的对应的接口:
http://127.0.0.1:17603//zipkin/simple
http://127.0.0.1:17603//zipkin/sleep

以上3个服务的代码比较简单,这里代码略,可以自己看github里的代码。下文中只列出和Sleuth和Zipkin相关的配置。

cloud-dashboard-zipkin
配置Zipkin服务,提供可视化链路监控
Zipkin首页:http://127.0.0.1:17601/zipkin/

4.2. 在工程中使用sleuth+zipkin+http配置

在cloud-service-zipkin和cloud-consumer-zipkin中启动sleuth,sleuth会收集spans信息,并使用http异步地将spans 信息发送到Zipkin,然后Zipkin展示信息

cloud-service-zipkin和cloud-consumer-zipkin配置Sleuth+Zipkin
2个工程中为了集成Sleuth和Zipkin,需要在普通工程基础上增加如下配置:

  • pom.xml增加sleuth和zipkin相关的jar
<!-- sleuth配置 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<!-- 引入zipkin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • application-zipkin.yml
    Zipkin+Sleuth配置参数:

    • spring.zipkin.baseUrl:指定cloud-dashboard-zipkin的服务地址,本例子中使用真实的IP。在新的版本spring-cloud-sleuth-core-1.3.0.RELEASE中,可以实现通过服务名称进行访问
    • spring.sleuth.sampler.percentage:设置采样率,为了测试设置100%采集
spring:
zipkin:
enabled: true
# zipkkin dashboard的地址:通过真实IP地址访问
baseUrl: http://localhost:17601/
# 通过cloud-dashboard-zipkin注册到注册中心的服务名称访问,本版本(spring-cloud-sleuth-core-1.2.5.RELEASE)不支持,需要从spring-cloud-sleuth-core-1.3.0.RELEASE开始支持这个功能
# 配置如下:
# baseUrl: http://cloud-dashboard-zipkin/
sleuth:
sampler:
# 默认值为0.1f,现在为了测试设置100%采集
percentage: 1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

cloud-dashboard-zipkin
配置zipkin服务

  • pom.xml
<!-- spring-cloud-starter-zipkin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<!-- zipkin 界面-->
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
</dependency>
<!-- zipkin 服务类-->
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-server</artifactId>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 配置参数
    bootstrap-zipkin-http.yml
# port
server:
port: 17601 spring:
application:
# 本服务注册到注册到服务器的名称, 这个名称就是后面调用服务时的服务标识符
name: cloud-dashboard-zipkin
eureka:
client:
serviceUrl:
# 服务器注册/获取服务器的zone
defaultZone: http://127.0.0.1:10761/eureka/
# defaultZone: http://192.168.21.3:10761/eureka/,http://192.168.21.4:10761/eureka/
instance:
prefer-ip-address: true
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • application-zipkin-http.yml
    关闭本工程的推送到zipkin服务的功能
spring:
zipkin:
enabled: false
  • 1
  • 2
  • 3

启动类
@EnableZipkinServer:注解此类为Zipkin服务

@EnableEurekaClient // 配置本应用将使用服务注册和服务发现
@SpringBootApplication
@EnableZipkinServer // 启动Zipkin服务
public class ZipkinDashboardCloudApplication { public static void main(String[] args) {
args = new String[1];
args[0] = "--spring.profiles.active=zipkin-http";
SpringApplication.run(ZipkinDashboardCloudApplication.class, args);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

4.3. 测试

启动zipkin服务,提供可视化链路监控
Zipkin访问地址: http://127.0.0.1:17601/zipkin/

我们演示链路跟踪,访问以下两个请求:
http://127.0.0.1:17603/zipkin/simple :正常方法
http://127.0.0.1:17603/zipkin/sleep :访问超时

进入zipkin,访问成功为蓝色,失败为红部。同时显示各个服务花费的时间

点击蓝色记录,进入详细界面
可知整个接口花费约19ms,cloud-zipkin-service的业务执行15ms,cloud-zipkin-consumer访问cloud-zipkin-service的
网络延迟为2ms,返回结果给cloud-zipkin-service的网络延迟是4s

点击cloud-zipkin-service得到更精确的数据

4.4. Zipkin数据持久化

默认情况,zipkin存储记录到内存,如果服务重启,则所有记录丢失。为了保证持久性,zipkin支持Mysql、Elasticsearch、Cassandra存储。我们演示为zipkin配置MYSQL数据库,其它两种配置类似,本文略
pom.xml为cloud-dashboard-zipkin引入新jar

 <!-- zipkin 存储到数据库需要引入此类 -->
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-storage-mysql</artifactId>
</dependency> <!--保存到数据库需要如下依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

配置参数
application-zipkin-http.yml:
配置启动时会根据classpath:/mysql.sql初始化数据库

spring:
zipkin:
enabled: false
# 配置mysql
datasource:
schema: classpath:/mysql.sql
# url: jdbc:mysql://127.0.0.1/test
url: jdbc:mysql://127.0.0.1:3306/test?zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2b8
username: root
password: root
# Switch this on to create the schema on startup:
initialize: true
continueOnError: true
sleuth:
enabled: false
zipkin:
storage:
type: mysql
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

重启服务,服务成功后,查看数据库,自动生成数据库

如此数据库配置完毕,所有的spans信息存储到数据库中,重启服务,也不会丢失记录

4.5 在工程中使用sleuth+zipkin+ Spring Cloud Stream配置

本版本(spring-cloud-sleuth-core-1.2.5.RELEASE)支持集成spring-cloud-sleuth-stream,但是在新版本从spring-cloud-sleuth-core-1.3.0.RELEASE开始不支持spring-cloud-sleuth-stream。推荐直接使用通过集成RabbitMQ
或 Kafka。关于集成RabbitMQ 或 Kafka,关键是引入spring-rabbit 或
spring-kafka依赖包。默认的目的名称为zipkin.
对于这个的使用demo。这里暂时略

5. 代码

以上的详细的代码见下面
github代码,请尽量使用tag v0.11,不要使用master,因为我不能保证master代码一直不变

Spring cloud系列十四 分布式链路监控Spring Cloud Sleuth的更多相关文章

  1. Spring Cloud第十四篇 | Api网关Zuul

    ​ 本文是Spring Cloud专栏的第十四篇文章,了解前十三篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring C ...

  2. 分布式链路监控与追踪系统Zipkin

    1.分布式链路监控与追踪产生背景2.SpringCloud Sleuth + Zipkin3.分布式服务追踪实现原理4.搭建Zipkin服务追踪系统5.搭建Zipkin集成RabbitMQ异步传输6. ...

  3. (转)Spring Boot(二十):使用 spring-boot-admin 对 Spring Boot 服务进行监控

    http://www.ityouknow.com/springboot/2018/02/11/spring-boot-admin.html 上一篇文章<Spring Boot(十九):使用 Sp ...

  4. struts2官方 中文教程 系列十四:主题Theme

    介绍 当您使用一个Struts 2标签时,例如 <s:select ..../>  在您的web页面中,Struts 2框架会生成HTML,它会显示外观并控制select控件的布局.样式和 ...

  5. 53. spring boot系列合集【从零开始学Spring Boot】

    前40章节的spring boot系列已经打包成PDF在csdn进行发布了,如果有需要的可以进行下载. 下载地址:http://download.csdn.net/detail/linxinglian ...

  6. Spring Cloud第十篇 | 分布式配置中心Config

    ​ 本文是Spring Cloud专栏的第十篇文章,了解前九篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Clo ...

  7. Spring Cloud(十四)Config 配置中心与客户端的使用与详细

    前言 在上一篇 文章 中我们直接用了本应在本文中配置的Config Server,对Config也有了一个基本的认识,即 Spring Cloud Config 是一种用来动态获取Git.SVN.本地 ...

  8. Spring Cloud 系列之 Netflix Hystrix 服务监控

    Actuator Hystrix 除了可以实现服务容错之外,还提供了近乎实时的监控功能,将服务执行结果和运行指标,请求数量成功数量等等这些状态通过 Actuator 进行收集,然后访问 /actuat ...

  9. Spring Cloud系列(四):Eureka源码解析之客户端

    一.自动装配 1.根据自动装配原理(详见:Spring Boot系列(二):Spring Boot自动装配原理解析),找到spring-cloud-netflix-eureka-client.jar的 ...

随机推荐

  1. java服务端技术

    服务端框架:1.servlet2.netty 协议:1.http 1.02.http 1.1 数据库:mysql 对象关系映射(ORM)框架:mybatis 缓存:redis eclipse能运行,导 ...

  2. Kprobes

    https://landley.net/kdocs/ols/2007/ols2007v1-pages-215-224.pdf https://www.kernel.org/doc/Documentat ...

  3. 【Android】事件处理系统

    linux输入子系统 Android是linux内核的,所以它的事件处理系统也在linux的基础上完成的. Linux内核提供了一个Input子系统来实现的,Input子系统会在/dev/input/ ...

  4. 利用jenkins的api来完成相关工作流程的自动化

    [本文出自天外归云的博客园] 背景 1. 实际工作中涉及到安卓客户端方面的测试,外推或运营部门经常会有很多的渠道,而每个渠道都对应着一个app的下载包,这些渠道都记录在安卓项目下的一个渠道列表文件中. ...

  5. Python与操作系统有关的模块

    Os模块 Python的标准库中的os模块主要涉及普遍的操作系统功能.可以在Linux和Windows下运行,与平台无关.os.sep 可以取代操作系统特定的路径分割符.os.name字符串指示你正在 ...

  6. 为python 添加新功能-dump

    一直觉得thinkphp提供的dump函数挺好用的,但是python里面没有,就一直想着写个简单的. dir是我比较常用的一个内置函数了,但是显示效果实在有点受不了,每次我都要从大量的字符串里找到我需 ...

  7. 基于jQuery滑动分步式进度导航条代码

    分享一款基于jQuery滑动分步式进度导航条代码.这是一款基于jquery实现的网站注册动态步骤导航条代码.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div id=& ...

  8. VMWare: eth0: error fetching interface information : device not found

    VMWare: eth0: error fetching interface information : device not found  今天在VMware上新搭建的Redhat Linux 64 ...

  9. git pull出现There is no tracking information for the current branch

    使用git pull 或者 git push 的时候报错 gitThere is no tracking information for the current branch. Please spec ...

  10. python学习笔记十四:wxPython Demo

    一.简介 wxPython是Python语言的一套优秀的GUI图形库,允许Python程序员很方便的创建完整的.功能键全的GUI用户界面. wxPython是作为优秀的跨平台GUI库wxWidgets ...