• 【SpringCloud-Alibaba系列教程】12.日志链路追踪


    引入问题

    毕竟写代码,肯定有bug的,所以我们必要日志查看还是需要的,但是微服务查看,我们需要一条整个链路追踪,要不然我们根本不知道,哪里出问题了,所以我们需要进行实现链路日志追踪。

    我们开始吧

    首先就是引入我们的链路追踪的sleuth的相关依赖。

    1. <dependency>
    2. <groupId>org.springframework.cloud</groupId>
    3. <artifactId>spring-cloud-starter-sleuth</artifactId>
    4. </dependency>
    5. 复制代码

    然后进行我们访问之前写的api接口,我们看一下控制台会有那些内容。 http://localhost:7000/product-serv/product/1?token=admin 我们可以看到这有一个相同的一串
    [service-product,a5670094401b487b,de21187ddc169043,true]
    [api-gateway,a5670094401b487b,a5670094401b487b,true]
    几个图对比后可以看到第一个就是服务名称。在不同的的会有一个相同的,你可以看一下
    a5670094401b487b 这就相当于一个链路上的,后面的不同id就是不同服务上面的唯一id。 第三个就是是否输出到第三方平台,这里true就是输出了,其实这里我已经配置了输出到zipkin界面。 我们来看一下配置:

    1. <dependency>
    2. <groupId>org.springframework.cloud</groupId>
    3. <artifactId>spring-cloud-starter-zipkin</artifactId>
    4. </dependency>
    5. </dependencies>
    6. zipkin:
    7. base-url: http://127.0.0.1:9411
    8. discovery-client-enabled: false #让nacos把它当成一个url不要做服务名
    9. sender:
    10. type: web
    11. sleuth:
    12. web:
    13. client:
    14. enabled: true
    15. sampler:
    16. probability: 1.0 # 采样比例为: 0.1(即10%),设置的值介于0.01.0之间,1.0则表示全部采集。
    17. 复制代码

    其实这样是存在内存中的,如果重启那就没有了,那如何进行持久化呢,主要就是两种,一个就是mysql,es 我们先演示MySQL的,其实就是在启动的时候做一下修改。 例如我们原本的 Windows java -jar zipkin-server-2.23.16-exec.jar 如果需要持久化数据库我们首先创建持久化存储的表。 可以直接去网址下载:

    github.com/openzipkin/…

    1. -- Copyright 2015-2019 The OpenZipkin Authors
    2. --
    3. -- Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
    4. -- in compliance with the License. You may obtain a copy of the License at
    5. --
    6. -- http://www.apache.org/licenses/LICENSE-2.0
    7. --
    8. -- Unless required by applicable law or agreed to in writing, software distributed under the License
    9. -- is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
    10. -- or implied. See the License for the specific language governing permissions and limitations under
    11. -- the License.
    12. --
    13. CREATE TABLE IF NOT EXISTS zipkin_spans (
    14. `trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit',
    15. `trace_id` BIGINT NOT NULL,
    16. `id` BIGINT NOT NULL,
    17. `name` VARCHAR(255) NOT NULL,
    18. `remote_service_name` VARCHAR(255),
    19. `parent_id` BIGINT,
    20. `debug` BIT(1),
    21. `start_ts` BIGINT COMMENT 'Span.timestamp(): epoch micros used for endTs query and to implement TTL',
    22. `duration` BIGINT COMMENT 'Span.duration(): micros used for minDuration and maxDuration query',
    23. PRIMARY KEY (`trace_id_high`, `trace_id`, `id`)
    24. ) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;
    25. ALTER TABLE zipkin_spans ADD INDEX(`trace_id_high`, `trace_id`) COMMENT 'for getTracesByIds';
    26. ALTER TABLE zipkin_spans ADD INDEX(`name`) COMMENT 'for getTraces and getSpanNames';
    27. ALTER TABLE zipkin_spans ADD INDEX(`remote_service_name`) COMMENT 'for getTraces and getRemoteServiceNames';
    28. ALTER TABLE zipkin_spans ADD INDEX(`start_ts`) COMMENT 'for getTraces ordering and range';
    29. CREATE TABLE IF NOT EXISTS zipkin_annotations (
    30. `trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit',
    31. `trace_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.trace_id',
    32. `span_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.id',
    33. `a_key` VARCHAR(255) NOT NULL COMMENT 'BinaryAnnotation.key or Annotation.value if type == -1',
    34. `a_value` BLOB COMMENT 'BinaryAnnotation.value(), which must be smaller than 64KB',
    35. `a_type` INT NOT NULL COMMENT 'BinaryAnnotation.type() or -1 if Annotation',
    36. `a_timestamp` BIGINT COMMENT 'Used to implement TTL; Annotation.timestamp or zipkin_spans.timestamp',
    37. `endpoint_ipv4` INT COMMENT 'Null when Binary/Annotation.endpoint is null',
    38. `endpoint_ipv6` BINARY(16) COMMENT 'Null when Binary/Annotation.endpoint is null, or no IPv6 address',
    39. `endpoint_port` SMALLINT COMMENT 'Null when Binary/Annotation.endpoint is null',
    40. `endpoint_service_name` VARCHAR(255) COMMENT 'Null when Binary/Annotation.endpoint is null'
    41. ) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;
    42. ALTER TABLE zipkin_annotations ADD UNIQUE KEY(`trace_id_high`, `trace_id`, `span_id`, `a_key`, `a_timestamp`) COMMENT 'Ignore insert on duplicate';
    43. ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`, `span_id`) COMMENT 'for joining with zipkin_spans';
    44. ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`) COMMENT 'for getTraces/ByIds';
    45. ALTER TABLE zipkin_annotations ADD INDEX(`endpoint_service_name`) COMMENT 'for getTraces and getServiceNames';
    46. ALTER TABLE zipkin_annotations ADD INDEX(`a_type`) COMMENT 'for getTraces and autocomplete values';
    47. ALTER TABLE zipkin_annotations ADD INDEX(`a_key`) COMMENT 'for getTraces and autocomplete values';
    48. ALTER TABLE zipkin_annotations ADD INDEX(`trace_id`, `span_id`, `a_key`) COMMENT 'for dependencies job';
    49. CREATE TABLE IF NOT EXISTS zipkin_dependencies (
    50. `day` DATE NOT NULL,
    51. `parent` VARCHAR(255) NOT NULL,
    52. `child` VARCHAR(255) NOT NULL,
    53. `call_count` BIGINT,
    54. `error_count` BIGINT,
    55. PRIMARY KEY (`day`, `parent`, `child`)
    56. ) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;
    57. 复制代码

    然后启动zipkin命令 java -jar zipkin-server-2.23.16-exec.jar --STORAGE_TYPE=mysql --MYSQL_HOST=127.0.0.1 --MYSQL_TCP_PORT=3306 --MYSQL_DB=zipkin --MYSQL_USER=root --MYSQL_PASS=123456

    可以点开数据库查看
    这样无论怎么重启都可以看到日志了 另一种就是es(全名elasticsearch) 我们可以先去官网下载www.elastic.co/cn/download… 然后下载解压去bin目录启动

    1. java -jar zipkin-server-2.23.16-exec.jar --STORAGE_TYPE=elasticsearch --ES-HOST=localhost:9200
    2. 复制代码

    然后使用以上命令重启动zipkin 然后重启也是可以看到持久化的数据了。 这样我们的持久化就完成了。

  • 相关阅读:
    文件系统的简单操作
    OpenCV 配置 VS 2022并识别人脸框出
    「教师资格证定期注册」相关答疑
    RK3588 rtc-hym8563设备开发
    21. 合并两个有序链表 ●
    Spring 5高级编程,投入Spring的怀抱
    【RuoYi移动端】uni-app中实现生成二维码功能(代码示例)
    【5GC】5G PDU会话以及会话类型
    typescript43-类型兼容性说明
    linux静态库与动态库
  • 原文地址:https://blog.csdn.net/BASK2312/article/details/128117941