一、spring cloud sleuth
提供链路追踪。通过sleuth可以很清楚的看出一个请求都经过了哪些服务;可以很方便的理清服务间的调用关系。
可视化错误。对于程序未捕捉的异常,可以结合zipkin分析。
分析耗时。通过sleuth可以很方便的看出每个采样请求的耗时,分析出哪些服务调用比较耗时。当服务调用的耗时随着请求量的增大而增大时,也可以对服务的扩容提供一定的提醒作用。
从官网得知 从2.1.0版开始,Spring Cloud Sleuth支持将跟踪发送到多个跟踪系统,且去掉了 spring cloud streaming,那如果只引入sleuth包,不同机器服务之间相互调用是否还能实现链路追踪?经过测试答案是可以的,源码这里我没研究,但是从技术角度要自己实现,其实只要在 header 里添加一个值(唯一),在上下游服务之间传递,便可行。
所以结论是如果只是单纯为了使用链路追踪在控制台看,只引入 sleuth 是已经足够的。
二、spring cloud zipkin
Zipkin是Twitter的一个开源项目,我们可以使用它来收集各个服务器上请求链路的跟踪数据,并通过它提供的API接口来辅助查询跟踪数据以分布式系统的监控程序,通过UI组件帮助我们及时发现系统中出现的延迟升高问题以及系统性能瓶颈根源。
Zipkin的基础架构
Collector(收集器组件)-> 主要负责收集外部系统跟踪信息,转化为Zipkin内部的Span格式。
Storage(存储组件)-> 主要负责收到的跟踪信息的存储,默认为存储在内存中,同时支持存储到Mysql、Cassandra以及ElasticSearch。
API(Query)-> 负责查询Storage中存储的数据,提供简单的JSON API获取数据,主要提供给web UI使用。
Web UI(展示组件)-> 提供简单的web界面,方便进行跟踪信息的查看以及查询,同时进行相关的分析。
Instrumented Client 和Instrumented Server,是指分布式架构中使用了Trace工具的两个应用,Client会调用Server提供的服务,两者都会向Zipkin上报Trace相关信息。在Client 和 Server通过Transport上报Trace信息后,由Zipkin的Collector模块接收,并由Storage模块将数据存储在对应的存储介质中,然后Zipkin提供API供UI界面查询Trace跟踪信息。Non-Instrumented Server,指的是未使用Trace工具的Server,显然它不会上报Trace信息。
三、日志解析
2020-06-19 11:10:44.967 DEBUG [bff-web-data-platform,368e435f7de29eff,368e435f7de29eff,false] 2516 --- [nio-8888-exec-5] c.giant.cloud.api.ElectricityStationApi : [ElectricityStationApi#getDashboardElectricityStationData] Content-Length: 1921
2020-06-19 11:10:44.967 DEBUG [bff-web-data-platform,368e435f7de29eff,368e435f7de29eff,false] 2516 --- [nio-8888-exec-5] c.giant.cloud.api.ElectricityStationApi : [ElectricityStationApi#getDashboardElectricityStationData] Content-Type: application/json
2020-06-19 11:10:44.968 DEBUG [bff-web-data-platform,368e435f7de29eff,368e435f7de29eff,false] 2516 --- [nio-8888-exec-5] c.giant.cloud.api.ElectricityStationApi : [ElectricityStationApi#getDashboardElectricityStationData]
2020-06-19 11:10:44.968 DEBUG [bff-web-data-platform,368e435f7de29eff,368e435f7de29eff,false] 2516 --- [nio-8888-exec-5] c.giant.cloud.api.ElectricityStationApi : [ElectricityStationApi#getDashboardElectricityStationData] {"data":{"createTime":null,"updateTime":null,"deleteFlag":null,"id":null,"menuId":null,"name":null,"buildingDegree":null,"cityName":null,"cityId":null,"lng":null,"lat":null,"remarks":null},"token":"5a717dca1142d685c8aa54b45d0388c8","user":{"id":1,"account":"15771720565","name":"冯**","phone":"15771720565","onlineState":null,"remarks":"fdsafdsa","rolesId":1,"avatar":null,"token":"5a717dca1142d685c8aa54b45d0388c8","roles":{"id":1,"roleName":"超级管理员","remarks":"2222","menus":null},"menus":[{"id":1,"parentId":0,"menuName":"大屏展示","menuPath":"/dashboard","menuOrder":1000,"icon":"el-icon-monitor","remarks":null,"menuData":false,"menuButton":false,"child":[]},{"id":2,"parentId":0,"menuName":"数据分析","menuPath":"/dataAnalysis","menuOrder":2000,"icon":"el-icon-data-line","remarks":null,"menuData":false,"menuButton":false,"child":[]},{"id":3,"parentId":0,"menuName":"系统管理","menuPath":"/setting","menuOrder":3000,"icon":"el-icon-setting","remarks":null,"menuData":false,"menuButton":false,"child":[{"id":26,"parentId":3,"menuName":"用户管理","menuPath":"/setting/user","menuOrder":3100,"icon":null,"remarks":null,"menuData":false,"menuButton":false,"child":[]},{"id":5,"parentId":3,"menuName":"角色管理","menuPath":"/setting/roles","menuOrder":3200,"icon":null,"remarks":null,"menuData":false,"menuButton":false,"child":[]},{"id":6,"parentId":3,"menuName":"菜单管理","menuPath":"/setting/menus","menuOrder":3300,"icon":null,"remarks":null,"menuData":false,"menuButton":false,"child":[]}]},{"id":16,"parentId":0,"menuName":"电站管理","menuPath":"/electricityStation","menuOrder":4000,"icon":"el-icon-lightning","remarks":null,"menuData":false,"menuButton":false,"child":[{"id":17,"parentId":16,"menuName":"电站信息","menuPath":"/electricityStation/list","menuOrder":4100,"icon":null,"remarks":"列出所有电站","menuData":false,"menuButton":false,"child":[]}]}]}}
2020-06-19 11:10:44.968 DEBUG [bff-web-data-platform,368e435f7de29eff,368e435f7de29eff,false] 2516 --- [nio-8888-exec-5] c.giant.cloud.api.ElectricityStationApi : [ElectricityStationApi#getDashboardElectricityStationData] ---> END HTTP (1921-byte body)
2020-06-19 11:10:44.998 DEBUG [bff-web-data-platform,368e435f7de29eff,368e435f7de29eff,false] 2516 --- [nio-8888-exec-5] c.giant.cloud.api.ElectricityStationApi : [ElectricityStationApi#getDashboardElectricityStationData]
2020-06-19 11:10:44.999 DEBUG [bff-web-data-platform,368e435f7de29eff,368e435f7de29eff,false] 2516 --- [nio-8888-exec-5] c.giant.cloud.api.ElectricityStationApi : [ElectricityStationApi#getDashboardElectricityStationData] connection: keep-alive
2020-06-19 11:10:44.999 DEBUG [bff-web-data-platform,368e435f7de29eff,368e435f7de29eff,false] 2516 --- [nio-8888-exec-5] c.giant.cloud.api.ElectricityStationApi : [ElectricityStationApi#getDashboardElectricityStationData] content-type: application/json
2020-06-19 11:10:44.999 DEBUG [bff-web-data-platform,368e435f7de29eff,368e435f7de29eff,false] 2516 --- [nio-8888-exec-5] c.giant.cloud.api.ElectricityStationApi : [ElectricityStationApi#getDashboardElectricityStationData] date: Fri, 19 Jun 2020 03:10:44 GMT
2020-06-19 11:10:44.999 DEBUG [bff-web-data-platform,368e435f7de29eff,368e435f7de29eff,false] 2516 --- [nio-8888-exec-5] c.giant.cloud.api.ElectricityStationApi : [ElectricityStationApi#getDashboardElectricityStationData] keep-alive: timeout=60
2020-06-19 11:10:44.999 DEBUG [bff-web-data-platform,368e435f7de29eff,368e435f7de29eff,false] 2516 --- [nio-8888-exec-5] c.giant.cloud.api.ElectricityStationApi : [ElectricityStationApi#getDashboardElectricityStationData] transfer-encoding: chunked
2020-06-19 11:10:44.999 DEBUG [bff-web-data-platform,368e435f7de29eff,368e435f7de29eff,false] 2516 --- [nio-8888-exec-5] c.giant.cloud.api.ElectricityStationApi : [ElectricityStationApi#getDashboardElectricityStationData]
2020-06-19 11:10:44.999 DEBUG [bff-web-data-platform,368e435f7de29eff,368e435f7de29eff,false] 2516 --- [nio-8888-exec-5] c.giant.cloud.api.ElectricityStationApi : [ElectricityStationApi#getDashboardElectricityStationData] {"code":200,"msg":null,"data":[{"name":"平顶山","value":30},{"name":"西安","value":50},{"name":"北京","value":43}]}
2020-06-19 11:10:45.000 DEBUG [bff-web-data-platform,368e435f7de29eff,368e435f7de29eff,false] 2516 --- [nio-8888-exec-5] c.giant.cloud.api.ElectricityStationApi : [ElectricityStationApi#getDashboardElectricityStationData]
[bff-web-data-platform, 368e435f7de29eff, 368e435f7de29eff, false] 解析:
bff-web-data-platform 称为 appname -> 应用名称
368e435f7de29eff 称为 traceId -> 为了追踪一个请求完整的流转过程,可以给每次请求分配一个唯一的 traceId,当请求调用其他服务时,通过传递这个 traceId。
368e435f7de29eff 称为 spanId -> 发生的特定操作的ID
false 称为 exportable -> 是否应将日志导出到Zipkin。
无论是使用 sleuth 或是使用 zipkin 都会在控制台上输出这样的日志信息。