• 在api_gateway_server当中添加

微服务链路追踪SkyWalking_spring cloud

logging:

level:

root: INFO

org.springframework.web.servlet.DispatcherServlet: DEBUG

org.springframework.cloud.sleuth: DEBUG

  • 在order_service当中

微服务链路追踪SkyWalking_spring cloud_02

logging:

level:

root: INFO

org.springframework.web.servlet.DispatcherServlet: DEBUG

org.springframework.cloud.sleuth: DEBUG

微服务链路追踪SkyWalking_SQL_03

logging:

level:

root: INFO

org.springframework.web.servlet.DispatcherServlet: DEBUG

org.springframework.cloud.sleuth: DEBUG

每个微服务都需要添加如上的配置。

运行测试

微服务链路追踪SkyWalking_微服务链路追踪SkyWalking_04

访问http://localhost:8080/order-service/order/buy/1

微服务链路追踪SkyWalking_java_05

查看控制台输出的日志信息

  • GatewayServerApplication :8080/

微服务链路追踪SkyWalking_微服务链路追踪SkyWalking_06

  • OrderApplication :9002/

微服务链路追踪SkyWalking_微服务_07

  • ProductApplication :9001/

微服务链路追踪SkyWalking_微服务链路追踪SkyWalking_08

启动微服务,调用之后,我们可以在控制台观察到sleuth的日志输出。

微服务链路追踪SkyWalking_spring cloud_09

微服务链路追踪SkyWalking_java_10

微服务链路追踪SkyWalking_SQL_11

上述三个日志信息都有:fa49799c056547b9,fa49799c056547b9,false]

其中 fa49799c056547b9是TraceId,后面跟着的是SpanId,依次调用有一个全局的TraceId,将调用链路串起来。

仔细分析每个微服务的日志,不难看出请求的具体过程。

查看日志文件并不是一个很好的方法,当微服务越来越多日志文件也会越来越多,通过Zipkin可以将日志聚合,并进行可视化展示和全文检索。

二、 Zipkin


1、 Zipkin的概述

Zipkin 是 Twitter 的一个开源项目,它基于 Google Dapper 实现,它致力于收集服务的定时数据,以解决微服务架构中的延迟问题,包括数据的收集、存储、查找和展现。

我们可以使用它来收集各个服务器上请求链路的跟踪数据,并通过它提供的 REST API 接口来辅助我们查询跟踪数据以实现对分布式系统的监控程序,

从而及时地发现系统中出现的延迟升高问题并找出系统性能瓶颈的根源。

除了面向开发的 API 接口之外,它也提供了方便的 UI 组件来帮助我们直观的搜索跟踪信息和分析请求链路明细,

比如:可以查询某段时间内各用户请求的处理时间等。

Zipkin 提供了可插拔数据存储方式:In-MemoryMySqlCassandra 以及 Elasticsearch

微服务链路追踪SkyWalking_微服务链路追踪SkyWalking_12

上图展示了 Zipkin 的基础架构,它主要由 4 个核心组件构成:

  • Collector:收集器组件,它主要用于处理从外部系统发送过来的跟踪信息,将这些信息转换为Zipkin 内部处理的 Span 格式,以支持后续的存储、分析、展示等功能。
  • Storage:存储组件,它主要对处理收集器接收到的跟踪信息,默认会将这些信息存储在内存中,

我们也可以修改此存储策略,通过使用其他存储组件将跟踪信息存储到数据库中。

  • RESTful API:API 组件,它主要用来提供外部访问接口。比如给客户端展示跟踪信息,或是外接

系统访问以实现监控等。

  • Web UI:UI 组件,基于 API 组件实现的上层应用。通过 UI 组件用户可以方便而有直观地查询和分析跟踪信息。

Zipkin 分为两端,一个是 Zipkin 服务端,一个是 Zipkin 客户端,客户端也就是微服务的应用。

客户端会配置服务端的 URL 地址,一旦发生服务间的调用的时候,会被配置在微服务里面的 Sleuth 的监听器监听,并生成相应的 Trace 和 Span 信息发送给服务端。

发送的方式主要有两种,一种是 HTTP 报文的方式,还有一种是消息总线的方式如 RabbitMQ。

不论哪种方式,我们都需要:

  • 一个 Eureka 服务注册中心,这里我们就用之前的 eureka 项目来当注册中心。
  • 一个 Zipkin 服务端。
  • 多个微服务,这些微服务中配置Zipkin 客户端。

2、 Zipkin Server的部署和配置

(1)Zipkin Server下载与启动
  • 下载

从spring boot 2.0开始,官方就不再支持使用自建Zipkin Server的方式进行服务链路追踪,而是直接提供了编译好的 jar 包来给我们使用。

下载链接:https://search.maven.org/remote_content?g=io.zipkin.java&a=zipkin-server&v=LATEST&c=exec

可以从官方网站下载先下载Zipkin的web UI,我们这里下载的是

zipkin-server-2.12.9-exec.jar

  • 启动

在命令行输入 java -jar zipkin-server-2.12.9-exec.jar 启动 Zipkin Server

微服务链路追踪SkyWalking_微服务链路追踪SkyWalking_13

默认Zipkin Server的请求端口为 9411

Zipkin Server的启动参数可以通过官方提供的yml配置文件查找

配置文件查看地址:https://github.com/openzipkin/zipkin/blob/master/zipkin-server/src/main/resources/zipkin-server-shared.yml

在浏览器输入 http://127.0.0.1:9411即可进入到Zipkin Server的管理后台

微服务链路追踪SkyWalking_微服务_14

三、 客户端Zipkin+Sleuth整合


通过查看日志分析微服务的调用链路并不是一个很直观的方案,结合zipkin可以很直观地显示微服务之间的调用关系。

1、客户端添加依赖

客户端指的是需要被追踪的微服务

  • api_gateway_server

微服务链路追踪SkyWalking_java_15

  • order_service

微服务链路追踪SkyWalking_微服务链路追踪SkyWalking_16

  • product_service

微服务链路追踪SkyWalking_java_17

org.springframework.cloud

spring-cloud-starter-zipkin

2、修改客户端配置文件

  • api_gateway_server

微服务链路追踪SkyWalking_SQL_18

  • order_service

微服务链路追踪SkyWalking_SQL_19

  • product_service

微服务链路追踪SkyWalking_SQL_20

zipkin:

base-url: http://127.0.0.1:9411/ #zipkin server的请求地址

sender:

type: web #请求方式,默认以http的方式向zipkin server发送追踪数据

sleuth:

sampler:

probability: 1.0 #采样的百分比

指定了zipkin server的地址,下面制定需采样的百分比,

默认为0.1,即10%,这里配置1,是记录全部的sleuth信息,是为了收集到更多的数据(仅供测试用)。

在分布式系统中,过于频繁的采样会影响系统性能,所以这里配置需要采用一个合适的值。

3、重新启动运行测试

微服务链路追踪SkyWalking_微服务链路追踪SkyWalking_21

http://localhost:8080/order-service/order/buy/1

微服务链路追踪SkyWalking_SQL_22

微服务链路追踪SkyWalking_java_23

查询指定的链路

微服务链路追踪SkyWalking_微服务链路追踪SkyWalking_24

微服务链路追踪SkyWalking_微服务链路追踪SkyWalking_25

微服务链路追踪SkyWalking_spring cloud_26

微服务链路追踪SkyWalking_SQL_27

因为数据是保存到内存当中的所有Zipkin重启数据就会丢失

四、 Zipkin服务端数据保存到mysql


微服务链路追踪SkyWalking_微服务_28

1、准备MYSQL数据库和表(官方提供好的)

SQL语句

/*
SQLyog Ultimate v11.33 (64 bit)
MySQL - 5.5.58 : Database - zipkin
*/
/*!40101 SET NAMES utf8 */;
/!40101 SET SQL_MODE=‘’/;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE=‘NO_AUTO_VALUE_ON_ZERO’ */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /!32312 IF NOT EXISTS/zipkin /*!40100 DEFAULT CHARACTER SET utf8 */;
USE zipkin;
/*Table structure for table zipkin_annotations */
DROP TABLE IF EXISTS zipkin_annotations;
CREATE TABLE zipkin_annotations (
trace_id_high bigint(20) NOT NULL DEFAULT ‘0’ COMMENT ‘If non zero, this means the trace uses 128 bit traceIds instead of 64 bit’,
trace_id bigint(20) NOT NULL COMMENT ‘coincides with zipkin_spans.trace_id’,
span_id bigint(20) 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(11) NOT NULL COMMENT ‘BinaryAnnotation.type() or -1 if Annotation’,
a_timestamp bigint(20) DEFAULT NULL COMMENT ‘Used to implement TTL; Annotation.timestamp or zipkin_spans.timestamp’,
endpoint_ipv4 int(11) DEFAULT NULL COMMENT ‘Null when Binary/Annotation.endpoint is null’,
endpoint_ipv6 binary(16) DEFAULT NULL COMMENT ‘Null when Binary/Annotation.endpoint is null, or no IPv6 address’,
endpoint_port smallint(6) DEFAULT NULL COMMENT ‘Null when Binary/Annotation.endpoint is null’,
endpoint_service_name varchar(255) DEFAULT NULL COMMENT ‘Null when Binary/Annotation.endpoint is null’,
UNIQUE KEY trace_id_high (trace_id_high,trace_id,span_id,a_key,a_timestamp) COMMENT ‘Ignore insert on duplicate’,
KEY trace_id_high_2 (trace_id_high,trace_id,span_id) COMMENT ‘for joining with zipkin_spans’,
KEY trace_id_high_3 (trace_id_high,trace_id) COMMENT ‘for getTraces/ByIds’,
KEY endpoint_service_name (endpoint_service_name) COMMENT ‘for getTraces and getServiceNames’,
KEY a_type (a_type) COMMENT ‘for getTraces’,
KEY a_key (a_key) COMMENT ‘for getTraces’,
KEY trace_id (trace_id,span_id,a_key) COMMENT ‘for dependencies job’
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPRESSED;
/*Data for the table zipkin_annotations */
/*Table structure for table zipkin_dependencies */
DROP TABLE IF EXISTS zipkin_dependencies;
CREATE TABLE zipkin_dependencies (
day date NOT NULL,
parent varchar(255) NOT NULL,
child varchar(255) NOT NULL,
call_count bigint(20) DEFAULT NULL,
UNIQUE KEY day (day,parent,child)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPRESSED;
/*Data for the table zipkin_dependencies */
/*Table structure for table zipkin_spans */
DROP TABLE IF EXISTS zipkin_spans;
CREATE TABLE zipkin_spans (
trace_id_high bigint(20) NOT NULL DEFAULT ‘0’ COMMENT ‘If non zero, this means the trace uses 128 bit traceIds instead of 64 bit’,
trace_id bigint(20) NOT NULL,
id bigint(20) NOT NULL,
name varchar(255) NOT NULL,
parent_id bigint(20) DEFAULT NULL,
debug bit(1) DEFAULT NULL,
start_ts bigint(20) DEFAULT NULL COMMENT ‘Span.timestamp(): epoch micros used for endTs query and to implement TTL’,
duration bigint(20) DEFAULT NULL COMMENT ‘Span.duration(): micros used for minDuration and maxDuration query’,
UNIQUE KEY trace_id_high (trace_id_high,trace_id,id) COMMENT ‘ignore insert on duplicate’,
KEY trace_id_high_2 (trace_id_high,trace_id,id) COMMENT ‘for joining with zipkin_annotations’,
KEY trace_id_high_3 (trace_id_high,trace_id) COMMENT ‘for getTracesByIds’,
KEY name (name) COMMENT ‘for getTraces and getSpanNames’,
KEY start_ts (start_ts) COMMENT ‘for getTraces ordering and range’
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPRESSED;
/*Data for the table zipkin_spans */
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

微服务链路追踪SkyWalking_微服务链路追踪SkyWalking_29

2、修改server的启动命令

微服务链路追踪SkyWalking_spring cloud_30

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

  • STORAGE_TYPE : 存储类型
  • MYSQL_HOST: mysql主机地址