【SpringCloudAlibaba温故而知新】(五)SpringCloudSleuth。。。

【SpringCloudAlibaba温故⽽知新】(五)SpringCloudSleuth。。。
⽬录
8.1.1 SpringCloud Sleuth 是什么
SpringCloud Sleuth 必知必会
SpringCloud Sleuth 实现的功能是:它会⾃动为当前应⽤构建起各通信通道的跟踪机制
1. 通过诸如 RabbitMQ、Kafka(或者其他任何 SpringCloud Sleuth 绑定器实现的消息中间件)传递的请求
2. 通过 Zuul、Gateway 代理传递的请求
3. 通过 RestTemplate 发起的请求
SpringCloud Sleuth 跟踪实现原理
为了实现请求跟踪:当请求发送到分布式系统的⼊⼝端点时,只需要服务跟踪框架为该请求创建⼀个唯⼀的跟踪标识 Trace ID 为了统计各处理单元的时间延迟,当请求到达各个服务组件时,或是处理逻辑到
达某个状态时,也通过⼀个唯⼀标识来标记它的开始、具体过程以及结束,Span ID
Span ID 如果要计算时间延迟,可以通过 spanIdA11 - spanIdA1
8.1.2 Zipkin 是什么
Zipkin 的基础概念
Zipkin 解决微服务架构中的延迟问题,包括数据的收集、存储、查和展现
Zipkin 有四⼤核⼼组件构成
1. Collector:收集器组件
2. Storge:存储组件
3. API:RESTFul API,提供外部访问接⼝
4. UI:Web UI,提供可视化查询页⾯
8.2.1 集成 SpringCloud Sleuth 实现微服务通信跟踪
8.2.1.1 集成步骤
保证服务与服务之间存在跨进程通信早泄宁
Maven 依赖
8.2.1.2 编写测试代码
sca-commerce-gateway 与 sca-commerce-alibaba-nacos-client 添加 Maven 依赖
<!-- 通过 Sleuth 实现链路跟踪 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
sca-commerce-alibaba-nacos-client 编写测试业务代码与控制层代码SleuthTraceInfoService
package;
import Tracer;
import RequiredArgsConstructor;
import Slf4j;
import Service;
/**
* @author eddie.lee
* @blog blog.eddilee
* @description 使⽤代码更直观的看到 Sleuth ⽣成的相关跟踪信息
*/
商君书锥指@Slf4j
@Service
@RequiredArgsConstructor
public class SleuthTraceInfoService {
/** brave.Tracer 跟踪对象 */
private final Tracer tracer;
/**
* 打印当前的跟踪信息到⽇志中
*/
public void logCurrentTraceInfo(){
全球通家庭计划log.info("Sleuth trace id: [{}]", tracer.currentSpan().context().traceId());
log.info("Sleuth span id: [{}]", tracer.currentSpan().context().spanId());
}
}
SleuthTraceInfoController
package;
import SleuthTraceInfoService;
import RequiredArgsConstructor;
import Slf4j;
import GetMapping;
import RequestMapping;
import RestController;
/**
* @author eddie.lee
* @blog blog.eddilee
* @description 打印跟踪信息
*/
@Slf4j
@RestController
@RequestMapping("/sleuth")
@RequiredArgsConstructor
public class SleuthTraceInfoController {
private final SleuthTraceInfoService traceInfoService;
/**
* 打印⽇志跟踪信息
*/
@GetMapping("/trace-info")
public void logCurrentTraceInfo(){
traceInfoService.logCurrentTraceInfo();
}
}
8.2.1.2 测试请求与查看控制台⽇志
发起请求
### 查看 Sleuth 跟踪信息
GET 127.0.0.1:9001/edcode/scacommerce-nacos-client/sleuth/trace-info
Accept: application/json
sca-commerce-user: JzY2EtY29tbWVyY2UtdXNlciI6IntcImlkXCI6MTEsXCJ1c2VybmFtZVwiOlwiZWRkaWVAcXEuY29tXCJ9Iiwian RpIjoiZjQ3M2NhZjctY2RjMi00ZmE4LWExNzQtZjZhYmQ5ZDFjMzAzIiwiZXhwIjoxNjM1ODY4ODAwfQ.iTtQE2gHzjPxVP5SEFHrDBkvrzI-yt6oy-w1x--Q3ahhT vYLTiYnvndtIx7IIyYipr_ayZnAQyluPt3oiLaS80r9qByaN3zQF-6gBW_wu_fd0yd89hIjPnQeP1mY2NcchV2FaMUW7Jlq8CUDPurEhW4GUDXOqBXgmxai5UTu 4yoXBUfyXUXznKTx697cGo5aoVKTAKvMReJg-77n5sQuafZNDu6pz2D1KMvEucNyZtbXw0JRIl1CsK777Jt3IG1bnOnwRBt8o1tkodZ3zJbfgTGVCHJmfEuUn Xwdf4DLAq568pNVvylPLh4_r-UUGGxE6Az9XwOtl1w4vzK1M2ATzw
token: edcode
响应信息
GET 127.0.0.1:9001/edcode/scacommerce-nacos-client/sleuth/trace-info
HTTP/1.1200 OK
transfer-encoding: chunked
Content-Type: application/json
Date: Tue,02 Nov 202113:04:55 GMT
{
"code":0,高空核爆
"message":"",
"data":null
}
肖洛霍夫Response code:200(OK); Time:1160ms; Content length:35 bytes
查看⽇志
sca-commerce-gateway
2021-11-0221:04:55.332  INFO [sca-commerce-gateway,353ea734cc43d6ee,353ea734cc43d6ee,true]1060---[ctor-http-nio-2]ChainedD ynamicProperty  : Flipping property: sca-commerce-nacos-ActiveConnectionsLimit to NEXT property: niws.loadbalancer.availabilityFilterin gRule.activeConnectionsLimit =2147483647
2021-11-0221:04:55.347  INFO [sca-commerce-gateway,353ea734cc43d6ee,353ea734cc43d6ee,true]1060---[ctor-http-nio-2]Bas eLoadBalancer  : Client: sca-commerce-nacos-client instantiated a LoadBalancer: DynamicServerListLoadBalancer:{NFLoadBalancer:name=sca-commerc e-nacos-client,current list of Servers=[],Load balancer stats=Zone stats:{},Server stats:[]}ServerList:null
2021-11-0221:04:55.353  INFO [sca-commerce-gateway,353ea734cc43d6ee,353ea734cc43d6ee,true]1060---[ctor-http-nio-2]DynamicServerListL oadBalancer      : Using serverListUpdater PollingServerListUpdater
2021-11-0221:04:55.372  INFO [sca-commerce-gateway,353ea734cc43d6ee,353ea734cc43d6ee,true]1060---[ctor-http-nio-2]ChainedD ynamicProp
erty  : Flipping property: sca-commerce-nacos-ActiveConnectionsLimit to NEXT property: niws.loadbalancer.availabilityFilterin gRule.activeConnectionsLimit =2147483647
2021-11-0221:04:55.374  INFO [sca-commerce-gateway,353ea734cc43d6ee,353ea734cc43d6ee,true]1060---[ctor-http-nio-2]DynamicServerListL oadBalancer      : DynamicServerListLoadBalancer for client sca-commerce-nacos-client initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:na me=sca-commerce-nacos-client,current list of Servers=[192.168.3.192:8000],Load balancer stats=Zone stats:{unknown=[Zone:unknown; Instance count:1 ; Active connections count:0; Circuit breaker tripped count:0; Active connections per server:0.0;]
},Server stats:[[Server:192.168.3.192:8000; Zone:UNKNOWN; Total Requests:0; Successive connection failure:0; Total blackout seconds:0; Last connecti on made:Thu Jan 0108:00:00 CST 1970; First connection made: Thu Jan 0108:00:00 CST 1970; Active Connections:0; total failure count in last (1000) m secs:0; average resp time:0.0;90 percentile resp time:0.0;95 percentile resp time:0.0; min resp time:0.0; max resp time:0.0; stddev resp time:0.0]
]}ServerList:NacosServerList@72186c8f
2021-11-0221:04:55.592  INFO [sca-commerce-gateway,353ea734cc43d6ee,353ea734cc43d6ee,true]1060---[ctor-http-nio-2]GlobalElapsed LogFilter      :[/edcode/scacommerce-nacos-client/sleuth/trace-info] elapsed:[1034ms]
2021-11-0221:04:56.358  INFO [sca-commerce-gateway,,,]1060---[erListUpdater-0]ChainedDynamicProperty  : Flipping property: sca-co mmerce-nacos-ActiveConnectionsLimit to NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit =214748364 7
sca-commerce-alibaba-nacos-client
2021-11-0221:04:55.543  INFO [sca-commerce-nacos-client,353ea734cc43d6ee,c85be2c1bb127558,true]33740---[nio-8000-exec-1]Sleut hTraceInfoService    : Sleuth trace id:[3836687777773377262]
2021-11-0221:04:55.543  INFO [sca-commerce-nacos-client,353ea734cc43d6ee,c85be2c1bb127558,true]33740---[nio-8000-exec-1]Sleut hTraceInfoService    : Sleuth span id:[-4009361721548180136]
解析:[sca-commerce-nacos-client,353ea734cc43d6ee,c85be2c1bb127558,true]
第⼀⾏:service name
第⼆⾏:trace id
第三⾏:span id
8.3.1 搭建 Zipkin Server 实现对跟踪信息的收集
8.3.1.1 ZS搭建步骤
Tips:SpringCloud Finchley 版本(包含)之后,官⽅不建议⾃⼰搭建 Zipkin-Server,提供了已经打包好的jar⽂件(SpringBoot ⼯程),直接下载启动即可
下载地址
curl -sSL zipkin.io/quickstart.sh | bash -s
1. 选择⾃⼰需要的版本即可
2. 选择 *.exec.jar 结尾的 jar
8.3.1.2 Linux 终端
[root@localhost opt]# curl -sSL zipkin.io/quickstart.sh | bash -s
Thank you for trying Zipkin!
This installer is provided as a quick-start helper, so you can try Zipkin out
without a lengthy installation process.
Fetching version number of latest io.zipkin:
Latest release of io.zipkin:zipkin-server seems to be 2.23.4
Downloading io.zipkin:zipkin-server:2.23.4:exec to
>curl -fL -o 'zipkin.jar''/maven2/io/zipkin/zipkin-server/2.23.4/zipkin-server-2.23.4-exec.jar'
% Total    % Received % Xferd  Average Speed  Time    Time    Time  Current
Dload  Upload  Total  Spent    Left  Speed
10059.0M  10059.0M    003414600:30:14  0:30:14 --:--:-- 33309
>curl -fL -o 'zipkin.jar.md5''/maven2/io/zipkin/zipkin-server/2.23.4/zipkin-server-2.23.4-exec.jar.md5'  % Total    % Received % Xferd  Average Speed  Time    Time    Time  Current
Dload  Upload  Total  Spent    Left  Speed
1003210032001700:00:01  0:00:01 --:--:--    17
> md5sum -c <<<"$(cat zipkin.jar.md5)  zipkin.jar"
zipkin.jar: OK
Checksum for zipkin.jar passes verification
Verifying GPG signature of
>curl -fL -o 'zipkin.jar.asc''/maven2/io/zipkin/zipkin-server/2.23.4/zipkin-server-2.23.4-exec.jar.asc'  % Total    % Received % Xferd  Average Speed  Time    Time    Time  Current
Dload  Upload  Total  Spent    Left  Speed
100833100833009100:00:09  0:00:09 --:--:--  180
GPG signing key is not known, skipping signature verification.
Use the following commands to manually verify the signature of zipkin.jar:
gpg --keyserver keyserver.ubuntu --recv FF31B515
# Optionally trust the key via 'gpg --edit-key FF31B515', then typing 'trust',
个人图书馆# choosing a trust level, and exiting the interactive GPG session by 'quit'
gpg --verify zipkin.jar.asc zipkin.jar
You can now run the downloaded executable jar:
java -jar zipkin.jar
[root@localhost opt]# nohup java -jar zipkin.jar &
[1]30238
[root@localhost opt]# nohup: ignoring input and appending output to ‘nohup.out’
8.3.1.3 Zipkin Web UI

本文发布于:2024-09-22 07:39:29,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/121525.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:跟踪   请求   服务   实现   信息   代码   提供   组件
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议