博客参考学习视频: https://www.bilibili.com/video/BV18E411x7eT?from=search&seid=4388336378730572330
上一篇:SpringCloud Stream 消息驱动 https://blog.youkuaiyun.com/qq_45738810/article/details/109239040
一、概述
① 为什么会出现这个技术? 需要解决哪些问题?
在微服务框架中,一个由客户端发起的请求在后端系统中会经过多个不同的服务点调用来协同产生最后的请求结果,每一个前端请求都会形成一条复杂的分布式服务调用链路,链路中的任何一环出现高延迟或错误都会引起整个请求最后的失败。
② 是什么
https://github.com/spring-cloud/spring-cloud-sleuth
Spring Cloud Sleuth 提供了一套完整的服务跟踪的解决方案,在分布式系统中提供追踪解决方案并且兼容支持了 zipkin。
③ 解决
二、搭建链路监控步骤
① zipkin 下载
- SpringCloud 从 F 版起已不需要自己构建 Zipkin server 了,只需要调用 jar 包即可
- https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/
- zipkin-server-2.12.9.exec.jar
运行 jar
运行控制台
- http://localhost:9411/zipkin/
- 术语
完整的调用链路
上图 what
名词解释
Trace: 类似于树结构的Span 集合,表示一条调用链路,存在唯一标识
span: 表示调用链路来源,通俗的理解 span 就是一次请求信息
② 服务提供者
cloud-provider-payment8001
POM
<!--包含了 sleuth+zipkin-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
YML
server:
port: 8001
spring:
application:
name: cloud-payment-service
zipkin:
base-url: http://localhost:9411
sleuth:
sampler:
#采样率值介于 0 到 1 之间,1 则表示全部采集
probability: 1
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
username: root
password: 6090
mybatis:
mapperLocations: classpath:mapper/*.xml
type-aliases-package: com.oy.springcloud.entities # 所有Entity别名类所在包
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
# 集群版
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
instance:
instance-id: payment8001
prefer-ip-address: true
业务类 PaymentController
// ====================> zipkin+sleuth
@GetMapping("/payment/zipkin")
public String paymentZipkin()
{
return "hi ,i'am paymentzipkin server fall back,welcome to atguigu,O(∩_∩)O哈哈~";
}
③ 服务消费者(调用方)
cloud-consumer-order80
POM
<!--包含了 sleuth+zipkin-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
YML
zipkin:
base-url: http://localhost:9411
sleuth:
sampler:
probability: 1
业务类 OrderController
@GetMapping("/consumer/payment/zipkin")
public String paymentZipkin()
{
String result =
restTemplate.getForObject("http://localhost:8001"+"/payment/zipkin/", String.class);
return result;
}
④ 依次启动 eureka7001/8001/80
80 调用 8001 几次测试下 : http://localhost/consumer/payment/zipkin
⑤ 打开浏览器访问:http:localhost:9411
会出现以下界面
查看依赖关系
原理
⑤ 打开浏览器访问:http:localhost:9411
会出现以下界面
[外链图片转存中…(img-kK5NjTgJ-1603429001501)]
[外链图片转存中…(img-X77scIx2-1603429001503)]
查看依赖关系
原理