赞
踩
随着各模块单体微服务越来越多,系统之间的服务依赖、服务调用越来越复杂,当我们的系统出现性能缓慢时,无法快速地定位是哪个服务造成的,而链路跟踪服务能帮助我们很好的查看各服务调用信息,帮助我们很好地分析系统性能问题。
由于有的生产问题可能无法复现,所以请求调用链路信息是有必要作数据存储地,但是不能所有的信息都去存储,需要制定相关策略。
因为链路跟踪服务是辅助服务,不是我们的主流程服务,所以选型不要过于复杂,需要适配spring cloud体系。
这里选用spring cloud全家桶里的组件,如下:
spring-cloud-sleuth:用作服务之间调用信息采集。
zipkin:用于图形化查看请求调用信息与服务之间的调用依赖关系。
mysql:用于存储请求调用信息。
rabbitmq:用于服务调用时给zipkin服务发送信息,在图形化界面查看调用信息,还支持web方式发送,但是web方式会占带宽影响系统性能,所以采用rabbitmq异步发送。
官方建议zipkin 2.0之后的版本用官方jar包;而zipkin没有提供存储策略,即数据要么全部存储,要么全部不存储。所以为了增加存储策略,不采用官方jar,自己定制zipkin服务。
定制内容:
增加定时任务,定时删除请求响应小于N秒的请求,N支持配置。
1、zipkin服务端配置文件
application.yml
server: port: 9411 spring: main: allow-bean-definition-overriding: true application: name: zipkin-server cloud: nacos: discovery: server-addr: 192.168.103.240:8848 cluster-name: DEFAULT zipkin: enabled: true #启用zipkin discovery-client-enabled: false #服务端不注册zipkin服务 sleuth: enabled: false #服务端不进行信息采集 #数据库配置 datasource: driver-class-name: com.mysql.cj.jdbc.Driver schema[0]: classpath:/zipkin.sql #服务启动执行脚本自动创建表结构 url: jdbc:mysql://192.168.103.240:13126/cbs_svr_zipkin?zeroDateTimeBehavior=CONVERT_TO_NULL&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2b8&allowPublicKeyRetrieval=true username: root password: q!W#3e4r123456 initialization-mode: always continueOnError: true zipkin: #配置rabbitmq collector: rabbitmq: addresses: 192.168.103.240:5672 username: comtom password: comtom virtual-host: cbs #指定数据库类型 storage: type: mysql #此配置不影响信息采集,若不配置,会报一个错误,但不影响服务采集信息,配置上时为了避免打印报错日志 management: metrics: web: server: auto-time-requests: false #配置请求响应用时多少秒内的链路需要删除,默认删除1S内的链路跟踪信息。 1S = 1000000 time: 1000000
2、zipkin客户端配置文件
#链路跟踪配置
zipkin:
sender:
type: rabbit #请求调用发送到服务端方式
sleuth:
sampler:
percentage: 1 # 全局采样率,测试环境配置全采样,生产环境可以不配置,采用默认值0.1
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'; ALTER TABLE zipkin_annotations ADD INDEX(`a_key`) COMMENT 'for getTraces'; 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, `error_count` BIGINT, `call_count` BIGINT ) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci; ALTER TABLE zipkin_dependencies ADD UNIQUE KEY(`day`, `parent`, `child`);
pom依赖
zipkin服务端:
一、zipkin服务
pom文件加入一下依赖:
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-server</artifactId> <version>2.12.3</version> <!--排除日志,否则报错--> <exclusions> <exclusion> <artifactId>log4j-slf4j-impl</artifactId> <groupId>org.apache.logging.log4j</groupId> </exclusion> </exclusions> </dependency> <!-- zipkin 界面--> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-autoconfigure-ui</artifactId> <version>2.12.3</version> </dependency> <!-- zipkin 存储到数据库需要引入此类 --> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-autoconfigure-storage-mysql</artifactId> <version>2.12.3</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!--<!–包含spring-cloud-starter-sleuth和spring-cloud-sleuth-zipkin两个依赖–>--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> <!--连接rabbitmq并配置--> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-autoconfigure-collector-rabbitmq</artifactId> <version>2.12.3</version> </dependency>
zipkin客户端:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<!-- 使用消息的方式收集数据(使用rabbitmq) -->
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-collector-rabbitmq</artifactId>
<version>2.12.3</version>
</dependency>
链路采集:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。