第九节:SpringCloud 分布式链路跟踪Sleuth

spring cloud sleuth

微服务情况下的系统存在的问题

1)我们通过上述讲了很多spring cloud的服务组件,可以使用以前的组件搭建一个很健壮的系统
微服务架构是一个分布式架构,它按业务划分服务单元,一个分布式系统往往有很多个服务单元。由于服务单元数量众多,业务的复杂性,如果出现了错误和异常,很难去定位。主要体现在,一个请求可能需要调用很多个服务,而内部服务的调用复杂性,决定了问题难以定位。所以微服务架构中,必须实现分布式链路追踪,去跟进一个请求到底有哪些服务参与,参与的顺序又是怎样的,从而达到每个请求的步骤清晰可见,出了问题,很快定位
在这里插入图片描述

spring cloud sleuth是什么?

Spring Cloud Sleuth为Spring Cloud提供了分布式跟踪的解决方案,
它大量借用了Google Dapper、Twitter Zipkin和Apache HTrace的设计
Span(跨度,或者微服务节点)是工作的最基本的一个工作单元
trace 一组共享“root span”的span组成的树状结构称为trace。trace也用一个64位的ID唯一标识,trace中的所有span都共享该trace的ID
Annotation(标记)
cs(client send)客户端发送 - 客户端已经发出请求。此注释描绘了跨度的开始
sr(server received)服务器接收 - 服务器端得到请求,并将开始处理它
ss (server send)服务器发送 - 在完成请求处理后(响应发送回客户端时)注释
cr(client received)客户端接收 - 表示跨度的结束。客户端已成功接收到服务器端的响应
如何计算网络延时
请求延时=sr-cs
响应延时 cr-ss
如何计算服务器处理时间:ss-sr
如何计算客户端请求时间:cr-cs=请求延时+服务器处理时间+响应延时=(sr-cs)+(ss-sr)+(cr-ss)

快速开始
创建工程
加入依赖sleuth的依赖

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

修改配置文件applition.yml

logging.level.org.springframework.cloud.sleuth=debug

sleuth+zipkin

什么是zipkin

Zipkin是Twitter开源的分布式跟踪系统,基于Dapper的论文设计而来。它的主要功能是收集系统的时序数据,从而追踪微服务架构的系统延时等问题。Zipkin还提供了一个非常友好的界面,帮助我们分析追踪数据

为啥要使用zipkin

第一:若我们仅仅使用sleuth的话,我们分析问题仅仅就是通过查询控制台的日志来分析,这样十分的不友好

zipkin服务端
springboot1.X
我们需要单独的搭建一个zipkin server工程
加依赖

<dependency>
  <groupId>io.zipkin.java</groupId>
  <artifactId>zipkin-autoconfigure-ui</artifactId>
</dependency>
<dependency>
  <groupId>io.zipkin.java</groupId>
  <artifactId>zipkin-server</artifactId>
</dependency>

写注解

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

springboot2.X
在使用 Spring Boot 2.x 版本后,官方就不推荐自行定制编译了,而是直接提供了编译好的 jar 包来给我们使用,所以在最新版本的 Spring Cloud 依赖管理里已经找不到 zipkin-server
Zipkin 服务端,在使用 Spring Boot 2.x 版本后,官方就不推荐自行定制编译了,而是直接提供了编译好的 jar 包来给我们使用(就是前面我们通过一键脚本下载安装的Zipkin),详情请看 https://github.com/openzipkin/zipkin/issues/1962 : upgrade to Spring Boot 2.0 NoClassDefFoundError UndertowEmbeddedServletContainerFactory #1962

If you decide to make a custom server, you accept responsibility for troubleshooting your build or configuration problems, even if such problems are a reaction to a change made by the OpenZipkin maintainers. In other words, custom servers are possible, but not supported.

EnableZipkinServer.java:https://github.com/openzipkin/zipkin/blob/master/zipkin-server/src/main/java/zipkin/server/EnableZipkinServer.java

简而言之就是:私自改包,后果自负。
下载链接https://search.maven.org/remote_content?g=io.zipkin.java&a=zipkin-server&v=LATEST&c=exec
通过java -jar 的方式启动server端,然后点击访问:http://localhost:9411/zipkin/

zipkin client端
加依赖

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

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

修改配置文件

#zipkin server的地址
spring.zipkin.base-url=http://localhost:9411/
spring.sleuth.web.client.enabled=true
#采样比例默认是0.1 为1表示全部上报
spring.sleuth.sampler.probability=1

通过调用来观察zipkin server的面板的变化

sleuth+zipkin+rabbitmq进行上报

使用linux 格式启动RABBIT_ADDRESSES=47.104.128.12 java -jar zipkin-server-2.11.1-exec.jar
客户端修改8001,8992
添加jar

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>

修改application.yml文件

spring.zipkin.sender.type=rabbit
spring.sleuth.web.client.enabled=true
#采样比例默认是0.1 为1表示全部上报
spring.sleuth.sampler.probability=1
spring.rabbitmq.host=47.104.128.12
spring.rabbitmq.port=5672
spring.rabbitmq.password=guest
spring.rabbitmq.username=guest
spring.rabbitmq.virtual-host=/

zipkin服务端下载的jar包:https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/

sleuth+zipkin使用mysql作后端存储

1、第一步,创建一个数据库名为zipkin的db

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`),
  KEY `trace_id_high_2` (`trace_id_high`,`trace_id`,`span_id`),
  KEY `trace_id_high_3` (`trace_id_high`,`trace_id`),
  KEY `endpoint_service_name` (`endpoint_service_name`),
  KEY `a_type` (`a_type`),
  KEY `a_key` (`a_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPRESSED;




CREATE TABLE `zipkin_dependencies` (
  `day` date NOT NULL,
  `parent` varchar(255) NOT NULL,
  `child` varchar(255) NOT NULL,
  `call_count` bigint(20) DEFAULT NULL,
  `error_count` varchar(255) DEFAULT NULL,
  UNIQUE KEY `day` (`day`,`parent`,`child`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPRESSED;


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`),
  UNIQUE KEY `trace_id_high_4` (`trace_id_high`,`trace_id`,`id`),
  KEY `trace_id_high_2` (`trace_id_high`,`trace_id`,`id`),
  KEY `trace_id_high_3` (`trace_id_high`,`trace_id`),
  KEY `trace_id_high_5` (`trace_id_high`,`trace_id`,`id`),
  KEY `trace_id_high_6` (`trace_id_high`,`trace_id`),
  KEY `name` (`name`),
  KEY `start_ts` (`start_ts`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPRESSED;

2、zipkin server启动java -jar zipkin-server-2.11.1-exec.jar --STORAGE_TYPE=mysql --MYSQL_DB=zipkin --MYSQL_USER=root --MYSQL_PASS=123456 --MYSQL_HOST=47.104.128.12 --MYSQL_TCP_PORT=3306

3、调用接口:http://localhost:800/hello
4、查看数据库数据
5、重启zipkin server
6、还是能看到保存的数据

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值