项目地址请到gitee下载查看https://gitee.com/xwb1056481167/spring-cloud
Zipkin
Zipkin是一款开源的分布式实时数据追踪系统(Distributed Tracking System),基于 Google Dapper的论文设计而来,由 Twitter 公司开发贡献。其主要功能是聚集来自各个异构系统的实时监控数据。分布式跟踪系统还有其他比较成熟的实现,例如:Naver的Pinpoint、Apache的HTrace、阿里的鹰眼Tracing、京东的Hydra、新浪的Watchman,美团点评的CAT,skywalking等。
zipkin-server下载
https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/
下载好后需要在所在目录下执行
java -jar zipkin-server-2.12.9-exec.jar
进入浏览器输入: http://localhost:9411/
官方工作原理图
抽象后的原理图
链路监控
服务提供者
操作项目cloud-provider-payment8001
1、pom.xml新增加zipkin
<!-- 引入sleuth+zipkin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
为何只是引入了zipkin不引入sleuth(因为zipkin已经包括了sleuth)
2、yml添加zipkin配置
spring:
zipkin:
base-url: http://localhost:9411 #监控的数据放到改地址下供监控使用
sleuth:
sampler:
#采样率 值介于0到1之间,1表示全部采样
probability: 1
3、controller添加提供放的调用链路
@GetMapping("/payment/zipkin")
public String paymentZipkin() {
return "hi,i'am paymentZipkin server fall back, welcome to";
}
服务消费者(调用方)cloud-consumer-order80
1、pom.xml
<!-- 引入sleuth+zipkin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
2、yml
spring:
zipkin:
base-url: http://localhost:9411 #监控的数据放到改地址下供监控使用
sleuth:
sampler:
#采样率 值介于0到1之间,1表示全部采样
probability: 1
3、controller
@GetMapping("/consumer/payment/zipkin")
public String getPayment2() {
String result = restTemplate.getForObject("http://localhost:8001/payment/zipkin", String.class);
return result;
}
测试
启动7001,8001,80发送 http://localhost/consumer/payment/zipkin请求查看