赞
踩
在微服务框架中,一个由客户端发起的请求在后端系统中会经过多个不同的的服务节点调用来协同产生最后的请求结果,每一个前段请求都会形成一 条复杂的分布式服务调用链路,链路中的任何一环出现高延时或错误都会弓|起整个请求最后的失败。
Spring Cloud Sleuth 提供了一套完整的服务跟踪解决方案,并且兼容支持了zipkin。
官网: https://github.com/spring-cloud/spring-cloud-sleuth
SpringCloud 从 F 版起已不需要自己构建 Zipkin server 了,只需要调用 jar 包即可。
下载 jar: https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/
下载: zipkin-server-2.12.9.exec.jar
运行 jar:java -jar zipkin-server-2.12.9-exec.jar
控制台: http://localhost:9411/zipkin/
zipkin 工作原理:
一条链路通过 Trace Id 唯一标识,Span标识发起的请求信息,各 span 通过 parent id 关联起来。
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
- datasource:
- type: com.alibaba.druid.pool.DruidDataSource
- driver-class-name: org.gjt.mm.mysql.Driver
- url: jdbc:mysql://localhost:3306/mycloud?useUnicode=true&characterEncoding=utf-8&useSSL=false
- username: root
- password: mysql
- zipkin:
- base-url: http://localhost:9411
- sleuth:
- sampler:
- probability: 1 # 采样率,介于0-1之间,1表示全部采集
-
- eureka:
- client:
- register-with-eureka: true
- fetchRegistry: true
- service-url:
- defaultZone: http://eureka7001.com:7001/eureka
- instance:
- instance-id: payment8001
- prefer-ip-address: true
业务类:
- @GetMapping("/payment/zipkin")
- public String paymentZipkin()
- {
- return "hi ,i'am paymentZipkin server fall back O(∩_∩)O哈哈~";
- }
cloud-consumer-order80
pom 添加依赖:
- <!--包含了sleuth+zipkin-->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-zipkin</artifactId>
- </dependency>
yml:
- server:
- port: 80
-
- spring:
- application:
- name: cloud-order-service
- zipkin:
- base-url: http://localhost:9411
- sleuth:
- sampler:
- probability: 1 # 采样率,介于0-1之间,1表示全部采集
-
- eureka:
- client:
- register-with-eureka: true
- fetchRegistry: true
- service-url:
- defaultZone: http://eureka7001.com:7001/eureka
业务类:
- @GetMapping("/consumer/payment/zipkin")
- public String paymentZipkin()
- {
- String result = restTemplate.getForObject("http://localhost:8001"+"/payment/zipkin/", String.class);
- return result;
- }
多次访问:http://localhost/consumer/payment/zipkin 80 调用8001
http:localhost:9411
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。