• sleuth+zipkin持久化和gateway设置跨域


    1.为什么使用sleuth+zipkin持久化

    Zipkin Server默认会将追踪数据信息保存到内存,但这种方式不适合生产环境。Zipkin支持将追踪数据持久化到mysql数据库或elasticsearch中。

    2.使用mysql实现数据持久化

    2.1.创建数据库--省略
    2.2.创建表

    1. CREATE TABLE IF NOT EXISTS zipkin_spans (
    2. `trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit',
    3. `trace_id` BIGINT NOT NULL,
    4. `id` BIGINT NOT NULL,
    5. `name` VARCHAR(255) NOT NULL,
    6. `parent_id` BIGINT,
    7. `debug` BIT(1),
    8. `start_ts` BIGINT COMMENT 'Span.timestamp(): epoch micros used for endTs query and to implement TTL',
    9. `duration` BIGINT COMMENT 'Span.duration(): micros used for minDuration and maxDuration query' )
    10. ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;
    11. ALTER TABLE zipkin_spans ADD UNIQUE KEY(`trace_id_high`, `trace_id`, `id`) COMMENT 'ignore insert on duplicate';
    12. ALTER TABLE zipkin_spans ADD INDEX(`trace_id_high`, `trace_id`, `id`) COMMENT 'for joining with zipkin_annotations';
    13. ALTER TABLE zipkin_spans ADD INDEX(`trace_id_high`, `trace_id`) COMMENT 'for getTracesByIds';
    14. ALTER TABLE zipkin_spans ADD INDEX(`name`) COMMENT 'for getTraces and getSpanNames';
    15. ALTER TABLE zipkin_spans ADD INDEX(`start_ts`) COMMENT 'for getTraces ordering and range';
    16. CREATE TABLE IF NOT EXISTS zipkin_annotations (
    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 COMMENT 'coincides with zipkin_spans.trace_id',
    19. `span_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.id',
    20. `a_key` VARCHAR(255) NOT NULL COMMENT 'BinaryAnnotation.key or Annotation.value if type == -1',
    21. `a_value` BLOB COMMENT 'BinaryAnnotation.value(), which must be smaller than 64KB',
    22. `a_type` INT NOT NULL COMMENT 'BinaryAnnotation.type() or -1 if Annotation',
    23. `a_timestamp` BIGINT COMMENT 'Used to implement TTL; Annotation.timestamp or zipkin_spans.timestamp',
    24. `endpoint_ipv4` INT COMMENT 'Null when Binary/Annotation.endpoint is null',
    25. `endpoint_ipv6` BINARY(16) COMMENT 'Null when Binary/Annotation.endpoint is null, or no IPv6 address',
    26. `endpoint_port` SMALLINT COMMENT 'Null when Binary/Annotation.endpoint is null',
    27. `endpoint_service_name` VARCHAR(255) COMMENT 'Null when Binary/Annotation.endpoint is null' )
    28. ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;
    29. ALTER TABLE zipkin_annotations ADD UNIQUE KEY(`trace_id_high`, `trace_id`, `span_id`, `a_key`, `a_timestamp`) COMMENT 'Ignore insert on duplicate';
    30. ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`, `span_id`) COMMENT 'for joining with zipkin_spans';
    31. ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`) COMMENT 'for getTraces/ByIds';
    32. ALTER TABLE zipkin_annotations ADD INDEX(`endpoint_service_name`) COMMENT 'for getTraces and getServiceNames';
    33. ALTER TABLE zipkin_annotations ADD INDEX(`a_type`) COMMENT 'for getTraces';
    34. ALTER TABLE zipkin_annotations ADD INDEX(`a_key`) COMMENT 'for getTraces';
    35. ALTER TABLE zipkin_annotations ADD INDEX(`trace_id`, `span_id`, `a_key`) COMMENT 'for dependencies job';
    36. CREATE TABLE IF NOT EXISTS zipkin_dependencies (
    37. `day` DATE NOT NULL,
    38. `parent` VARCHAR(255) NOT NULL,
    39. `child` VARCHAR(255) NOT NULL,
    40. `call_count` BIGINT )
    41. ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;
    42. ALTER TABLE zipkin_dependencies ADD UNIQUE KEY(`day`, `parent`, `child`);

    2.3.在启动ZipKin Server的时候,指定数据保存的mysql的信息

    找到自己下载的位置在地址栏输入cmd

     

    2.4.重启一下自己的项目--省略

    2.5.访问浏览器

     可以自己在测试一下关闭 ZipKin Server,在重新启动一下看看有没有 ,持久化错了的可能数据库的版本高

    3.gateway设置跨域

    3.1.设置一个跨域配置类

    1. @Configuration
    2. public class CorsConfig {
    3. @Bean
    4. public CorsWebFilter corsFilter() {
    5. CorsConfiguration config = new CorsConfiguration();
    6. config.addAllowedMethod("*");
    7. config.addAllowedOrigin("*");
    8. config.addAllowedHeader("*");
    9. UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
    10. source.registerCorsConfiguration("/**", config);
    11. return new CorsWebFilter(source);
    12. }
    13. }

    3.2.或者设置配置文件 

    1. spring:
    2. cloud:
    3. gateway:
    4. globalcors:
    5. cors-configurations:
    6. '[/**]':
    7. allowedOrigins: "*"
    8. allowedMethods:
    9. - GET
    10. - POST
    11. - DELETE
    12. - PUT
    13. - OPTION

     

     

  • 相关阅读:
    入手云服务器后,你需要做这些事【基于CentOS】
    学习Oracle数据库新建数据库操作(三)
    用Python实现感知机学习算法及其对偶算法实验报告
    MySQL基础篇【第二篇】| 简单的查询、条件查询、排序查询
    数据库向量化如何进行性能优化
    一种隐私保护边云协同训练
    【PTA题目】6-20 使用函数判断完全平方数 分数 10
    中级软件设计师考试(软考中级)计算机专业英语
    Spring 使用指南 ~ 1、Spring 的 IOC 和 DI 简介
    Vue入门
  • 原文地址:https://blog.csdn.net/qq_55682798/article/details/126506666