赞
踩
微服务架构中,一个由客户端发起的请求在后端系统中会经过多个不同的的服务节点调用来协同产生最后的请求结果,每一个前段请求都会形成一条复杂的分布式服务调用链路,链路中的任何一环出现高延时或错误都会引起整个请求最后的失败。
在大规模分布式与微服务集群下,如何实时观测系统的整体调用链路情况。
在大规模分布式与微服务集群下,如何快速发现并定位到问题。
在大规模分布式与微服务集群下,如何尽可能精确的判断故障对系统的影响范围与影响程度。
在大规模分布式与微服务集群下,如何尽可能精确的梳理出服务之间的依赖关系,并判断出服务之间的依赖关系是否合理。
在大规模分布式与微服务集群下,如何尽可能精确的分析整个系统调用链路的性能与瓶颈点。
在大规模分布式与微服务集群下,如何尽可能精确的分析系统的存储瓶颈与容量规划。
springboot3之前还可以用sleuth,springboot3之后就被Micrometer所替代。
官网:Tracing support :: Micrometer Tracing
源码:https://github.com/spring-cloud/spring-cloud-sleuth
一个分布式链路跟踪系统图形化的工具,Zipkin 是 Twitter 开源的分布式跟踪系统,能够收集微服务运行过程中的实时调用链路信息,并能够将这些调用链路信息展示到Web图形化界面上供开发人员分析。
官网 https://zipkin.io/
If you have Java 17 or higher installed, the quickest way to get started is to fetch the latest release as a self-contained executable jar:
- curl -sSL https://zipkin.io/quickstart.sh | bash -s
- java -jar zipkin.jar
Zipkin服务默认访问地址http://localhost:9411/
主工程pom.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-
- <properties>
- <micrometer-tracing.version>1.2.0</micrometer-tracing.version>
- <micrometer-observation.version>1.12.0</micrometer-observation.version>
- <feign-micrometer.version>12.5</feign-micrometer.version>
- <zipkin-reporter-brave.version>2.17.0</zipkin-reporter-brave.version>
- </properties>
-
- <dependencyManagement>
- <dependencies>
-
- <!--micrometer-tracing一系列包 -->
- <dependency>
- <groupId>io.micrometer</groupId>
- <artifactId>micrometer-tracing-bom</artifactId>
- <version>${micrometer-tracing.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- <dependency>
- <groupId>io.micrometer</groupId>
- <artifactId>micrometer-tracing</artifactId>
- <version>${micrometer-tracing.version}</version>
- </dependency>
- <dependency>
- <groupId>io.micrometer</groupId>
- <artifactId>micrometer-tracing-bridge-brave</artifactId>
- <version>${micrometer-tracing.version}</version>
- </dependency>
- <dependency>
- <groupId>io.micrometer</groupId>
- <artifactId>micrometer-observation</artifactId>
- <version>${micrometer-observation.version}</version>
- </dependency>
- <dependency>
- <groupId>io.github.openfeign</groupId>
- <artifactId>feign-micrometer</artifactId>
- <version>${feign-micrometer.version}</version>
- </dependency>
- <dependency>
- <groupId>io.zipkin.reporter2</groupId>
- <artifactId>zipkin-reporter-brave</artifactId>
- <version>${zipkin-reporter-brave.version}</version>
- </dependency>
- </dependencies>
- </dependencyManagement>
- </project>
提供者pom.xml
- <!--micrometer-tracing指标追踪 1-->
- <dependency>
- <groupId>io.micrometer</groupId>
- <artifactId>micrometer-tracing</artifactId>
- </dependency>
- <!--micrometer-tracing-bridge-brave适配zipkin的桥接包 2-->
- <dependency>
- <groupId>io.micrometer</groupId>
- <artifactId>micrometer-tracing-bridge-brave</artifactId>
- </dependency>
- <!--micrometer-observation 3-->
- <dependency>
- <groupId>io.micrometer</groupId>
- <artifactId>micrometer-observation</artifactId>
- </dependency>
- <!--feign-micrometer 4-->
- <dependency>
- <groupId>io.github.openfeign</groupId>
- <artifactId>feign-micrometer</artifactId>
- </dependency>
- <!--zipkin-reporter-brave 5-->
- <dependency>
- <groupId>io.zipkin.reporter2</groupId>
- <artifactId>zipkin-reporter-brave</artifactId>
- </dependency>
提供者application.yml
- # ========================zipkin===================
- management:
- zipkin:
- tracing:
- endpoint: http://localhost:9411/api/v2/spans
- tracing:
- sampling:
- probability: 1.0 #采样率默认为0.1(0.1就是10次只能有一次被记录下来),值越大收集越及时。
demo调用流程如下:
请求→订单服务myMicrometer→支付服务myMicrometer→订单服务myMicrometer2→支付服务myMicrometer2→结束。
订单服务controller
- @RestController
- @RequestMapping("/feign")
- public class OrderMicrometerController {
-
- @Resource
- private PayFeignApi payFeignApi;
-
- @GetMapping("/pay/micrometer/{id}")
- public String myMicrometer(@PathVariable("id") Integer id) {
-
- try {
- TimeUnit.SECONDS.sleep(1);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- return payFeignApi.myMicrometer(id);
- }
- @GetMapping("/pay/micrometer2/{id}")
- public String myMicrometer2(@PathVariable("id") Integer id) {
- try {
- TimeUnit.SECONDS.sleep(2);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- return payFeignApi.myMicrometer2(id);
-
- }
-
- }
支付服务controller
-
- @RestController
- @RequestMapping("/pay")
- public class PayMicrometerController {
-
- @Resource
- private OrderFeignApi orderFeignApi;
-
-
- @GetMapping("/micrometer/{id}")
- public String myMicrometer(@PathVariable("id") Integer id) {
-
- try {
- TimeUnit.SECONDS.sleep(3);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- return orderFeignApi.myMicrometer(id);
-
- }
-
- @GetMapping("/micrometer2/{id}")
- public String myMicrometer2(@PathVariable("id") Integer id) {
-
- try {
- TimeUnit.SECONDS.sleep(4);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- return "这是链路追踪, Id:" + IdUtil.simpleUUID();
- }
- }
测试链路追踪:http://localhost/feign/pay/micrometer/1
结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。