首页> 博客> SpringCloud入门系列之服务链路追踪Sleuth&Zipkin
17 07 2020

服务链路追踪Sleuth&Zipkin

一、链路追踪 Sleuth
1.1、Spring Cloud Sleuth 优点

Spring Cloud Sleuth为服务之间调用提供链路跟踪

Spring Cloud Sleuth通过日志提供链路跟踪数据

Sleuth遵循OpenTracing的规范,实现跨平台特性

1.2、Sleuth数据格式

1.3、整合步骤
  1. pom.xml中添加依赖

    <?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>
    
  2. 启动类上增加@EnableEurekaClient

    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);
        }
    }
    
  3. application.properties添加配置

    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、整合
  1. pom.xml 添加依赖

    <?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>
    
  2. 启动类上增加注解@EnableZipkinServer@EnableEurekaClient

    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);
        }
    }
    
  3. application.properties中添加配置

    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
    
  4. 启动访问http://localhost:9411/,即可打开如下页面

  5. 完整案例

详细代码见仓库:https://gitee.com/xmlvhy/springcloud-learn 参考链接:http://www.itlaoqi.com/

类似文章

  1. SpringCloud入门系列之配置中心
  2. SpringCloud入门系列之微服务之间的通信
  3. SpringCloud入门系列之Eureka注册中心
  4. SpringCloud入门系列之API网关
  5. Redis 客户端常用命令

评论区

| 0 评论

还没有评论,快来抢沙发吧!