SpringCloudSleuth整合Zipkin
Sleuth是一个Spring Cloud的分布式跟踪解决方案。它由以下几个方面组成
- Span(跨度):Sleuth的基本工作单元,它由一个64位的id唯一标识。除ID外,span还包含其他数据,例如描述、时间戳事件、键值对的注解(标签)、span ID,span父ID等。
- trace(跟踪):一组span组成的树状结构称为trace.
- Annotation(标注)
- CS(Client Sent客户端发送) 客户端发起一个请求,该annotation描述了span的开始。
- SR(Server Received服务端接收) 服务端获得请求,并准备处理它。
- SS(Server Sent服务器端发送) 该annotation表明完成请求处理,当响应发回客户端时。
- CR(Client Received客户端接收) span结束的标识。客户端成功接收到服务端到相应。
现在我们来具体看一下Sleuth的作用是什么,当我们没有加Sleuth依赖的时候,假设我们要访问这样一个接口
@GetMapping(value = "/users-anon/internal", params = "username")public LoginAppUser findByUsername(String username) {
return appUserService.findByUsername(username);
}
在日志中,我们可以看到是这个样子的
2019-11-01 00:17:05.402 DEBUG 805 --- [nio-8001-exec-1] c.c.u.d.U.findUserByUsername : ==> Preparing: select u.* from app_user u inner join user_credentials c on c.userId = u.id where c.username = ?
2019-11-01 00:17:05.426 DEBUG 805 --- [nio-8001-exec-1] c.c.u.d.U.findUserByUsername : ==> Parameters: admin(String)
2019-11-01 00:17:05.503 DEBUG 805 --- [nio-8001-exec-1] c.c.u.d.U.findUserByUsername : <== Total: 1
2019-11-01 00:17:05.522 INFO 805 --- [nio-8001-exec-1] c.c.u.service.impl.AppUserServiceImpl : {"createTime":1516179419000,"enabled":true,"headImgUrl":"","id":1,"nickname":"测试1","password":"$2a$10$3uOoX1ps14CxuotogUoDreW8zXJOZB9XeGdrC/xDV36hhaE8Rn9HO","phone":"","province":"广东","receiveAddress":"广州天河珠村东横五路红噗公寓367","sex":1,"type":"APP","updateTime":1516179421000,"userIdentity":"SERVICEOPERATOR","username":"admin"}
2019-11-01 00:17:05.528 DEBUG 805 --- [nio-8001-exec-1] c.c.u.dao.UserRoleDao.findRolesByUserId : ==> Preparing: select r.* from sys_role_user ru inner join sys_role r on r.id = ru.roleId where ru.userId = ?
2019-11-01 00:17:05.529 DEBUG 805 --- [nio-8001-exec-1] c.c.u.dao.UserRoleDao.findRolesByUserId : ==> Parameters: 1(Long)
2019-11-01 00:17:05.584 DEBUG 805 --- [nio-8001-exec-1] c.c.u.dao.UserRoleDao.findRolesByUserId : <== Total: 1
2019-11-01 00:17:05.605 DEBUG 805 --- [nio-8001-exec-1] c.c.u.d.R.findPermissionsByRoleIds : ==> Preparing: select p.* from sys_permission p inner join sys_role_permission rp on p.id = rp.permissionId WHERE rp.roleId in ( ? )
2019-11-01 00:17:05.606 DEBUG 805 --- [nio-8001-exec-1] c.c.u.d.R.findPermissionsByRoleIds : ==> Parameters: 1(Long)
2019-11-01 00:17:06.245 DEBUG 805 --- [nio-8001-exec-1] c.c.u.d.R.findPermissionsByRoleIds : <== Total: 31
除了mybatis的sql代码外,就只能看到类似nio-8001-exec-1这样的线程名了。现在我们加入依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId></dependency>
重启服务,我们再来看一下日志
2019-11-01 00:23:01.469 DEBUG [user-center,d406e2f823a1c2a3,d406e2f823a1c2a3,false] 816 --- [nio-8001-exec-1] c.c.u.d.U.findUserByUsername : ==> Preparing: select u.* from app_user u inner join user_credentials c on c.userId = u.id where c.username = ?
2019-11-01 00:23:01.487 DEBUG [user-center,d406e2f823a1c2a3,d406e2f823a1c2a3,false] 816 --- [nio-8001-exec-1] c.c.u.d.U.findUserByUsername : ==> Parameters: admin(String)
2019-11-01 00:23:01.649 DEBUG [user-center,d406e2f823a1c2a3,d406e2f823a1c2a3,false] 816 --- [nio-8001-exec-1] c.c.u.d.U.findUserByUsername : <== Total: 1
2019-11-01 00:23:01.671 INFO [user-center,d406e2f823a1c2a3,d406e2f823a1c2a3,false] 816 --- [nio-8001-exec-1] c.c.u.service.impl.AppUserServiceImpl : {"createTime":1516179419000,"enabled":true,"headImgUrl":"","id":1,"nickname":"测试1","password":"$2a$10$3uOoX1ps14CxuotogUoDreW8zXJOZB9XeGdrC/xDV36hhaE8Rn9HO","phone":"","province":"广东","receiveAddress":"广州天河珠村东横五路红噗公寓367","sex":1,"type":"APP","updateTime":1516179421000,"userIdentity":"SERVICEOPERATOR","username":"admin"}
2019-11-01 00:23:01.679 DEBUG [user-center,d406e2f823a1c2a3,d406e2f823a1c2a3,false] 816 --- [nio-8001-exec-1] c.c.u.dao.UserRoleDao.findRolesByUserId : ==> Preparing: select r.* from sys_role_user ru inner join sys_role r on r.id = ru.roleId where ru.userId = ?
2019-11-01 00:23:01.679 DEBUG [user-center,d406e2f823a1c2a3,d406e2f823a1c2a3,false] 816 --- [nio-8001-exec-1] c.c.u.dao.UserRoleDao.findRolesByUserId : ==> Parameters: 1(Long)
2019-11-01 00:23:01.834 DEBUG [user-center,d406e2f823a1c2a3,d406e2f823a1c2a3,false] 816 --- [nio-8001-exec-1] c.c.u.dao.UserRoleDao.findRolesByUserId : <== Total: 1
2019-11-01 00:23:01.852 DEBUG [user-center,d406e2f823a1c2a3,d406e2f823a1c2a3,false] 816 --- [nio-8001-exec-1] c.c.u.d.R.findPermissionsByRoleIds : ==> Preparing: select p.* from sys_permission p inner join sys_role_permission rp on p.id = rp.permissionId WHERE rp.roleId in ( ? )
2019-11-01 00:23:01.852 DEBUG [user-center,d406e2f823a1c2a3,d406e2f823a1c2a3,false] 816 --- [nio-8001-exec-1] c.c.u.d.R.findPermissionsByRoleIds : ==> Parameters: 1(Long)
2019-11-01 00:23:01.980 DEBUG [user-center,d406e2f823a1c2a3,d406e2f823a1c2a3,false] 816 --- [nio-8001-exec-1] c.c.u.d.R.findPermissionsByRoleIds : <== Total: 31
现在我们可以看到类似于user-center,d406e2f823a1c2a3,d406e2f823a1c2a3,false的东西。
- user-center很好理解,就是我们的项目名称。
- 第一个d406e2f823a1c2a3为traceID
- 第二个d406e2f823a1c2a3为spanID
- false表示是否将此数据上传给zipkin
当然要打印这些日志,我们需要在配置文件中加入如下配置信息
logging:level:
root: info
com.cloud: debug
file: logs/${spring.application.name}.log
现在我们来看一下zipkin。
- Zipkin是Twitter开源的分布式跟踪系统,主要用来收集系统的时序数据,从而跟踪系统的调用问题。
要使用zipkin我们需要先下载zipkin server,下载方式为
curl -sSL https://zipkin.io/quickstart.sh | bash -s
然后启动它
java -jar zipkin.jar
当然我们也可以使用docker,建议在服务器上使用
docker pull openzipkin/zipkin
docker run -d --name zipkin -p 9411:9411 openzipkin/zipkin
启动之后,我们可以访问zipkin的UI界面
http://127.0.0.1:9411/zipkin/
在我们的项目中添加依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId></dependency>
这里zipkin的依赖已经包含了sleuth的依赖,所以我们可以去掉了之前sleuth的依赖。
添加配置
spring:zipkin:
#。 在服务器上填入服务器的IP
base-url: http://localhost:9411
enabled: true
sender:
type: web
sleuth:
sampler:
# 抽样率,默认是0.1(10%),现在取100%,只为测试方便,生产环境勿设100%
probability: 1.0
现在我们重新启动项目,进行一次请求访问,日志为
2019-11-01 01:04:03.187 DEBUG [user-center,d7fa432636d9df52,d7fa432636d9df52,true] 956 --- [nio-8001-exec-1] c.c.u.d.U.findUserByUsername : ==> Preparing: select u.* from app_user u inner join user_credentials c on c.userId = u.id where c.username = ?
2019-11-01 01:04:03.203 DEBUG [user-center,d7fa432636d9df52,d7fa432636d9df52,true] 956 --- [nio-8001-exec-1] c.c.u.d.U.findUserByUsername : ==> Parameters: admin(String)
2019-11-01 01:04:03.276 DEBUG [user-center,d7fa432636d9df52,d7fa432636d9df52,true] 956 --- [nio-8001-exec-1] c.c.u.d.U.findUserByUsername : <== Total: 1
2019-11-01 01:04:03.290 INFO [user-center,d7fa432636d9df52,d7fa432636d9df52,true] 956 --- [nio-8001-exec-1] c.c.u.service.impl.AppUserServiceImpl : {"createTime":1516179419000,"enabled":true,"headImgUrl":"","id":1,"nickname":"测试1","password":"$2a$10$3uOoX1ps14CxuotogUoDreW8zXJOZB9XeGdrC/xDV36hhaE8Rn9HO","phone":"","province":"广东","receiveAddress":"广州天河珠村东横五路红噗公寓367","sex":1,"type":"APP","updateTime":1516179421000,"userIdentity":"SERVICEOPERATOR","username":"admin"}
2019-11-01 01:04:03.294 DEBUG [user-center,d7fa432636d9df52,d7fa432636d9df52,true] 956 --- [nio-8001-exec-1] c.c.u.dao.UserRoleDao.findRolesByUserId : ==> Preparing: select r.* from sys_role_user ru inner join sys_role r on r.id = ru.roleId where ru.userId = ?
2019-11-01 01:04:03.294 DEBUG [user-center,d7fa432636d9df52,d7fa432636d9df52,true] 956 --- [nio-8001-exec-1] c.c.u.dao.UserRoleDao.findRolesByUserId : ==> Parameters: 1(Long)
2019-11-01 01:04:03.347 DEBUG [user-center,d7fa432636d9df52,d7fa432636d9df52,true] 956 --- [nio-8001-exec-1] c.c.u.dao.UserRoleDao.findRolesByUserId : <== Total: 1
2019-11-01 01:04:03.367 DEBUG [user-center,d7fa432636d9df52,d7fa432636d9df52,true] 956 --- [nio-8001-exec-1] c.c.u.d.R.findPermissionsByRoleIds : ==> Preparing: select p.* from sys_permission p inner join sys_role_permission rp on p.id = rp.permissionId WHERE rp.roleId in ( ? )
2019-11-01 01:04:03.368 DEBUG [user-center,d7fa432636d9df52,d7fa432636d9df52,true] 956 --- [nio-8001-exec-1] c.c.u.d.R.findPermissionsByRoleIds : ==> Parameters: 1(Long)
2019-11-01 01:04:03.433 DEBUG [user-center,d7fa432636d9df52,d7fa432636d9df52,true] 956 --- [nio-8001-exec-1] c.c.u.d.R.findPermissionsByRoleIds : <== Total: 31
这里我们可以看到所有的user-center,d7fa432636d9df52,d7fa432636d9df52,true都已经为true了,表示全部提交给zipkin了。
我们多刷新访问几次,来看一下zipkin的结果记录
在页面中点查找,结果如下
由于我们这个接口在数据库中进行了3次查询,并且我这里连接的是云端数据库,所以时间会比较长一点,但无论如何,这里都会记录下每次调用的时间,它是按降序排序的,这样我们就可以很轻松的插看到每次调用的情况,知道调用访问哪里耗时。我们点击某一个进去,可以看到
再点击如上的service,可以看到更加详细的信息
这里只有SR,SC的两条数据,因为我们是使用浏览器访问的,而浏览器并没有集成zipkin,它不会上报自己的数据,所以这里没有CS,CR的数据。如果我们在多个微服务之间都配置了zipkin,此处就会把所有的SR,SC,CS,CR的数据都上报上来。
以上是 SpringCloudSleuth整合Zipkin 的全部内容, 来源链接: utcz.com/z/510286.html