当前位置:   article > 正文

springcloud第4季 使用micrometer+zipkin实现链路追踪5_spring cloud zipkin micrometer

spring cloud zipkin micrometer

链路追踪

1.1 链路追踪概念

链路追踪是在微服务中,一个请求经过的节点服务,每个服务的耗时,执行状态的完美展现。

其中使用micrometer

1.2 链路追踪的原理

通过traceid标记整个请求的唯一性;通过spanid标记本节点要发送的内容,通过parentid记录上一个节点。

1.3 ZIPkin的介绍

zipkin是一种分布式链路跟踪系统图形化工具。开发人员能够从zipkin中分析出调用链路中的性能瓶颈,识别出存在问题的应用程序,进而定位问题和解决问题。

二  micrometer+zipkin实现链路追踪

2.1 zipkin服务启动

1.程序包

2.服务启动:java -jar zipkin-server-2.23.4-exec.jar

3.页面查看:http://localhost:9411/zipkin/

2.2 配置监控的项目

2.2.1 父工程声明依赖版本

  1. <micrometer-tracing.version>1.2.0</micrometer-tracing.version>
  2. <micrometer-observation.version>1.12.0</micrometer-observation.version>
  3. <feign-micrometer.version>12.5</feign-micrometer.version>
  4. <zipkin-reporter-brave.version>2.17.0</zipkin-reporter-brave.version>
  5. <!--micrometer-tracing-bom导入链路追踪版本中心 1-->
  6. <dependency>
  7. <groupId>io.micrometer</groupId>
  8. <artifactId>micrometer-tracing-bom</artifactId>
  9. <version>${micrometer-tracing.version}</version>
  10. <type>pom</type>
  11. <scope>import</scope>
  12. </dependency>
  13. <!--micrometer-tracing指标追踪 2-->
  14. <dependency>
  15. <groupId>io.micrometer</groupId>
  16. <artifactId>micrometer-tracing</artifactId>
  17. <version>${micrometer-tracing.version}</version>
  18. </dependency>
  19. <!--micrometer-tracing-bridge-brave适配zipkin的桥接包 3-->
  20. <dependency>
  21. <groupId>io.micrometer</groupId>
  22. <artifactId>micrometer-tracing-bridge-brave</artifactId>
  23. <version>${micrometer-tracing.version}</version>
  24. </dependency>
  25. <!--micrometer-observation 4-->
  26. <dependency>
  27. <groupId>io.micrometer</groupId>
  28. <artifactId>micrometer-observation</artifactId>
  29. <version>${micrometer-observation.version}</version>
  30. </dependency>
  31. <!--feign-micrometer 5-->
  32. <dependency>
  33. <groupId>io.github.openfeign</groupId>
  34. <artifactId>feign-micrometer</artifactId>
  35. <version>${feign-micrometer.version}</version>
  36. </dependency>
  37. <!--zipkin-reporter-brave 6-->
  38. <dependency>
  39. <groupId>io.zipkin.reporter2</groupId>
  40. <artifactId>zipkin-reporter-brave</artifactId>
  41. <version>${zipkin-reporter-brave.version}</version>
  42. </dependency>

2.2.2 提供者配置

2.2.2.1 pom依赖添加
  1. <!--micrometer-tracing指标追踪 1-->
  2. <dependency>
  3. <groupId>io.micrometer</groupId>
  4. <artifactId>micrometer-tracing</artifactId>
  5. </dependency>
  6. <!--micrometer-tracing-bridge-brave适配zipkin的桥接包 2-->
  7. <dependency>
  8. <groupId>io.micrometer</groupId>
  9. <artifactId>micrometer-tracing-bridge-brave</artifactId>
  10. </dependency>
  11. <!--micrometer-observation 3-->
  12. <dependency>
  13. <groupId>io.micrometer</groupId>
  14. <artifactId>micrometer-observation</artifactId>
  15. </dependency>
  16. <!--feign-micrometer 4-->
  17. <dependency>
  18. <groupId>io.github.openfeign</groupId>
  19. <artifactId>feign-micrometer</artifactId>
  20. </dependency>
  21. <!--zipkin-reporter-brave 5-->
  22. <dependency>
  23. <groupId>io.zipkin.reporter2</groupId>
  24. <artifactId>zipkin-reporter-brave</artifactId>
  25. </dependency>
2.2.2.2 配置文件配置

1.配置图

2.配置内容 

  1. # ========================zipkin===================
  2. management:
  3. zipkin:
  4. tracing:
  5. endpoint: http://localhost:9411/api/v2/spans
  6. tracing:
  7. sampling:
  8. probability: 1.0 #采样率默认为0.1(0.1就是10次只能有一次被记录下来),值越大收集越及时。
 2.2.2.3 业务类
  1. package com.jurf.ms.provider9091.controller;
  2. import cn.hutool.core.util.IdUtil;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.PathVariable;
  5. import org.springframework.web.bind.annotation.RestController;
  6. /**
  7. * @auther zzyy
  8. * @create 2023-12-28 16:08
  9. */
  10. @RestController
  11. public class PayMicrometerController
  12. {
  13. /**
  14. * Micrometer(Sleuth)进行链路监控的例子
  15. * @param id
  16. * @return
  17. */
  18. @GetMapping(value = "/pay/micrometer/{id}")
  19. public String myMicrometer(@PathVariable("id") Integer id)
  20. {
  21. return "Hello, 欢迎到来myMicrometer inputId: "+id+" \t 服务返回:" + IdUtil.simpleUUID();
  22. }
  23. }

2.2.3 openfegin的接口api

2.2.4 消费者配置

2.2.4.1 pom依赖添加
  1. <dependencies>
  2. <!--micrometer-tracing指标追踪 1-->
  3. <dependency>
  4. <groupId>io.micrometer</groupId>
  5. <artifactId>micrometer-tracing</artifactId>
  6. </dependency>
  7. <!--micrometer-tracing-bridge-brave适配zipkin的桥接包 2-->
  8. <dependency>
  9. <groupId>io.micrometer</groupId>
  10. <artifactId>micrometer-tracing-bridge-brave</artifactId>
  11. </dependency>
  12. <!--micrometer-observation 3-->
  13. <dependency>
  14. <groupId>io.micrometer</groupId>
  15. <artifactId>micrometer-observation</artifactId>
  16. </dependency>
  17. <!--feign-micrometer 4-->
  18. <dependency>
  19. <groupId>io.github.openfeign</groupId>
  20. <artifactId>feign-micrometer</artifactId>
  21. </dependency>
  22. <!--zipkin-reporter-brave 5-->
  23. <dependency>
  24. <groupId>io.zipkin.reporter2</groupId>
  25. <artifactId>zipkin-reporter-brave</artifactId>
  26. </dependency>
2.2.4.2 配置文件配置
  1. # zipkin图形展现地址和采样率设置
  2. management:
  3. zipkin:
  4. tracing:
  5. endpoint: http://localhost:9411/api/v2/spans
  6. tracing:
  7. sampling:
  8. probability: 1.0 #采样率默认为0.1(0.1就是10次只能有一次被记录下来),值越大收集越及时。
2.2.4.3 业务类配置
  1. package com.jurf.ms.fegin.controller;
  2. import com.jurf.ms.api.sb.PayFeginApi;
  3. import jakarta.annotation.Resource;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.PathVariable;
  7. import org.springframework.web.bind.annotation.RestController;
  8. /**
  9. * @auther zzyy
  10. * @create 2023-12-28 16:11
  11. */
  12. @RestController
  13. @Slf4j
  14. public class OrderMicrometerController
  15. {
  16. @Resource
  17. private PayFeginApi payFeginApi;
  18. @GetMapping(value = "/feign/micrometer/{id}")
  19. public String myMicrometer(@PathVariable("id") Integer id)
  20. {
  21. return payFeginApi.myMicrometer(id);
  22. }
  23. }

2.3 项目测试

2.3.1.启动zipkin

服务启动:java -jar zipkin-server-2.23.4-exec.jar

2.3.2启动consul

2.3.3.启动消费,提供者服务

2.3.4.访问验证

http://localhost:8081/feign/micrometer/666

查看zipkin

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/695895
推荐阅读
相关标签
  

闽ICP备14008679号