微服务跟踪(sleuth)其实是一个工具,它在整个分布式系统中能跟踪一个用户请求的过程(包括数据采集,数据传输,数据存储,数据分析,数据可视化),捕获这些跟踪数据,就能构建微服务的整个调用链的视图,这是调试和监控微服务的关键工具。
在分布式系统中提供追踪解决方案并且兼容支持了zipkin
搭建链路监控步骤
1.zipkin
1)下载:SpringCloud从F版起已不需要自己构建Zipkin server了,只需要调用jar包即可
https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/2.12.9/
zipkin-server-2.12.9.exec.jar
链接: https://pan.baidu.com/s/1CaY0OHtyVZx27uToWxk_bQ 提取码: qiew
2)运行jar包:java -jar zipkin-server-2.12.9-exec.jar
3)http://localhost:9411/zipkin/
4)
Trace:类似于树结构的Span集合,表示一条调用链路,存在唯一标识
span:表示调用链路来源,通俗的理解span就是一次请求信息
2.服务提供者:provider-payment8001修改
1)pom
<!--包含了sleuth+zipkin-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
2)yml
server:
port: 8001 # 端口号
spring:
# 应用名称
application:
name: payment-service # 服务名称
zipkin:
base-url: http://localhost:9411
sleuth:
sampler:
probability: 1 # 采样率,值为0-1,一般采用0.5即可。
# 数据源
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 数据库操作类型
driver-class-name: org.gjt.mm.mysql.Driver # 数据库驱动
#url: jdbc:mysql://192.168.221.129:3306/demo2020?useUnicode=true&characterEncoding=utf-8&useSSL=false
url: jdbc:mysql://localhost:3306/demo2020?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
# 是否支持热部署
devtools:
restart:
enabled: true
eureka:
client:
register-with-eureka: true
fetchRegistry: true
service-url:
defaultZone: http://localhost:7001/eureka # 单机版
#defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群版
instance:
instance-id: payment8001
prefer-ip-address: true
# eureka客户端想服务端发动心跳的时间间隔,单位为秒(默认是30秒)。开发的时候可以设置小一些,以保证服务关闭后注册中心及时剔除服务
lease-renewal-interval-in-seconds: 1
# eureka服务端在收到最后一次心跳后等待时间上限,单位为秒(默认是90秒)。开发时候设置小一些
lease-expiration-duration-in-seconds: 2
mybatis:
mapperLocations: classpath:mapper/*.xml #mapper文件
type-aliases-package: cn.chen.entity #所有entity别名所在包
3)业务类
@GetMapping("zipkin")
public String paymentZipkin() {
return "payment-zipkin";
}
3.服务消费者:comsumer-order80
1)pom
<!--包含了sleuth+zipkin-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
2)yml
zipkin:
base-url: http://localhost:9411
sleuth:
sampler:
probability: 1
3)业务类
@GetMapping("payment/zipkin")
public String paymentZipkin() {
String result = restTemplate.getForObject(paymentServerName+"/payment/zipkin", String.class);
return result;
}
4.依次启动eureka7001/8001/80
5.打开浏览器访问:http:localhost:9411
springcloud学习系列目录