1.概述
为什么会出现这个技术?需要解决哪些问题?是什么- https://github.com/spring-cloud/spring-cloud-sleuth
- Spring Cloud Sleuth提供了一套完整的服务跟踪的解决方案
- 在分布式系统中提供追踪解决方案并且兼容支持了zipkin
解决
2.搭建链路监控步骤
zipkin
下载
SpringCloud从F版起已不需要自己构建Zipkin server了,只需要调用jar包即可https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/ zipkin-server-2.12.9.exec.jar启动jarjava -jar zipkin-server-2.12.9-exec.jar
运行控制台
http://localhost:9411/zipkin/完整的调用链路上图what
名词解释
- Trace:类似于树结构的Span集合,表示一条调用链路,存在唯一标识
- span:表示调用链路来源,通俗的理解span就是一次请求信息
服务提供者
改造cloud-provider-payment8001pom<!--包含了sleuth+zipkin-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
ymlserver:
port: 8001
spring:
application:
name: cloud-payment-service
zipkin: #zipkin的配置
base-url: http://localhost:9411 #注册到sleuth的服务端地址
sleuth:
sampler:
probability: 1
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/springcloud2020?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
mybatis:
mapperLocations: classpath:mapper/*/*.xml
type-aliases-package: com.chl.springcloud.entity
#eureka
eureka:
client:
register-with-eureka: true #注册中心注册自己
fetchRegistry: true #去服务中心拉去服务列表需要去检索服务
service-url:
#单机版的注册 eureka的服务中心注册地址
defaultZone: http://eureka7001.com:7001/eureka
修改业务类PaymentController/**
* 测试sleuth的链路追踪
* @return
*/
@GetMapping("/payment/zipkin")
public String paymentZipkin() {
return "hi ,i'am paymentzipkin server fall back,welcome to atguigu,O(∩_∩)O哈哈~";
}
服务消费者(调用方)
修改cloud-consumer-order80pom<!--包含了sleuth+zipkin-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
ymlserver:
port: 80
spring:
application:
name: cloud-order-service
zipkin:
base-url: http://localhost:9411 #注册到sleuth的服务端地址
sleuth:
sampler:
probability: 1
eureka:
client:
register-with-eureka: true
fetchRegistry: true
service-url:
defaultZone: http://localhost:7001/eureka
修改业务类OrderController//测试sleuth链路追踪
// ====================> zipkin+sleuth
@GetMapping("/consumer/payment/zipkin")
public String paymentZipkin() {
String result = restTemplate.getForObject("http://localhost:8001"+"/payment/zipkin/", String.class);
return result;
}
测试
依次启动eureka7001/8001/8080调用8001几次测试下打开浏览器访问 http://localhost:9411 会出现以下界面
附件列表