Spring Cloud 链路追踪 Sleuth + Twitter Zipkin 中 Zipkin 服务

Posted 小可8703

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Cloud 链路追踪 Sleuth + Twitter Zipkin 中 Zipkin 服务相关的知识,希望对你有一定的参考价值。

2021年5月15日 随笔

  • spring boot 2.2.x 版本以上,不建议自己写服务了,我尝试了很多方法没有成功,各种问题,所以需要下载它的jar包 然后执行 
    • 持久化数据到mysql:命令:
    • java -jar zipkin-server-2.23.2-exec.jar --STORAGE_TYPE=mysql --MYSQL_HOST=127.0.0.1 --MYSQL_TCP_PORT=3306 --MYSQL_DB=zipkin --MYSQL_USER=root --MYSQL_PASS=123456

       

  • springboot 2.1.x 版本 ,本人使用2.1.6.RELEASE

    • 新建maven项目: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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.myzpkin</groupId>
    <artifactId>server-zipkin</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>

    <dependencyManagement>
        <dependencies>
            <!--spring cloud依赖管理,引入了Spring Cloud的版本-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>io.zipkin.java</groupId>
            <artifactId>zipkin-server</artifactId>
            <version>2.12.3</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-log4j2</artifactId>
                </exclusion>
            </exclusions>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.zipkin.java</groupId>
            <artifactId>zipkin-autoconfigure-ui</artifactId>
            <version>2.12.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <target>11</target>
                    <source>11</source>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
    • application.yml:
      server:
        port: 9411
      
      management:
        metrics:
          web:
            server:
              auto-time-requests: false

       

    • 新建启动类,使用注解@EnableZipkinServer
      package com.myzipkin;
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      import zipkin2.server.internal.EnableZipkinServer;
      
      @SpringBootApplication
      @EnableZipkinServer
      public class ZipkinApplication9411 {
          public static void main(String[] args) {
              SpringApplication.run(ZipkinApplication9411.class, args);
          }
      }
    • 启动项目

       

    • 浏览器输入:http://127.0.0.1:9411  如下图则说明启动成功
  • 将数据持久化到mysql
  1. 修改pom.xml 添加 持久化层、 mysql 、transaction 依赖
    <!-- 将链路追踪数据持久化 -->
            <!-- 针对mysql 的持久化方案 -->
            <dependency>
                <groupId>io.zipkin.java</groupId>
                <artifactId>zipkin-autoconfigure-storage-mysql</artifactId>
                <version>2.12.3</version>
            </dependency>
            <!-- mysql -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
            <!-- 连接池 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>1.2.6</version>
            </dependency>
            <!--jdbc-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
            </dependency>
            <!-- spring 操作数据库需要的事务控制-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
            </dependency>
  2. application.yml 配置数据库数据,指定 持久化方案
    server:
      port: 9411
    
    management:
      metrics:
        web:
          server:
            auto-time-requests: false
    
    #持久化所需要的配置
    zipkin:
      storage:
        type: mysql
    
    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/zipkin?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true&serverTimezone=UTC
        username: root
        password: 123456
        druid:
          initialSize: 10
          minIdle: 10
          maxActive: 30
          maxWait: 50000
  3.  新增事务类
    package com.myzipkin.config;
    
    import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.jdbc.datasource.DataSourceTransactionManager;
    import org.springframework.transaction.PlatformTransactionManager;
    
    import javax.sql.DataSource;
    
    
    @Configuration
    public class ZipkinConfig {
    
        @Bean
        public PlatformTransactionManager transactionManager(DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }
    }

     

  4. 新建zipkin库和相关表;zipkin 相关的表可以去  zipkin-server.jar 中去查找 

     

     

    #建立数据库:
    create database zipkin;
    #建立相关的表
    CREATE TABLE IF NOT EXISTS zipkin_spans (
      `trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT \'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit\',
      `trace_id` BIGINT NOT NULL,
      `id` BIGINT NOT NULL,
      `name` VARCHAR(255) NOT NULL,
      `parent_id` BIGINT,
      `debug` BIT(1),
      `start_ts` BIGINT COMMENT \'Span.timestamp(): epoch micros used for endTs query and to implement TTL\',
      `duration` BIGINT COMMENT \'Span.duration(): micros used for minDuration and maxDuration query\'
    ) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;
    
    ALTER TABLE zipkin_spans ADD UNIQUE KEY(`trace_id_high`, `trace_id`, `id`) COMMENT \'ignore insert on duplicate\';
    ALTER TABLE zipkin_spans ADD INDEX(`trace_id_high`, `trace_id`, `id`) COMMENT \'for joining with zipkin_annotations\';
    ALTER TABLE zipkin_spans ADD INDEX(`trace_id_high`, `trace_id`) COMMENT \'for getTracesByIds\';
    ALTER TABLE zipkin_spans ADD INDEX(`name`) COMMENT \'for getTraces and getSpanNames\';
    ALTER TABLE zipkin_spans ADD INDEX(`start_ts`) COMMENT \'for getTraces ordering and range\';
    
    CREATE TABLE IF NOT EXISTS zipkin_annotations (
      `trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT \'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit\',
      `trace_id` BIGINT NOT NULL COMMENT \'coincides with zipkin_spans.trace_id\',
      `span_id` BIGINT NOT NULL COMMENT \'coincides with zipkin_spans.id\',
      `a_key` VARCHAR(255) NOT NULL COMMENT \'BinaryAnnotation.key or Annotation.value if type == -1\',
      `a_value` BLOB COMMENT \'BinaryAnnotation.value(), which must be smaller than 64KB\',
      `a_type` INT NOT NULL COMMENT \'BinaryAnnotation.type() or -1 if Annotation\',
      `a_timestamp` BIGINT COMMENT \'Used to implement TTL; Annotation.timestamp or zipkin_spans.timestamp\',
      `endpoint_ipv4` INT COMMENT \'Null when Binary/Annotation.endpoint is null\',
      `endpoint_ipv6` BINARY(16) COMMENT \'Null when Binary/Annotation.endpoint is null, or no IPv6 address\',
      `endpoint_port` SMALLINT COMMENT \'Null when Binary/Annotation.endpoint is null\',
      `endpoint_service_name` VARCHAR(255) COMMENT \'Null when Binary/Annotation.endpoint is null\'
    ) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;
    
    ALTER TABLE zipkin_annotations ADD UNIQUE KEY(`trace_id_high`, `trace_id`, `span_id`, `a_key`, `a_timestamp`) COMMENT \'Ignore insert on duplicate\';
    ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`, `span_id`) COMMENT \'for joining with zipkin_spans\';
    ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`) COMMENT \'for getTraces/ByIds\';
    ALTER TABLE zipkin_annotations ADD INDEX(`endpoint_service_name`) COMMENT \'for getTraces and getServiceNames\';
    ALTER TABLE zipkin_annotations ADD INDEX(`a_type`) COMMENT \'for getTraces and autocomplete values\';
    ALTER TABLE zipkin_annotations ADD INDEX(`a_key`) COMMENT \'for getTraces and autocomplete values\';
    ALTER TABLE zipkin_annotations ADD INDEX(`trace_id`, `span_id`, `a_key`) COMMENT \'for dependencies job\';
    
    CREATE TABLE IF NOT EXISTS zipkin_dependencies (
      `day` DATE NOT NULL,
      `parent` VARCHAR(255) NOT NULL,
      `child` VARCHAR(255) NOT NULL,
      `call_count` BIGINT,
      `error_count` BIGINT
    ) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;
    
    ALTER TABLE zipkin_dependencies ADD UNIQUE KEY(`day`, `parent`, `child`);

     

  5. 重新启动 
  6. 使用网关调用;查看数据库中 表里面已经有数据了,重启该服务数据也不会丢失

 

以上是关于Spring Cloud 链路追踪 Sleuth + Twitter Zipkin 中 Zipkin 服务的主要内容,如果未能解决你的问题,请参考以下文章

Sleuth+Zipkin 服务链路追踪

SpringCloud 教程 服务链路追踪(Spring Cloud Sleuth)

Spring Cloud(16)——链路追踪工具Sleuth

全链路追踪spring-cloud-sleuth-zipkin

第八篇: 服务链路追踪(Spring Cloud Sleuth)

Spring Cloud 整合分布式链路追踪系统Sleuth和ZipKin实战,分析系统瓶颈