SpringCloud学习(六):sleuth/zipkin 链路追踪
菜鸟学渣接触spring cloud 系列...
公司也上微服务了,再不学习下就凉了,所以来踩坑吧...
版本:
spring-boot: 2.0
spring-cloud: Finchley.SR1
已有项目:
[eureka-server] # 注册中心 port 8761
[eureka-client-one] # 微服务1 port 8501
[eureka-client-two] # 微服务2 port 8502
[eureka-client-turbine] # 断路监控 port 8503
[eureka-client-zuul] # 网关服务 port 8601
能上图绝不BB:
sleuth可以列出如图一个请求中微服务的依赖关系,即依次调用顺序
以及跟踪每个微服务处理时间及是否出错
核心组件在于 Twitter 推出的 zipkin 监控组件
一、sleuth [eureka-client-sleuth]
新建微服务 [eureka-client-sleuth]
引入依赖 spring-cloud-starter-sleuth、spring-cloud-starter-zipkin、zipkin-server、zipkin-autoconfigure-ui
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.renzku</groupId> <artifactId>eureka-client-sleuth</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>eureka-client-sleuth</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Finchley.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--1. zipkin 从aliyun的仓库居然下不下来 <url>http://maven.aliyun.com/nexus/content/groups/public/</url> --> <!--2. zipkin-server 依赖包下来了,单名为'zipkin-server'本身没下来,也不报错,'@EnableZipkinServer'使用 不了。单独新建个空maven项目引用zipkin-server,再回本项目reimport又好了,我靠2个小时啊 --> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-server</artifactId> <version>2.11.4</version> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-autoconfigure-ui</artifactId> <version>2.11.4</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
配置文件 application.yml
server: port: 8602 spring: application: name: eureka-client-sleuth eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ # 启动报错 -- 见 https://github.com/openzipkin/zipkin/issues/2043#issuecomment-385932918 management: metrics: web: server: auto-time-requests: false
启动类 EurekaClientSleuthApplication.java
@SpringBootApplication @EnableEurekaClient @EnableZipkinServer // 启用zipkin-server public class EurekaClientSleuthApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientSleuthApplication.class, args); } }
依次启动 [eureka-server]、[eureka-client-one]、[eureka-client-two]、[eureka-client-zuul]、[eureka-sleuth]
访问 http://localhost:8602/zipkin/
二、结果
访问 http://localhost:8601/client-two/test/home 产生一起请求(请求顺序:request -> clientTwo -> clientOne -> response)
点击 【Find Traces】按钮,出现请求记录列表
点击一条数据,可展示详细请求数据: 经过的微服务和耗时
另外,点击Dependencies 查看微服务依赖关系
三、坑
如果操作正常但点击 【Find Traces】就是没数据,F12报错: 手贱用QQ浏览器调试--> zipkin前端用来ES6语法,QQ浏览器用的老版chrome核。