0x00 关于zipkin
zipkin的介绍网上有很多, 这里不再赘述(太懒,不想打字)。简而言之:分析多个微服务之间的调用关系并分析时间消耗。
- 本文作用及提醒:
- 记录zipkin服务端的搭建过程,最终我们可以得到一个UI版本的zipkin服务。
- 这里采用
mysql
做为数据存储。但据官方介绍,生产中数据量比较大时,响应可能会比较慢。
The MySQL v1 component uses MySQL 5.6+ features, but is tested against MariaDB 10.3.
The schema was designed to be easy to understand and get started with; it was not designed for performance. Ex spans fields are columns, so you can perform ad-hoc queries using SQL. However, this component has known performance issues: queries will eventually take seconds to return if you put a lot of data into it.
This store does not require a job to aggregate dependency links. However, running the job will improve performance of dependencies queries.
0x01 数据库准备
我们采用mysql作为数据存储载体,但需要手动初始化表结构。
关于mysql的安装请参考这里。
登录mysql后,先创建数据库create database zipkin
。
然后 use zipkin
后,执行这里的SQL文:
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,
`remote_service_name` VARCHAR(255),
`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',
PRIMARY KEY (`trace_id_high`, `trace_id`, `id`)
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;
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(`remote_service_name`) COMMENT 'for getTraces and getRemoteServiceNames';
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 and autocomplete values';
ALTER TABLE zipkin_annotations ADD INDEX(`a_key`) COMMENT 'for getTraces and autocomplete values';
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,
`call_count` BIGINT,
`error_count` BIGINT,
PRIMARY KEY (`day`, `parent`, `child`)
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;
到这里数据准备完成, 具体访问方式为
mysql://localhost:3306/zipkin
root用户本机访问,可以不需要密码访问。
0x02. 下载Jar包
这里我们采用jar包方式直接运行。
我们参考官方教程,先下载jar包:
curl -sSL https://zipkin.io/quickstart.sh | bash -s
下载zipkin Jar文件
0x03 启动Jar包
关于 zipkin的存储类型, 是通过环境变量进行设置生效的。
参考这里,我们需要处理的环境变量如下:
-
MYSQL_DB
: The database to use. Defaults to "zipkin". -
MYSQL_USER
andMYSQL_PASS
: MySQL authentication, which defaults to empty string. -
MYSQL_HOST
: Defaults to localhost -
MYSQL_TCP_PORT
: Defaults to 3306 -
MYSQL_MAX_CONNECTIONS
: Maximum concurrent connections, defaults to 10 -
MYSQL_USE_SSL
: Requiresjavax.net.ssl.trustStore
andjavax.net.ssl.trustStorePassword
, defaults to false.
考虑到有很多值与默认值一致,我们得到最后的启动命令:
nohup STORAGE_TYPE=mysql MYSQL_USER=root MYSQL_DB=zipkin java -jar zipkin.jar 2>&1 &
启动日志
0x04 验证
默认zipkin开启的是 9411
端口,此时我们可以在浏览器验证,如果能正常打开,表示安装完成。
0x05 后记
本来打算结合kong的zipkin插件进行实践的,然而第一回合完败-_-!!!
どうして~~~
网友评论