服务链路追踪Sleuth&Zipkin
一、链路追踪 Sleuth
1.1、Spring Cloud Sleuth 优点
Spring Cloud Sleuth为服务之间调用提供链路跟踪 Spring Cloud Sleuth通过日志提供链路跟踪数据 Sleuth遵循OpenTracing的规范,实现跨平台特性
1.2、Sleuth数据格式
1.3、整合步骤
pom.xml中添加依赖
代码语言:javascript复制<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.15.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.codesofun</groupId>
<artifactId>a-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>a-service</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR6</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--Zipkin客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</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>
启动类上增加@EnableEurekaClient
代码语言:javascript复制package com.codesofun.aservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class AServiceApplication {
public static void main(String[] args) {
SpringApplication.run(AServiceApplication.class, args);
}
}
application.properties添加配置
代码语言:javascript复制server.port=8001
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
spring.application.name=a-service
#设置zipkin服务端地址
spring.zipkin.base-url=http://localhost:9411
#采样率是采集trace的比率,默认0.1,例:10000条请求中有效为1000
spring.sleuth.sampler.probability=1.0
#最多每秒最多可生成多少trace条数据
spring.sleuth.sampler.rate=10000
二、Zipkin 分布式链路追踪系统
2.1、优点
Zipkin用于收集Sleuth产生的跟踪日志 Zipkin采用可视化方式提供对链路跟踪的分析与展现 Zipkin采用C/S(客户端与服务端)模式,需单独服务
备注:zipkin 服务端,可以直接前往官网https://zipkin.io/
下载jar包运行。当然也可以整合在spring cloud中(常见)
2.2、整合
pom.xml 添加依赖
代码语言:javascript复制<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.15.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.codesofun</groupId>
<artifactId>zipkin-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>zipkin-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR6</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--ZIPKIN-SERVER 服务端-->
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-server</artifactId>
<version>2.11.8</version>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
<version>2.11.8</version>
</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>
启动类上增加注解@EnableZipkinServer 、@EnableEurekaClient
代码语言:javascript复制package com.codesofun.zipkinserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import zipkin2.server.internal.EnableZipkinServer;
@SpringBootApplication
@EnableEurekaClient
@EnableZipkinServer
public class ZipkinServerApplication {
public static void main(String[] args) {
SpringApplication.run(ZipkinServerApplication.class, args);
}
}
application.properties中添加配置
代码语言:javascript复制server.port=9411
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
spring.application.name=zipkin-service
#关闭监控项,解决zipkin与spring actuator监控项冲突问题
management.metrics.web.server.auto-time-requests=false
启动访问http://localhost:9411/,即可打开如下页面
完整案例
详细代码见仓库:https://gitee.com/xmlvhy/springcloud-learn
参考链接:http://www.itlaoqi.com/
本文作者: [AI码真香](https://www.xmlvhy.com/about)
代码语言:txt复制 本文标题: [SpringCloud入门系列之服务链路追踪Sleuth&Zipkin](https://www.xmlvhy.com/article/detail/93.html)
代码语言:txt复制 本文网址: [https://www.xmlvhy.com/article/93.html](https://www.xmlvhy.com/article/detail/93.html)
代码语言:txt复制 版权说明: 自由转载-非商用-非衍生-保持署名 [ 署名-非商业性使用4.0 国际 (CC BY-NC 4.0) ](https://creativecommons.org/licenses/by-nc/4.0/deed.zh)
类似文章
- SpringCloud入门系列之配置中心
- SpringCloud入门系列之微服务之间的通信
- SpringCloud入门系列之Eureka注册中心
- SpringCloud入门系列之API网关
- Redis 客户端常用命令