天天看点

SpringCloud Alibaba系列 Sleuth Zipkin(五)什么是链路追踪?链路追踪的作用SpringCloud 整合 Sleuth Zipkin 链路追踪可视化工具SpringCloud 整合 ZipkinZipkin 持久化到 mysql

目录

什么是链路追踪?

链路追踪的作用

SpringCloud 整合 Sleuth 

Zipkin 链路追踪可视化工具

部署 zipkin 服务端

从 Github 上下载 Zipkin

启动 Zipkin 服务

SpringCloud 整合 Zipkin

Zipkin 持久化到 mysql

Sleuth 官网:https://cloud.spring.io/spring-cloud-sleuth/reference/html

Zipkin 官网:https://zipkin.io

什么是链路追踪?

即调用链监控,特点是通过记录多个在请求间跨服务完成的逻辑请求信息,帮助开发人员优化性能和进行问题追踪。链路追踪可以捕获每个请求遇到的异常和错误,以及即时信息和有价值的数据。

链路追踪的作用

随着微服务应用数量的极速增加,服务与服务链路之间的调用关系也变得错综复杂。此时,我们也会碰到各种难题。
  • 系统出现问题后,由于服务链路过长或过于复杂,无法快速准确定位问题。客户端(如浏览器)或者移动端应用报出异常或者错误,也无法确定是哪个服务抛出的异常。
  • 某个业务请求非常慢,且总是超时,无法确定系统哪个环节存在性能的问题。
  • 如何快速发现问题?可以通过调用链结合业务日志快速定位错误信息。
  • 如何判断故障影响范围?各个阶段链路耗时、服务依赖关系可以通过可视化界面展现出来,从而直观地审视故障的影响范围。

SpringCloud 整合 Sleuth 

pom.xml 中添加依赖

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
           

启动微服务项目,随便请求一个接口,我们可以看到 控制台 输出的 日志带了上链路信息,如下图

SpringCloud Alibaba系列 Sleuth Zipkin(五)什么是链路追踪?链路追踪的作用SpringCloud 整合 Sleuth Zipkin 链路追踪可视化工具SpringCloud 整合 ZipkinZipkin 持久化到 mysql

参数说明:

[order-service,ed01c90c22d328fe,775eef639523a898]
  • 参数一:order-service 表示微服务名称,spring.application.name 的值
  • 参数二:ed01c90c22d328fe 表示 本次请求的链路追踪 id(Trace ID),本次请求涉及多个下游服务,链路追踪 id 相同,表示这些都是同一次追踪
  • 参数三:775eef639523a898 表示 当前服务本次链路追踪的 id(Span ID),本次请求涉及多个下游服务,每个微服务的 Span ID 都不相同,代表不同的服务。

Zipkin 链路追踪可视化工具

上面可以看到整合Sleuth后有日志打印出来,但是没有页面让我们看到具体的链路追踪信息。Sleuth 结合 Zipkin 就可以把 链路追踪信息通过网页可视化的方式直观的展现 

部署 zipkin 服务端

  • 从 Github 上下载 Zipkin

https://github.com/openzipkin/zipkin

SpringCloud Alibaba系列 Sleuth Zipkin(五)什么是链路追踪?链路追踪的作用SpringCloud 整合 Sleuth Zipkin 链路追踪可视化工具SpringCloud 整合 ZipkinZipkin 持久化到 mysql
  • 启动 Zipkin 服务

注意:启动 Zipkin 需要 JDK 版本为 1.8 及以上版本

java -jar zipkin-server-2.23.19-exec.jar

Zipkin 默认端口 9411

访问地址:http://ip:9411/zipkin

SpringCloud Alibaba系列 Sleuth Zipkin(五)什么是链路追踪?链路追踪的作用SpringCloud 整合 Sleuth Zipkin 链路追踪可视化工具SpringCloud 整合 ZipkinZipkin 持久化到 mysql

SpringCloud 整合 Zipkin

pom.xml 中添加依赖

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-zipkin</artifactId>
    <version>2.2.8.RELEASE</version>	<!--zipkin 是第三方的,版本和 spring cloud 不同步,所以要指定版本-->
</dependency>
           

application.yml 文件配置 zipkin

spring:
  zipkin:
    # 配置 zipkin 服务所在地址
    base-url: http://localhost:9411
    # 不开启服务发现
    discovery-client-enabled: false
  
  sleuth:
    sampler:
      # 配置 sleuth 链路追踪采样 百分比,1:百分百,0.1:百分之十。采样比例过高影响性能
      probability: 0.1
           

配置好后重启所有的微服务,然后随便请求一个接口,查看 Zipkin 可视化页面

SpringCloud Alibaba系列 Sleuth Zipkin(五)什么是链路追踪?链路追踪的作用SpringCloud 整合 Sleuth Zipkin 链路追踪可视化工具SpringCloud 整合 ZipkinZipkin 持久化到 mysql

点进去一个链路,可以看到请求的链路耗时,以及调用的类、接口、方法、服务ip端口、集群节点等信息

SpringCloud Alibaba系列 Sleuth Zipkin(五)什么是链路追踪?链路追踪的作用SpringCloud 整合 Sleuth Zipkin 链路追踪可视化工具SpringCloud 整合 ZipkinZipkin 持久化到 mysql

以及查看依赖链路

SpringCloud Alibaba系列 Sleuth Zipkin(五)什么是链路追踪?链路追踪的作用SpringCloud 整合 Sleuth Zipkin 链路追踪可视化工具SpringCloud 整合 ZipkinZipkin 持久化到 mysql

Zipkin 持久化到 mysql

  • 数据库中创建 zipkin 数据脚本

脚本下载地址在 github zipkin项目里的 zipkin-storage/mysql-v1/src/main/resounce 目录下 https://github.com/openzipkin/zipkin/tree/master/zipkin-storage/mysql-v1/src/main/resources

或者直接拷贝

--
-- Copyright 2015-2019 The OpenZipkin Authors
--
-- Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
-- in compliance with the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software distributed under the License
-- is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
-- or implied. See the License for the specific language governing permissions and limitations under
-- the License.
--

CREATE TABLE IF NOT EXISTS zipkin_spans (
  `trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit',
  `trace_id` BIGINT NOT NULL,
  `id` BIGINT NOT NULL,
  `name` VARCHAR(255) NOT NULL,
  `remote_service_name` VARCHAR(255),
  `parent_id` BIGINT,
  `debug` BIT(1),
  `start_ts` BIGINT COMMENT 'Span.timestamp(): epoch micros used for endTs query and to implement TTL',
  `duration` BIGINT COMMENT 'Span.duration(): micros used for minDuration and maxDuration query',
  PRIMARY KEY (`trace_id_high`, `trace_id`, `id`)
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;

ALTER TABLE zipkin_spans ADD INDEX(`trace_id_high`, `trace_id`) COMMENT 'for getTracesByIds';
ALTER TABLE zipkin_spans ADD INDEX(`name`) COMMENT 'for getTraces and getSpanNames';
ALTER TABLE zipkin_spans ADD INDEX(`remote_service_name`) COMMENT 'for getTraces and getRemoteServiceNames';
ALTER TABLE zipkin_spans ADD INDEX(`start_ts`) COMMENT 'for getTraces ordering and range';

CREATE TABLE IF NOT EXISTS zipkin_annotations (
  `trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit',
  `trace_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.trace_id',
  `span_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.id',
  `a_key` VARCHAR(255) NOT NULL COMMENT 'BinaryAnnotation.key or Annotation.value if type == -1',
  `a_value` BLOB COMMENT 'BinaryAnnotation.value(), which must be smaller than 64KB',
  `a_type` INT NOT NULL COMMENT 'BinaryAnnotation.type() or -1 if Annotation',
  `a_timestamp` BIGINT COMMENT 'Used to implement TTL; Annotation.timestamp or zipkin_spans.timestamp',
  `endpoint_ipv4` INT COMMENT 'Null when Binary/Annotation.endpoint is null',
  `endpoint_ipv6` BINARY(16) COMMENT 'Null when Binary/Annotation.endpoint is null, or no IPv6 address',
  `endpoint_port` SMALLINT COMMENT 'Null when Binary/Annotation.endpoint is null',
  `endpoint_service_name` VARCHAR(255) COMMENT 'Null when Binary/Annotation.endpoint is null'
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;

ALTER TABLE zipkin_annotations ADD UNIQUE KEY(`trace_id_high`, `trace_id`, `span_id`, `a_key`, `a_timestamp`) COMMENT 'Ignore insert on duplicate';
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`, `span_id`) COMMENT 'for joining with zipkin_spans';
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`) COMMENT 'for getTraces/ByIds';
ALTER TABLE zipkin_annotations ADD INDEX(`endpoint_service_name`) COMMENT 'for getTraces and getServiceNames';
ALTER TABLE zipkin_annotations ADD INDEX(`a_type`) COMMENT 'for getTraces and autocomplete values';
ALTER TABLE zipkin_annotations ADD INDEX(`a_key`) COMMENT 'for getTraces and autocomplete values';
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id`, `span_id`, `a_key`) COMMENT 'for dependencies job';

CREATE TABLE IF NOT EXISTS zipkin_dependencies (
  `day` DATE NOT NULL,
  `parent` VARCHAR(255) NOT NULL,
  `child` VARCHAR(255) NOT NULL,
  `call_count` BIGINT,
  `error_count` BIGINT,
  PRIMARY KEY (`day`, `parent`, `child`)
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;
           

创建一个 zipkin 数据库,把脚本执行生成 三张表

SpringCloud Alibaba系列 Sleuth Zipkin(五)什么是链路追踪?链路追踪的作用SpringCloud 整合 Sleuth Zipkin 链路追踪可视化工具SpringCloud 整合 ZipkinZipkin 持久化到 mysql
  • 持久化启动 zipkin

使用如下命令

// java -jar zipkin-server-2.23.19-exec.jar --STORAGE_TYPE=mysql --MYSQL_HOST=IP地址 --MYSQL_TCP_PORT=端口 --MYSQL_DB=数据库名称 --MYSQL_USER=登录账号 --MYSQL_PASS=登录密码

java -jar zipkin-server-2.23.19-exec.jar --STORAGE_TYPE=mysql --MYSQL_HOST=127.0.0.1 --MYSQL_TCP_PORT=3306 --MYSQL_DB=zipkin --MYSQL_USER=root --MYSQL_PASS=123456
           
  • 测试 持久化是否成功

随便请求一个接口,查看数据库中是否有数据了

SpringCloud Alibaba系列 Sleuth Zipkin(五)什么是链路追踪?链路追踪的作用SpringCloud 整合 Sleuth Zipkin 链路追踪可视化工具SpringCloud 整合 ZipkinZipkin 持久化到 mysql

完成。