• 分布式链路追踪技术 Sleuth +Zipkin


    (1)每一个需要被追踪踪迹的微服务工程都引入依赖坐标

    1. <dependency>
    2. <groupId>org.springframework.cloudgroupId>
    3. <artifactId>spring-cloud-starter-sleuthartifactId>
    4. dependency>

    (2)每一个微服务都修改application.yml配置文件,添加日志级别

    1. #分布式链路追踪
    2. logging:
    3. level:
    4. org.springframework.web.servlet.DispatcherServlet: debug
    5. org.springframework.cloud.sleuth: debug

    请求到来时,我们在控制台可以观察到 Sleuth 输出的日志(全局 TraceId、SpanId等)。

            这样的日志首先不容易阅读观察,另外日志分散在各个微服务服务器上,接下来我们使用zipkin统一聚合轨迹日志并进行存储展示。 

    (3)结合 Zipkin 展示追踪数据

            Zipkin 包括Zipkin ServerZipkin Client两部分,Zipkin Server是一个单独的服务,Zipkin Client就是具体的微服务

    Zipkin Server 构建

    • pom.xml
      1. <dependency>
      2. <groupId>io.zipkin.javagroupId>
      3. <artifactId>zipkin-serverartifactId>
      4. <version>2.12.3version>
      5. <exclusions>
      6. <exclusion>
      7. <groupId>org.springframework.bootgroupId>
      8. <artifactId>spring-boot-starter-log4j2artifactId>
      9. exclusion>
      10. exclusions>
      11. dependency>
      12. <dependency>
      13. <groupId>io.zipkin.javagroupId>
      14. <artifactId>zipkin-autoconfigure-uiartifactId>
      15. <version>2.12.3version>
      16. dependency>
    •  入口启动类
      1. package com.lagou.edu;
      2. import org.springframework.boot.SpringApplication;
      3. import org.springframework.boot.autoconfigure.SpringBootApplication;
      4. import zipkin2.server.internal.EnableZipkinServer;
      5. import javax.sql.DataSource;
      6. @SpringBootApplication
      7. @EnableZipkinServer // 开启Zipkin 服务器功能
      8. public class ZipkinServerApplication9411 {
      9. public static void main(String[] args) {
      10. SpringApplication.run(ZipkinServerApplication9411.class,args);
      11. }
      12. }
    • application.yml
      1. server:
      2. port: 9411
      3. management:
      4. metrics:
      5. web:
      6. server:
      7. auto-time-requests: false # 关闭自动检测

    Zipkin Client 构建(在具体微服务中修改)

    • pom中添加 zipkin 依赖
      1. <dependency>
      2. <groupId>org.springframework.cloudgroupId>
      3. <artifactId>spring-cloud-starter-zipkinartifactId>
      4. dependency>
    • application.yml 中添加对zipkin server的引用
      1. spring:
      2. zipkin:
      3. base-url: http://127.0.0.1:9411 # zipkin server的请求地址
      4. sender:
      5. # web 客户端将踪迹日志数据通过网络请求的方式传送到服务端,另外还有配置
      6. # kafka/rabbit 客户端将踪迹日志数据传递到mq进行中转
      7. type: web
      8. sleuth:
      9. sampler:
      10. # 采样率 1 代表100%全部采集 ,默认0.1 代表10% 的请求踪迹数据会被采集
      11. # 生产环境下,请求量非常大,没有必要所有请求的踪迹数据都采集分析,对于网络包括server端压力都是比较大的,可以配置采样率采集一定比例的请求的踪迹数据进行分析即可
      12. probability: 1

      另外,对于log日志,依然保持开启debug状态

    (4)启动服务器访问,就能看到zipkin页面

            Zipkin server 页面方便我们查看服务调用依赖关系及一些性能指标和异常信息。

    (5)追踪数据Zipkin持久化到mysql(这些改动是在zipkinServer中)

    • mysql中创建名称为zipkin的数据库,并执行如下sql语句(官方提供)
      1. --
      2. -- Copyright 2015-2019 The OpenZipkin Authors
      3. --
      4. -- Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
      5. -- in compliance with the License. You may obtain a copy of the License at
      6. --
      7. -- http://www.apache.org/licenses/LICENSE-2.0
      8. --
      9. -- Unless required by applicable law or agreed to in writing, software distributed under the License
      10. -- is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
      11. -- or implied. See the License for the specific language governing permissions and limitations under
      12. -- the License.
      13. --
      14. CREATE TABLE
      15. IF
      16. NOT EXISTS zipkin_spans (
      17. `trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit',
      18. `trace_id` BIGINT NOT NULL,
      19. `id` BIGINT NOT NULL,
      20. `name` VARCHAR ( 255 ) NOT NULL,
      21. `remote_service_name` VARCHAR ( 255 ),
      22. `parent_id` BIGINT,
      23. `debug` BIT ( 1 ),
      24. `start_ts` BIGINT COMMENT 'Span.timestamp(): epoch micros used for endTs query and to implement TTL',
      25. `duration` BIGINT COMMENT 'Span.duration(): micros used for minDuration and maxDuration query',
      26. PRIMARY KEY ( `trace_id_high`, `trace_id`, `id` )
      27. ) ENGINE = INNODB ROW_FORMAT = COMPRESSED CHARACTER
      28. SET = utf8 COLLATE utf8_general_ci;
      29. ALTER TABLE zipkin_spans ADD INDEX ( `trace_id_high`, `trace_id` ) COMMENT 'for getTracesByIds';
      30. ALTER TABLE zipkin_spans ADD INDEX ( `name` ) COMMENT 'for getTraces and getSpanNames';
      31. ALTER TABLE zipkin_spans ADD INDEX ( `remote_service_name` ) COMMENT 'for getTraces and getRemoteServiceNames';
      32. ALTER TABLE zipkin_spans ADD INDEX ( `start_ts` ) COMMENT 'for getTraces ordering and range';
      33. CREATE TABLE
      34. IF
      35. NOT EXISTS zipkin_annotations (
      36. `trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit',
      37. `trace_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.trace_id',
      38. `span_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.id',
      39. `a_key` VARCHAR ( 255 ) NOT NULL COMMENT 'BinaryAnnotation.key or Annotation.value if type == -1',
      40. `a_value` BLOB COMMENT 'BinaryAnnotation.value(), which must be smaller than 64KB',
      41. `a_type` INT NOT NULL COMMENT 'BinaryAnnotation.type() or -1 if Annotation',
      42. `a_timestamp` BIGINT COMMENT 'Used to implement TTL; Annotation.timestamp or zipkin_spans.timestamp',
      43. `endpoint_ipv4` INT COMMENT 'Null when Binary/Annotation.endpoint is null',
      44. `endpoint_ipv6` BINARY ( 16 ) COMMENT 'Null when Binary/Annotation.endpoint is null, or no IPv6 address',
      45. `endpoint_port` SMALLINT COMMENT 'Null when Binary/Annotation.endpoint is null',
      46. `endpoint_service_name` VARCHAR ( 255 ) COMMENT 'Null when Binary/Annotation.endpoint is null'
      47. ) ENGINE = INNODB ROW_FORMAT = COMPRESSED CHARACTER
      48. SET = utf8 COLLATE utf8_general_ci;
      49. ALTER TABLE zipkin_annotations ADD UNIQUE KEY ( `trace_id_high`, `trace_id`, `span_id`, `a_key`, `a_timestamp` ) COMMENT 'Ignore insert on duplicate';
      50. ALTER TABLE zipkin_annotations ADD INDEX ( `trace_id_high`, `trace_id`, `span_id` ) COMMENT 'for joining with zipkin_spans';
      51. ALTER TABLE zipkin_annotations ADD INDEX ( `trace_id_high`, `trace_id` ) COMMENT 'for getTraces/ByIds';
      52. ALTER TABLE zipkin_annotations ADD INDEX ( `endpoint_service_name` ) COMMENT 'for getTraces and getServiceNames';
      53. ALTER TABLE zipkin_annotations ADD INDEX ( `a_type` ) COMMENT 'for getTraces and autocomplete values';
      54. ALTER TABLE zipkin_annotations ADD INDEX ( `a_key` ) COMMENT 'for getTraces and autocomplete values';
      55. ALTER TABLE zipkin_annotations ADD INDEX ( `trace_id`, `span_id`, `a_key` ) COMMENT 'for dependencies job';
      56. CREATE TABLE
      57. IF
      58. NOT EXISTS zipkin_dependencies (
      59. `day` DATE NOT NULL,
      60. `parent` VARCHAR ( 255 ) NOT NULL,
      61. `child` VARCHAR ( 255 ) NOT NULL,
      62. `call_count` BIGINT,
      63. `error_count` BIGINT,
      64. PRIMARY KEY ( `day`, `parent`, `child` )
      65. ) ENGINE = INNODB ROW_FORMAT = COMPRESSED CHARACTER
      66. SET = utf8 COLLATE utf8_general_ci;
    • pom文件引入相关依赖
      1. "1.0" encoding="UTF-8"?>
      2. <project xmlns="http://maven.apache.org/POM/4.0.0"
      3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      5. <parent>
      6. <artifactId>lagou-parentartifactId>
      7. <groupId>com.lagou.edugroupId>
      8. <version>1.0-SNAPSHOTversion>
      9. parent>
      10. <modelVersion>4.0.0modelVersion>
      11. <artifactId>lagou-cloud-zipkin-server-9411artifactId>
      12. <dependencies>
      13. <dependency>
      14. <groupId>io.zipkin.javagroupId>
      15. <artifactId>zipkin-serverartifactId>
      16. <version>2.12.3version>
      17. <exclusions>
      18. <exclusion>
      19. <groupId>org.springframework.bootgroupId>
      20. <artifactId>spring-boot-starter-log4j2artifactId>
      21. exclusion>
      22. exclusions>
      23. dependency>
      24. <dependency>
      25. <groupId>io.zipkin.javagroupId>
      26. <artifactId>zipkin-autoconfigure-uiartifactId>
      27. <version>2.12.3version>
      28. dependency>
      29. <dependency>
      30. <groupId>io.zipkin.javagroupId>
      31. <artifactId>zipkin-autoconfigure-storage-mysqlartifactId>
      32. <version>2.12.3version>
      33. dependency>
      34. <dependency>
      35. <groupId>mysqlgroupId>
      36. <artifactId>mysql-connector-javaartifactId>
      37. dependency>
      38. <dependency>
      39. <groupId>com.alibabagroupId>
      40. <artifactId>druid-spring-boot-starterartifactId>
      41. <version>1.1.10version>
      42. dependency>
      43. <dependency>
      44. <groupId>org.springframeworkgroupId>
      45. <artifactId>spring-txartifactId>
      46. dependency>
      47. <dependency>
      48. <groupId>org.springframeworkgroupId>
      49. <artifactId>spring-jdbcartifactId>
      50. dependency>
      51. dependencies>
      52. project>
    • 修改配置文件,添加如下内容
      1. server:
      2. port: 9411
      3. management:
      4. metrics:
      5. web:
      6. server:
      7. auto-time-requests: false # 关闭自动检测
      8. spring:
      9. datasource:
      10. driver-class-name: com.mysql.jdbc.Driver
      11. url: jdbc:mysql://localhost:3306/zipkin?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true
      12. username: root
      13. password: 123456
      14. druid:
      15. initialSize: 10
      16. minIdle: 10
      17. maxActive: 30
      18. maxWait: 50000
      19. # 指定zipkin持久化介质为mysql
      20. zipkin:
      21. storage:
      22. type: mysql
    • 启动类中注入事务管理器
      1. package com.lagou.edu;
      2. import org.springframework.boot.SpringApplication;
      3. import org.springframework.boot.autoconfigure.SpringBootApplication;
      4. import org.springframework.context.annotation.Bean;
      5. import org.springframework.jdbc.datasource.DataSourceTransactionManager;
      6. import org.springframework.transaction.PlatformTransactionManager;
      7. import zipkin2.server.internal.EnableZipkinServer;
      8. import javax.sql.DataSource;
      9. @SpringBootApplication
      10. @EnableZipkinServer // 开启Zipkin 服务器功能
      11. public class ZipkinServerApplication9411 {
      12. public static void main(String[] args) {
      13. SpringApplication.run(ZipkinServerApplication9411.class,args);
      14. }
      15. // 注入事务控制器
      16. @Bean
      17. public PlatformTransactionManager transactionManager(DataSource dataSource) {
      18. return new DataSourceTransactionManager(dataSource);
      19. }
      20. }

     

  • 相关阅读:
    FFplay文档解读-49-多媒体过滤器三
    Flink内核源码(八)Flink Checkpoint
    三、支付宝支付对接 - 申请、配置、签约、获取RSkey(1)
    jquery.i18n.properties.js使用及seo优化
    Java注解与反射知识梳理
    【LeetCode】1838. 最高频元素的频数
    Linux命令--hexdump(以16进制查看文件内容)
    中国无纺布行业市场竞争分析与发展前景预测报告
    【大数据】Flink 内存管理(二):JobManager 内存分配(含实际计算案例)
    【技术积累】Mysql中的SQL语言【技术篇】【二】
  • 原文地址:https://blog.csdn.net/weixin_52851967/article/details/126598346