1.zipkin server

1.1.新建Springboot项目,zinkin

1.2.添加依赖

  1. <dependency>
  2. <groupId>io.zipkin.java</groupId>
  3. <artifactId>zipkin-server</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>io.zipkin.java</groupId>
  7. <artifactId>zipkin-autoconfigure-ui</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>mysql</groupId>
  11. <artifactId>mysql-connector-java</artifactId>
  12. <scope>runtime</scope>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-jdbc</artifactId>
  17. </dependency>
  18. <dependency>
  19. <groupId>io.zipkin.java</groupId>
  20. <artifactId>zipkin-autoconfigure-storage-mysql</artifactId>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework.cloud</groupId>
  24. <artifactId>spring-cloud-starter-eureka</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>io.zipkin.java</groupId>
  28. <artifactId>zipkin-storage-mysql</artifactId>
  29. <version>2.4.9</version>
  30. </dependency>

1.3.修改配置文件

  1. zipkin:
  2. storage:
  3. type: mysql
  4.  
  5. spring:
  6. datasource:
  7. schema: classpath*:sql/mysql.sql
  8. url: jdbc:mysql://127.0.0.1:3306/altair_tmp?useUnicode=true&characterEncoding=utf8&autoReconnect=true
  9. username: root
  10. password: 123456
  11. # Switch this on to create the schema on startup:
  12. # initialize: true
  13. # continueOnError: true
  14. sleuth:
  15. enabled: false
  16.  
  17. server:
  18. port: 9944
  19.  
  20. eureka:
  21. client:
  22. service-url:
  23. defaultZone: http://th-blog.cn:7000/eureka/

1.4.主启动类

  1. package com.chinawayltd.altair.zipkin;
  2.  
  3. import org.apache.tomcat.jdbc.pool.DataSource;
  4. import org.springframework.boot.SpringApplication;
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;
  6. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  7. import org.springframework.context.annotation.Bean;
  8. import zipkin.server.internal.EnableZipkinServer;
  9. import zipkin.storage.mysql.MySQLStorage;
  10.  
  11. @SpringBootApplication
  12. @EnableEurekaClient
  13. @EnableZipkinServer
  14. public class ZipkinApplication {
  15.  
  16. public static void main(String[] args) {
  17. SpringApplication.run(ZipkinApplication.class, args);
  18. }
  19. @Bean
  20. public MySQLStorage mySQLStorage(DataSource datasource) {
  21. return MySQLStorage.builder().datasource(datasource).executor(Runnable::run).build();
  22. }
  23. }

1.5.新建表

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

2.zipkin client

2.1.添加依赖

  1. <dependency>
  2. <groupId>io.zipkin.brave</groupId>
  3. <artifactId>brave-core</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>io.zipkin.brave</groupId>
  7. <artifactId>brave-http</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>com.squareup.okhttp3</groupId>
  11. <artifactId>okhttp</artifactId>
  12. <version>3.8.1</version>
  13. <scope>compile</scope>
  14. </dependency>
  15. <dependency>
  16. <groupId>io.zipkin.brave</groupId>
  17. <artifactId>brave-spancollector-http</artifactId>
  18. </dependency>
  19. <dependency>
  20. <groupId>io.zipkin.brave</groupId>
  21. <artifactId>brave-okhttp</artifactId>
  22. </dependency>
  23. <dependency>
  24. <groupId>io.zipkin.brave</groupId>
  25. <artifactId>brave-web-servlet-filter</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>io.zipkin.brave</groupId>
  29. <artifactId>brave-mysql</artifactId>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.apache.httpcomponents</groupId>
  33. <artifactId>httpclient</artifactId>
  34. </dependency>

2.2.修改配置文件

  1. com:
  2. zipkin:
  3. serviceName: altair-user-console-v1
  4. url: http://localhost:9944
  5. connectTimeout: 6000
  6. readTimeout: 6000
  7. flushInterval: 1
  8. compressionEnabled: true

2.3.config

  1. package com.chinawayltd.altair.user.console.config;
  2.  
  3. import com.github.kristofa.brave.Brave;
  4. import com.github.kristofa.brave.EmptySpanCollectorMetricsHandler;
  5. import com.github.kristofa.brave.Sampler;
  6. import com.github.kristofa.brave.SpanCollector;
  7. import com.github.kristofa.brave.http.DefaultSpanNameProvider;
  8. import com.github.kristofa.brave.http.HttpSpanCollector;
  9. import com.github.kristofa.brave.mysql.MySQLStatementInterceptorManagementBean;
  10. import com.github.kristofa.brave.okhttp.BraveOkHttpRequestResponseInterceptor;
  11. import com.github.kristofa.brave.servlet.BraveServletFilter;
  12. import okhttp3.OkHttpClient;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.beans.factory.annotation.Value;
  15. import org.springframework.context.annotation.Bean;
  16. import org.springframework.context.annotation.Configuration;
  17.  
  18. /**
  19. * Created by liaokailin on 16/7/27.
  20. */
  21. @Configuration
  22. public class ZipkinConfig {
  23. @Value("${com.zipkin.serviceName}")
  24. private String serviceName;
  25. @Value("${com.zipkin.url}")
  26. private String url;
  27. @Value("${com.zipkin.connectTimeout}")
  28. private int connectTimeout;
  29. @Value("${com.zipkin.readTimeout}")
  30. private int readTimeout;
  31. @Value("${com.zipkin.flushInterval}")
  32. private int flushInterval;
  33. @Value("${com.zipkin.compressionEnabled}")
  34. private boolean compressionEnabled;
  35.  
  36. @Bean
  37. public SpanCollector spanCollector() {
  38. HttpSpanCollector.Config config = HttpSpanCollector.Config.builder().connectTimeout(connectTimeout).readTimeout(readTimeout)
  39. .compressionEnabled(compressionEnabled).flushInterval(flushInterval).build();
  40. return HttpSpanCollector.create(url, config, new EmptySpanCollectorMetricsHandler());
  41. }
  42.  
  43. @Bean
  44. public Brave brave(SpanCollector spanCollector){
  45. Brave.Builder builder = new Brave.Builder(serviceName); //指定state
  46. builder.spanCollector(spanCollector);
  47. builder.traceSampler(Sampler.ALWAYS_SAMPLE);
  48. Brave brave = builder.build();
  49. return brave;
  50. }
  51.  
  52. @Bean
  53. public BraveServletFilter braveServletFilter(Brave brave){
  54. BraveServletFilter filter = new BraveServletFilter(brave.serverRequestInterceptor(),brave.serverResponseInterceptor(),new DefaultSpanNameProvider());
  55. return filter;
  56. }
  57.  
  58. @Bean
  59. public OkHttpClient okHttpClient(Brave brave){
  60. OkHttpClient client = new OkHttpClient.Builder()
  61. .addInterceptor(new BraveOkHttpRequestResponseInterceptor(brave.clientRequestInterceptor(), brave.clientResponseInterceptor(), new DefaultSpanNameProvider()))
  62. .build();
  63. return client;
  64. }
  65.  
  66. @Bean
  67. public MySQLStatementInterceptorManagementBean mySQLStatementInterceptorManagementBean(Brave brave) {
  68. return new MySQLStatementInterceptorManagementBean(brave.clientTracer());
  69. }
  70. }

参考文档:https://blog.csdn.net/qq_37843943/article/details/80835437

      https://my.oschina.net/u/136848/blog/1623460

SpringCloud:Zipkin链路追踪,并将数据写入mysql的更多相关文章

  1. 微服务 Zipkin 链路追踪原理(图文详解)

    一个看起来很简单的应用,可能需要数十或数百个服务来支撑,一个请求就要多次服务调用. 当请求变慢.或者不能使用时,我们是不知道是哪个后台服务引起的. 这时,我们使用 Zipkin 就能解决这个问题. 由 ...

  2. PHP如何通过SQL语句将数据写入MySQL数据库呢?

    1,php和MySQL建立连接关系 2,打开 3,接受页面数据,PHP录入到指定的表中 1.2两步可直接使用一个数据库链接文件即可:conn.php <?phpmysql_connect(&qu ...

  3. 将pandas的DataFrame数据写入MySQL数据库 + sqlalchemy

    将pandas的DataFrame数据写入MySQL数据库 + sqlalchemy import pandas as pd from sqlalchemy import create_engine ...

  4. flink04 -----1 kafkaSource 2. kafkaSource的偏移量的存储位置 3 将kafka中的数据写入redis中去 4 将kafka中的数据写入mysql中去

    1. kafkaSource 见官方文档 2. kafkaSource的偏移量的存储位置 默认存在kafka的特殊topic中,但也可以设置参数让其不存在kafka的特殊topic中   3   将k ...

  5. 微服务SpringCloud之zipkin链路追踪

    随着业务发展,系统拆分导致系统调用链路愈发复杂一个前端请求可能最终需要调用很多次后端服务才能完成,当整个请求变慢或不可用时,我们是无法得知该请求是由某个或某些后端服务引起的,这时就需要解决如何快读定位 ...

  6. SpringCloud(七)之SpringCloud的链路追踪组件Sleuth实战,以及 zipkin 的部署和使用

    一.前言 Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案 ,并且兼容了zipkin,提供了REST API接口来辅助我们查询跟踪数据以实现对分布式系统的监控程序 . ...

  7. 十一、springcloud之链路追踪Sleuth

    一.背景 随着微服务的数量增长,一个业务接口涉及到多个微服务的交互,在出错的情况下怎么能够快速的定位错误 二.简介 Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案, ...

  8. SpringCloud之链路追踪整合Sleuth(十三)

    前言 SpringCloud 是微服务中的翘楚,最佳的落地方案. 在一个完整的微服务架构项目中,服务之间的调用是很复杂的,当其中某一个服务出现了问题或者访问超时,很 难直接确定是由哪个服务引起的,所以 ...

  9. zipkin链路追踪

    zipkin架构说明 zipkin api 我想自己搞一些满足zipkin格式的日志,入库es,然后让zipkin仅做展示 1.需要了解zipkin组件 2,学习zipkin设计原理,何时何地产生日志 ...

随机推荐

  1. Base64图片编码的使用

    一.base64编码介绍 Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,Base64编码可用于在HTTP环境下传递较长的标识信息.采用Base64编码具有不可读性,即所编码的数据 ...

  2. Python3 MySQL

    首先安装pymysql  pip install pymysql 准备数据库:创建一个数据库testdb mysql实例: import pymysql #打开数据库连接,使用数据库所在的IP127. ...

  3. 5_PHP数组_3_数组处理函数及其应用_6_数组检索函数

    以下为学习孔祥盛主编的<PHP编程基础与实例教程>(第二版)所做的笔记. 数组检索函数 1. array_keys() 函数 程序: <?php $interests[2] = &q ...

  4. Flutter裁剪图片

    最近在学习中需要用到裁剪图片,记录一下解决方法 思路: 使用canvas的drawImageRect()方法,对Image进行裁剪,这里的Image需要 'dart:ui' 库中的Image. 1. ...

  5. 【阿里云开发】- 安装tomcat

    Tomcat配置过程 1.下载Tomcat 官网地址:http://tomcat.apache.org/ 这里我用的是apache-tomcat-8.5.38.tar.gz 2.通过ftp工具将下载好 ...

  6. input file 无法打开手机端文件选择器

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/m0_37805167/article/details/78538044手机端对input file的 ...

  7. web前端布局HTML+CSS

    1.W3C标准 万维网联盟(外语缩写:W3C)标准不是某一个标准,而是一系列标准的集合.网页主要由三部分组成:结构(Structure).表现(Presentation)和行为(Behavior).万 ...

  8. HTTP协议复习三--TCP/IP的网络分层模型和OSI 网络分层模型

    TCP/IP网络分层模型 第一层叫“链接层”(link layer),负责在以太网.WiFi这样的底层网络上发送原始数据包,工 作在网卡这个层次,使用MAC地址来标记网络上的设备,所以有时候也叫MAC ...

  9. 使用Kerberos进行Hadoop认证

    使用Kerberos进行Hadoop认证 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. Kerberos是一种网络身份验证协议.它旨在通过使用秘密密钥加密为客户端/服务器应用程序提 ...

  10. Makefile学习一

    上次随着信号学习告一段落,也标志着linux系统编程相关的知识学完了,而学了这么多知识点,是需要用一个综合的项目来将其进行串起来的,这样学习的技术才会不那么空洞,所以接下来会以一个实际例子来综合运用下 ...