影响网站收录的因素,做外贸用什么网站,怎么删除安装的wordpress,多语言多风格网站方案1. 概述
Spring Cloud Sleuth实现对Spring cloud 分布式链路监控 本文介绍了和Sleuth相关的内容#xff0c;主要内容如下#xff1a;
Spring Cloud Sleuth中的重要术语和意义#xff1a;Span、Trance、AnnotationZipkin中图形化展示分布式链接监控数据并说明字段意义Spring …1. 概述
Spring Cloud Sleuth实现对Spring cloud 分布式链路监控 本文介绍了和Sleuth相关的内容主要内容如下
Spring Cloud Sleuth中的重要术语和意义Span、Trance、AnnotationZipkin中图形化展示分布式链接监控数据并说明字段意义Spring Cloud集成Sleuth Zipkin 的代码demo: Sleuth集成Zipkin, Zipkin数据持久化等
2. 术语
Span Span是基本的工作单元。Span包括一个64位的唯一ID一个64位trace码描述信息时间戳事件key-value 注解(tags)span处理者的ID通常为IP。 最开始的初始Span称为根span此span中span id和 trace id值相同。
Trance 包含一系列的span它们组成了一个树型结构
Annotation 用于及时记录存在的事件。常用的Annotation如下
cs - Client Sent客户端发送一个请求表示span的开始sr - Server Received服务端接收请求并开始处理它。(sr-cs)等于网络的延迟ss - Server Sent服务端处理请求完成开始返回结束给服务端。(ss-sr)表示服务端处理请求的时间cr - Client Received客户端完成接受返回结果此时span结束。(cr-sr)表示客户端接收服务端数据的时间
如果一个服务的调用关系如下
那么此时将Span和Trace在一个系统中使用Zipkin注解的过程图形化
每个颜色的表明一个span(总计7个spans从A到G)每个span有类似的信息
Trace Id X
Span Id D
Client Sent
123
此span表示span的Trance Id是XSpan Id是D同时它发送一个Client Sent事件
spans 的parent/child关系图形化如下
3. 在Zipkin中图形化展示分布式链接监控数据
3.1 spans在zipkin界面的信息解读
在Zipkin中展示了上图的跟踪信息红框里是对上图调用span的跟踪
但是你点击这个trace时我们只看到4个span
为什么两个界面显示的span数量不同一个是7一个4.
1 个 spans来自servier1的接口http:/start 被调用分别是Server Received (SR) 和 Server Sent (SS) annotations.2 个 spans来自 service1 调用service2 的 http:/foo 接口。service1 端有两个span分别为 Client Sent (CS) 和 Client Received (CR) annotations。service2 端也有两个span分别为Server Received (SR) 和Server Sent (SS) 。物理上他有2个span但是从逻辑上说这个他们组成一个RPC调用的span。2个span来自 service2 调用 service3 的 http:/bar 接口service2 端有两个span分别为Client Sent (CS) 和 Client Received (CR) annotations。service3 端也有两个span分别为Server Received (SR) 和Server Sent (SS) 。物理上他有2个span但是从逻辑上说它们都是同一个RPC调用的span。2个span来自 service2 调用 service4 的 http:/bar 接口service2 端有两个span分别为Client Sent (CS) 和 Client Received (CR) annotations。service4 端也有两个span分别为Server Received (SR) and Server Sent (SS) 。物理上他有2个span但是从逻辑上说这个它们都是同一个RPC调用的span。
所以我们计算物理spans有7个
1个来自 http:/start 被请求2个来自 service1调用service22个来自 service2调用service32个来自 service2调用service4.
从逻辑上说我们只看到4个span:
1个来自 service1 的接口http:/start 被请求3个来自 服务之前的RCP接口调用
3.2. Zipkin可视化错误
如果调用链路中发生接口调用失败zipkin会默认使用红色展示信息如下图 点击红色的span可以看到详细的失败信息
4. Spring Cloud集成Sleuth Zipkin
本节演示在Spring Cloud中集成Sleuth并将链路监控数据传送到Zipkin使用Zipkin展示数据并配置mysql进行数据持久化
4.1. 相关工程
cloud-registration-center 注册中心
cloud-service-zipkin 提供测试服务接口对外提供有两个接口 - 调用成功后马上返回一个结果 http://127.0.0.1:17602//zipkin/simple - 调用成功后服务sleep 5s后再返回 http://127.0.0.1:17602//zipkin/sleep
cloud-consumer-zipkin 消费服务 此服务会通过Feign调用cloud-service-zipkin里提供的两个接口(/zipkin/sleep和/zipkin/simple)我们访问如下URL会调用的对应的接口 http://127.0.0.1:17603//zipkin/simple http://127.0.0.1:17603//zipkin/sleep
以上3个服务的代码比较简单这里代码略可以自己看github里的代码。下文中只列出和Sleuth和Zipkin相关的配置。
cloud-dashboard-zipkin 配置Zipkin服务提供可视化链路监控 Zipkin首页http://127.0.0.1:17601/zipkin/
4.2. 在工程中使用sleuthzipkinhttp配置
在cloud-service-zipkin和cloud-consumer-zipkin中启动sleuthsleuth会收集spans信息并使用http异步地将spans 信息发送到Zipkin然后Zipkin展示信息
cloud-service-zipkin和cloud-consumer-zipkin配置SleuthZipkin 2个工程中为了集成Sleuth和Zipkin需要在普通工程基础上增加如下配置
pom.xml增加sleuth和zipkin相关的jar
!-- sleuth配置 --
dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-sleuth/artifactId
/dependency
!-- 引入zipkin --
dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-zipkin/artifactId
/dependency
12345678910
application-zipkin.yml ZipkinSleuth配置参数 spring.zipkin.baseUrl指定cloud-dashboard-zipkin的服务地址本例子中使用真实的IP。在新的版本spring-cloud-sleuth-core-1.3.0.RELEASE中可以实现通过服务名称进行访问spring.sleuth.sampler.percentage设置采样率为了测试设置100%采集
spring:zipkin:enabled: true# zipkkin dashboard的地址通过真实IP地址访问baseUrl: http://localhost:17601/# 通过cloud-dashboard-zipkin注册到注册中心的服务名称访问本版本(spring-cloud-sleuth-core-1.2.5.RELEASE)不支持需要从spring-cloud-sleuth-core-1.3.0.RELEASE开始支持这个功能# 配置如下# baseUrl: http://cloud-dashboard-zipkin/sleuth:sampler:# 默认值为0.1f现在为了测试设置100%采集percentage: 1
123456789101112
cloud-dashboard-zipkin 配置zipkin服务
pom.xml
!-- spring-cloud-starter-zipkin --
dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-zipkin/artifactId
/dependency
!-- zipkin 界面--
dependencygroupIdio.zipkin.java/groupIdartifactIdzipkin-autoconfigure-ui/artifactId
/dependency
!-- zipkin 服务类--
dependencygroupIdio.zipkin.java/groupIdartifactIdzipkin-server/artifactId
/dependency
123456789101112131415
配置参数 bootstrap-zipkin-http.yml
# port
server:port: 17601spring:application:# 本服务注册到注册到服务器的名称, 这个名称就是后面调用服务时的服务标识符name: cloud-dashboard-zipkin
eureka:client:serviceUrl:# 服务器注册/获取服务器的zonedefaultZone: http://127.0.0.1:10761/eureka/# defaultZone: http://192.168.21.3:10761/eureka/,http://192.168.21.4:10761/eureka/instance:prefer-ip-address: true
12345678910111213141516
application-zipkin-http.yml 关闭本工程的推送到zipkin服务的功能
spring:zipkin:enabled: false
123
启动类 EnableZipkinServer注解此类为Zipkin服务
EnableEurekaClient // 配置本应用将使用服务注册和服务发现
SpringBootApplication
EnableZipkinServer // 启动Zipkin服务
public class ZipkinDashboardCloudApplication {public static void main(String[] args) {args new String[1];args[0] --spring.profiles.activezipkin-http;SpringApplication.run(ZipkinDashboardCloudApplication.class, args);}
12345678910
4.3. 测试
启动zipkin服务提供可视化链路监控 Zipkin访问地址 http://127.0.0.1:17601/zipkin/
我们演示链路跟踪访问以下两个请求 http://127.0.0.1:17603/zipkin/simple 正常方法 http://127.0.0.1:17603/zipkin/sleep 访问超时
进入zipkin访问成功为蓝色失败为红部。同时显示各个服务花费的时间
点击蓝色记录进入详细界面 可知整个接口花费约19mscloud-zipkin-service的业务执行15mscloud-zipkin-consumer访问cloud-zipkin-service的 网络延迟为2ms返回结果给cloud-zipkin-service的网络延迟是4s
点击cloud-zipkin-service得到更精确的数据4.4. Zipkin数据持久化
默认情况zipkin存储记录到内存如果服务重启则所有记录丢失。为了保证持久性zipkin支持Mysql、Elasticsearch、Cassandra存储。我们演示为zipkin配置MYSQL数据库其它两种配置类似本文略 pom.xml为cloud-dashboard-zipkin引入新jar!-- zipkin 存储到数据库需要引入此类 --
dependencygroupIdio.zipkin.java/groupIdartifactIdzipkin-autoconfigure-storage-mysql/artifactId
/dependency!--保存到数据库需要如下依赖--
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-jdbc/artifactId
/dependency
dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion6.0.6/version
/dependency1234567891011121314151617
配置参数 application-zipkin-http.yml 配置启动时会根据classpath:/mysql.sql初始化数据库
spring:zipkin:enabled: false# 配置mysqldatasource:schema: classpath:/mysql.sql# url: jdbc:mysql://127.0.0.1/testurl: jdbc:mysql://127.0.0.1:3306/test?zeroDateTimeBehaviorconvertToNullserverTimezoneGMT%2b8username: rootpassword: root
# Switch this on to create the schema on startup:initialize: truecontinueOnError: truesleuth:enabled: false
zipkin:storage:type: mysql
123456789101112131415161718
重启服务服务成功后查看数据库自动生成数据库
如此数据库配置完毕所有的spans信息存储到数据库中重启服务也不会丢失记录
4.5 在工程中使用sleuthzipkin Spring Cloud Stream配置
本版本(spring-cloud-sleuth-core-1.2.5.RELEASE)支持集成spring-cloud-sleuth-stream但是在新版本从spring-cloud-sleuth-core-1.3.0.RELEASE开始不支持spring-cloud-sleuth-stream。推荐直接使用通过集成RabbitMQ 或 Kafka。关于集成RabbitMQ 或 Kafka关键是引入spring-rabbit 或 spring-kafka依赖包。默认的目的名称为zipkin. 对于这个的使用demo。这里暂时略
5. 代码
以上的详细的代码见下面 github代码请尽量使用tag v0.11不要使用master因为我不能保证master代码一直不变