1、创建三到四个微服务;
在链接(【免费】zipkin服务相关资源包,以及后续会有相关的说明配合当前文件的使用资源-CSDN文库)中的service1、service2、service3、service4分别为4个相关的服务,按照1、2、3、4的顺序进行启动即可,在application.yml中将数据库Mysql的地址、用户、密码与zipkin服务的地址进行配置,一般情况为本地127.0.0.1的地址
2、建立需要的表结构;
(1)CREATE TABLE `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) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci 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 DEFAULT NULL COMMENT 'Used to implement TTL; Annotation.timestamp or zipkin_spans.timestamp',
`endpoint_ipv4` int DEFAULT NULL COMMENT 'Null when Binary/Annotation.endpoint is null',
`endpoint_ipv6` binary(16) DEFAULT NULL COMMENT 'Null when Binary/Annotation.endpoint is null, or no IPv6 address',
`endpoint_port` smallint DEFAULT NULL COMMENT 'Null when Binary/Annotation.endpoint is null',
`endpoint_service_name` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL COMMENT 'Null when Binary/Annotation.endpoint is null',
UNIQUE KEY `trace_id_high` (`trace_id_high`,`trace_id`,`span_id`,`a_key`,`a_timestamp`) COMMENT 'Ignore insert on duplicate',
KEY `trace_id_high_2` (`trace_id_high`,`trace_id`,`span_id`) COMMENT 'for joining with zipkin_spans',
KEY `trace_id_high_3` (`trace_id_high`,`trace_id`) COMMENT 'for getTraces/ByIds',
KEY `endpoint_service_name` (`endpoint_service_name`) COMMENT 'for getTraces and getServiceNames',
KEY `a_type` (`a_type`) COMMENT 'for getTraces and autocomplete values',
KEY `a_key` (`a_key`) COMMENT 'for getTraces and autocomplete values',
KEY `trace_id` (`trace_id`,`span_id`,`a_key`) COMMENT 'for dependencies job'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=COMPRESSED;
(2)CREATE TABLE `zipkin_dependencies` (
`day` date NOT NULL,
`parent` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
`child` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
`call_count` bigint DEFAULT NULL,
`error_count` bigint DEFAULT NULL,
PRIMARY KEY (`day`,`parent`,`child`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=COMPRESSED;
(3)CREATE TABLE `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) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
`remote_service_name` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL,
`parent_id` bigint DEFAULT NULL,
`debug` bit(1) DEFAULT NULL,
`start_ts` bigint DEFAULT NULL COMMENT 'Span.timestamp(): epoch micros used for endTs query and to implement TTL',
`duration` bigint DEFAULT NULL COMMENT 'Span.duration(): micros used for minDuration and maxDuration query',
PRIMARY KEY (`trace_id_high`,`trace_id`,`id`),
KEY `trace_id_high` (`trace_id_high`,`trace_id`) COMMENT 'for getTracesByIds',
KEY `name` (`name`) COMMENT 'for getTraces and getSpanNames',
KEY `remote_service_name` (`remote_service_name`) COMMENT 'for getTraces and getRemoteServiceNames',
KEY `start_ts` (`start_ts`) COMMENT 'for getTraces ordering and range'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=COMPRESSED;
(4)CREATE TABLE `zipkin_spans_test` (
`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) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
`remote_service_name` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL,
`parent_id` bigint DEFAULT NULL,
`debug` bit(1) DEFAULT NULL,
`start_ts` bigint DEFAULT NULL COMMENT 'Span.timestamp(): epoch micros used for endTs query and to implement TTL',
`duration` bigint DEFAULT NULL COMMENT 'Span.duration(): micros used for minDuration and maxDuration query',
PRIMARY KEY (`trace_id_high`,`trace_id`,`id`),
KEY `name` (`name`) COMMENT 'for getTraces and getSpanNames',
KEY `remote_service_name` (`remote_service_name`) COMMENT 'for getTraces and getRemoteServiceNames',
KEY `start_ts` (`start_ts`) COMMENT 'for getTraces ordering and range',
KEY `trace_id_high` (`trace_id_high`,`trace_id`) COMMENT 'for getTracesByIds'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=COMPRESSED;
3、启动zipkin与以上提到的四个微服务;
在链接中(【免费】zipkin服务相关资源包,以及后续会有相关的说明配合当前文件的使用资源-CSDN文库)将zipkin的jar包下载下来,并且在下载路径下在命令行按照如下命令进行启动:
java -jar zipkin-server-2.11.8-exec.jar --STORAGE_TYPE=mysql --MYSQL_HOST=127.0.0.1 --MYSQL_TCP_PORT=3306 --MYSQL_DB=zipkin --MYSQL_USER=root --MYSQL_PASS=root
(备注:注意将mysql的地址、用户名、密码改成自己的相应信息)
然后按照顺序启动srevice1、srevice2、srevice3、srevice4四个微服务
4、利用postman调用接口,检验数据库中表数据变化
在浏览器中输入zipkin可视化界面的链接(http://127.0.0.1:9411), 利用postman调用接口(http://127.0.0.1:8080/hi),然后再zipkin的可视化界面观察,请求的链路信息。并且在数据库中查看相应的数据变化,如果有数据写入,证明链路追踪成功。(有感兴趣的同学,我们可以私聊进行相应的沟通)