本文共 6222 字,大约阅读时间需要 20 分钟。
在前面: 一文中我们利用Zipkin搭建了一个微服务调用链的追踪中心,并且模拟了微服务调用的实验场景。利用Zipkin的库Brave,我们可以收集一个客户端请求从发出到被响应 经历了哪些组件、哪些微服务、请求总时长、每个组件所花时长 等信息。
本文将讲述如何利用Zipkin对Mysql数据库的调用进行追踪,这里同样借助OpenZipkin库Brave来完成。
ZipkinTool是在一文中编写的与Zipkin通信的工具组件,利用其追踪微服务调用链的,现在我们想追踪Mysql数据库调用链的话,可以扩展一下其功能。
io.zipkin.brave brave-mysql 4.0.6
@Bean public MySQLStatementInterceptorManagementBean mySQLStatementInterceptorManagementBean() { return new MySQLStatementInterceptorManagementBean(brave().clientTracer()); }
依然继承前文:,我们改造一下文中的ServiceC这个微服务,在其中添加与Mysql数据库的交互。
org.springframework.boot spring-boot-starter-jdbc mysql mysql-connector-java runtime
spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://你的Mysql服务所在IP:3307/test?useSSL=false\ &statementInterceptors=com.github.kristofa.brave.mysql.MySQLStatementInterceptor\ &zipkinServiceName=mysqlServicespring.datasource.username=rootspring.datasource.password=XXXXXX
@GetMapping("/mysqltest”) public String mysqlTest() { String name = jdbcTemplate.queryForObject( "SELECT name FROM user WHERE id = 1", String.class ); return "Welcome " + name; }
1. 启动Mysql容器
docker run -d -p 3307:3306 \-v ~/mysql/data:/var/lib/mysql \-v ~/mysql/conf:/etc/mysql/conf.d \-e MYSQL_ROOT_PASSWORD=XXXXXX \--name mysql mysql
2. 再启动一个Mysql容器,接入其中做一些设置
docker run -it --rm \--link mysql:mysql mysql \mysql -hmysql -u root -p
CREATE DATABASE `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, `call_count` BIGINT, `error_count` BIGINT) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;ALTER TABLE zipkin_dependencies ADD UNIQUE KEY(`day`, `parent`, `child`);
这里创建了三个数据表。
该Sql文件可以从以下链接获得:
Sql脚本执行完成后,可以看到zipkin相关的三个表已经建成:
CREATE DATABASE `test`CREATE TABLE `user` ( `id` int(11) unsigned NOT NULL auto_increment, `name` varchar(100) DEFAULT NULL , PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET = utf8;insert into user values (1,”hansonwang99”)
这里插入了一条数据用于实验。
docker run -d -p 9411:9411 \--link mysql:mysql \-e STORAGE_TYPE=mysql \-e MYSQL_HOST=mysql \-e MYSQL_TCP_PORT=3306 \-e MYSQL_DB=zipkin \-e MYSQL_USER=root \-e MYSQL_PASS=XXXXXX \ --name zipkin openzipkin/zipkin
在浏览器中输入:localhost:8883/mysqltest,如果看到以下输出,就可以证明数据库调用操作已经成功了!
打开Zipkin Web UI,点击服务名下拉列表能看见已经成功识别了Mysql数据库调用服务
可以看到 首次查询 Mysql的调用链追踪信息,有很多
随便点开某一个查看:
目的是再次触发Mysql的调用,然后再次Find Traces,可以看到追踪数据类似下图:包含两次Mysql的query动作:
点开第一个query查看,其实际上是在 尝试连接Mysql数据库
点开第二个query查看,发现这里才是 实际查询业务
从图形化界面上可以清楚地知道每个阶段的详细步骤与耗时,因此可以用来分析哪个SQL语句执行相对较慢。
本文实验所用源码已经开源,需要的话请 。
作者更多的原创文章:
作者一些其他容器化、微服务化方面的文章:
转载地址:http://hzlgo.baihongyu.com/