SpringCloudAlibaba整合Sleuth+Zipkin

有关Sleuth之前有写过两篇文章

Spring Cloud Alibaba(13)---Sleuth概述

Spring Cloud Alibaba(14)---SpringCloudAlibaba整合Sleuth

上篇文章我们通过打印日志的方式,看到一个请求的完整链路。但是当微服务越来越多日志文件也会越来越多,查询工作会变得越来越麻烦,所以这篇我们通过 Zipkin 进行

链路跟踪。Zipkin 可以将日志聚合,并进行可视化展示和全文检索

一、Zipkin客户端搭建

1、快速开始

Zipkin是通过jar的方式运行的,所以我们去官方下载相应的jar包

https://github.com/openzipkin/zipkin

Quick-start 下面的链接直接下载官方提供的jar包,当前的版本是 zipkin-server-2.23.2-exec.jar,通过java命令运行。

java -jar zipkin-server-2.23.2-exec.jar

启动之后访问zipkin地址

http://127.0.0.1:9411/zipkin/

可以看出,Zipkin的客户端已经搭建好了。

接下来我们在我们项目中添加相关zipkin配置。

二、SpringCloudAlibaba整合Zipkin

注意 这篇也是在上篇SpringCloudAlibaba已经整合Sleuth的基础上在整合Zipkin,所以这里也不把所有代码都复制在这里,完整项目代码会放到github上。

1、pom.xml

在需要进行链路追踪的项目中(服务网关商品服务订单服务)添加 spring-cloud-starter-zipkin 依赖。

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

2、application.yml

spring:
zipkin:
base-url: http://127.0.0.1:9411/ #zipkin地址
discovery-client-enabled: false #不用开启服务发现
sleuth:
sampler:
probability: 1.0 #采样百分比

这里采样百分比说下: 如果服务的流量很大,全部采集对传输、存储压力比较大。这个时候可以设置采样率(如配置为1.0,则采样率为100%,采集服务的全部追踪数据),

若不配置默认采样率是0.1(即10%)。这里为了等下演示更明显,所以设置100%抽取。

3、测试

这里整个请求链路如下图:

这里一个完整的链路是 一个请求通过网关服务,然后转发到 订单微服务,然后订单微服务中会去调商品服务

访问地址如下:

#通过网关访问订单服务
http://localhost:8001/mall-order/api/v1/goods_order/getGoodsByFeign?goodsId=1

请求已经成功,接下来我们来看zipkin客户端

发现客户端已经有一条链路,而且顺序是 mall-gateway (3) -> mall-order (2) -> mall-goods (1) 跟我们实际请求的顺序吻合。

同时我们还可以看具体每个微服务的执行时间

从这个图可以看出整个请求链路总共花多少时间,每个微服务的执行花费时间。这样在实际生产场景中如果当前请求响应很慢,那我们可以通过zipkin客户端就就可以看出哪个服务

执行时间较慢,进而优化。

三、 Zipkin+Mysql持久化

Zipkin Server 默认存储追踪数据至内存中,这种方式并不适合生产环境,一旦 Server 关闭重启或者服务崩溃,就会导致历史数据消失。Zipkin 支持修改存储策略使用其他

存储组件,支持 MySQL,Elasticsearch 等。

1、Mysql数据库脚本

打开 MySQL 数据库,创建 zipkin 库,执行以下 SQL 脚本。

官网地址:https://github.com/openzipkin/zipkin/blob/master/zipkin-storage/mysql-v1/src/main/resources/mysql.sql

所有sql如下

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;

2、部署Zipkin

上面是我们是通过直接 java -jar zipkin-server-2.23.2-exec.jar 启动的,这里我们在启动的时候去指定Mysql配置信息

java -jar zipkin-server-2.23.2-exec.jar --STORAGE_TYPE=mysql --MYSQL_HOST=localhost --MYSQL_TCP_PORT=3306 --MYSQL_USER=root --MYSQL_PASS=root --MYSQL_DB=zipkin

启动之后我们再访问上面的接口

#通过网关访问订单服务
http://localhost:8001/mall-order/api/v1/goods_order/getGoodsByFeign?goodsId=1

请求成功后,我们在来看数据库

很明显数据库已经把数据固化下来了,这样的话就算我们重启zipkin,数据都不会丢失,客户端一直可以查询到。

总结 Spring Cloud Alibaba 系列文章到这里先告一段落了,后期在开发中遇到实际生产问题,在来总结。

github地址 nacos-feign-sentinel-gatway-sleuth

少说多做,句句都会得到别人的重视;多说少做,句句都会受到别人的忽视。(15)

Spring Cloud Alibaba(15)---Sleuth+Zipkin的更多相关文章

  1. Spring Cloud Alibaba(13)---Sleuth概述

    Sleuth概述 前言 在微服务架构中,众多的微服务之间互相调用,如何清晰地记录服务的调用链路是一个需要解决的问题.同时,由于各种原因,跨进程的服务调用失败时,运维人员希望能够通过 查看日志和查看服务 ...

  2. spring cloud 学习(8) - sleuth & zipkin 调用链跟踪

    业务复杂的微服务架构中,往往服务之间的调用关系比较难梳理,一次http请求中,可能涉及到多个服务的调用(eg: service A -> service B -> service C... ...

  3. Spring Cloud Alibaba(14)---SpringCloudAlibaba整合Sleuth

    SpringCloudAlibaba整合Sleuth 上一篇有写过Sleuth概述,Spring Cloud Alibaba(13)---Sleuth概述 这篇我们开始通过示例来演示链路追踪. 一.环 ...

  4. Spring Cloud Alibaba 实战(十三) - Sleuth调用链监控

    本文概要:大白话剖析调用链监控原理,然后学习Sleuth,Zipkin,然后将Sleuth整合Zipkin,最后学习Zipkin数据持久化(Elasticsearch)以及Zipkin依赖关系图 实战 ...

  5. Spring Cloud Alibaba学习笔记(15) - 整合Spring Cloud Gateway

    Spring Cloud Gateway 概述 Spring Cloud Gateway 是 Spring Cloud 的一个全新项目,该项目是基于Netty.Reactor以及WEbFlux构建,它 ...

  6. Spring Cloud Alibaba入门篇

    学习条件 了解web三层架构 熟练应用SSM架构 了解Maven管理工具的使用 熟练使用SpringBoot,以及了解SpringBoot基本原理. 了解部分术语:应用.工具.耦合.负载等 温馨提示: ...

  7. Spring Cloud Alibaba 之 user服务

    项目技术选型 Spring Boot Spring MVC MyBatis + 通用Mapper (官网信息https://mapperhelper.github.io/docs/) Spring C ...

  8. Spring Cloud Alibaba基础教程:Nacos配置的多环境管理

    前情回顾: <Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现> <Spring Cloud Alibaba基础教程:支持的几种服务消费方式> ...

  9. Spring Cloud Alibaba | Sentinel: 分布式系统的流量防卫兵初探

    目录 Spring Cloud Alibaba | Sentinel: 分布式系统的流量防卫兵初探 1. Sentinel 是什么? 2. Sentinel 的特征: 3. Sentinel 的开源生 ...

随机推荐

  1. 初入MongoDB

    初入MongoDB 业务需求,需要用到MongoDB.向来一直是mysql数据库的思想,一下转换为nosql还是很不适应.经过一个月的开发,写一下自己的感触.本文会对应mysql数据库进行说明. 数据 ...

  2. Day001 Typora Markdown语法学习

    # Markdown语法 ## 标题 ### 三级标题 #### 四级标题 注:最多支持到六级标题 ## 字体 **hello,world!** *hello,world!* ***hello,wor ...

  3. Wampserver-添加虚拟主机

    鼠标左键点击,之后点击第一个 localhost(有一个小房子) 添加虚拟地址 具体添加 完成界面 注意:这个时候一定需要重启一个Wampserver64 如果没有重启直接进入4这个步骤,会发现进入的 ...

  4. ERROR Invalid options in vue.config.js: "baseUrl" is not allowed

    vue项目 我的这个版本是 3.10.0 module.exports = { baseUrl: process.env.NODE_ENV === 'production' ? './' : '/' ...

  5. 【微信小程序】--小程序中循环遍历(包括js中遍历和wxml中的遍历)

    文章为博主原创,纯属个人见解,如有错误欢迎指出. 如需转载,请注明出处. 在js中遍历 for (var index in res.data.infos) { res.data.infos[index ...

  6. SDK安全测试

    设备调试 strace MI 5X 链接:https://pan.baidu.com/s/1KfsfEgjniozXGUD_69m0SQ 提取码:mulo 推strace到手机中 adb push s ...

  7. Python实现TCP通讯

    Environment Client:Windows Server:KaLi Linux(VM_virtul) Network:Same LAN Client #!/usr/bin/python3 # ...

  8. Go语言web开发---Beego的session

    一.简介 Session是一段保存在服务器上的信息,当客户端第一次访问服务器时创建Session,同时也会创建一个名为beegosessionID,值为创建的Session的id的Cookie. 这个 ...

  9. setTimeout使用问题

    通常禁止使用setTimeout的情况: 1.不能用于模拟异步,有的人不熟悉异步流程,而使用setTimeout伪实现,比如设置一个 setTimeout 来等待函数执行完毕,正确做法是使用回调来处理 ...

  10. CMOS图像传感器理解

    CMOS图像传感器理解 水流方向从左边流向右边,上面有一个开关,拧下去的时候水流停止,拧上去的时候水流打开.左边是水流的源头我们给它起个名字,叫做源端(就是源头的意思嘛),右边是水流出去的地方,也就是 ...