概述
在前面:微服务调用链追踪中心搭建 一文中我们利用Zipkin搭建了一个微服务调用链的追踪中心,并且模拟了微服务调用的实验场景。利用Zipkin的库Brave,我们可以收集一个客户端请求从发出到被响应 经历了哪些组件、哪些微服务、请求总时长、每个组件所花时长 等信息。
本文将讲述如何利用Zipkin对Mysql数据库的调用进行追踪,这里同样借助OpenZipkin库Brave来完成。
扩展ZipkinTool组件
ZipkinTool是在《微服务调用链追踪中心搭建》一文中编写的与Zipkin通信的工具组件,利用其追踪微服务调用链的,现在我们想追踪Mysql数据库调用链的话,可以扩展一下其功能。
pom.xml添加依赖:
<dependency> <groupId>io.zipkin.brave</groupId> <artifactId>brave-mysql</artifactId> <version>4.0.6</version></dependency>
在ZipkinConfiguration类中添加MySQLStatementInterceptorManagementBean
@Bean public MySQLStatementInterceptorManagementBean mySQLStatementInterceptorManagementBean() { return new MySQLStatementInterceptorManagementBean(brave().clientTracer()); }
添加Mysql数据库访问的微服务
依然继承前文:《微服务调用链追踪中心搭建》,我们改造一下文中的ServiceC这个微服务,在其中添加与Mysql数据库的交互。
pom.xml中添加JDBC和Mysql依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
application.properties中添加Mysql连接的配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://你的Mysql服务所在IP:3307/test?useSSL=false\ &statementInterceptors=com.github.kristofa.brave.mysql.MySQLStatementInterceptor\ &zipkinServiceName=mysqlService spring.datasource.username=root spring.datasource.password=XXXXXX
Controller中添加JdbcTemplate访问数据库的代码
@GetMapping("/mysqltest”) public String mysqlTest() { String name = jdbcTemplate.queryForObject( "SELECT name FROM user WHERE id = 1", String.class ); return "Welcome " + name; }
启动Mysql数据库服务
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容器,接入其中做一些设置
首先进入mysql命令行
docker run -it --rm \--link mysql:mysql mysql \mysql -hmysql -u root -p
接下来创建数据库zipkin: 用于存放Zipkin所收集的数据
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文件可以从以下链接获得:https://github.com/openzipkin/zipkin/blob/master/zipkin-storage/mysql/src/main/resources/mysql.sql
Sql脚本执行完成后,可以看到zipkin相关的三个表已经建成:
创建数据库test:用作测试数据库
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”)
这里插入了一条数据用于实验。
启动zipkin服务
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
启动Mysql数据库访问的微服务(即ServiceC)
在浏览器中输入:localhost:8883/mysqltest,如果看到以下输出,就可以证明数据库调用操作已经成功了!
Zipkin追踪数据库调用实际实验
**浏览器输入:**http://localhost:9411/zipkin/
打开Zipkin Web UI,点击服务名下拉列表能看见已经成功识别了Mysql数据库调用服务
选中mysqlservice后,点击Find Traces
可以看到 首次查询 Mysql的调用链追踪信息,有很多
随便点开某一个查看:
**接下来浏览器中再次输入:**localhost:8883/mysqltest
目的是再次触发Mysql的调用,然后再次Find Traces,可以看到追踪数据类似下图:包含两次Mysql的query动作:
点开第一个query查看,其实际上是在 尝试连接Mysql数据库
点开第二个query查看,发现这里才是 实际查询业务
从图形化界面上可以清楚地知道每个阶段的详细步骤与耗时,因此可以用来分析哪个SQL语句执行相对较慢。
作者:CodeSheep
来源:https://my.oschina.net/hansonwang99/blog/1807943