当前位置:   article > 正文

Cloud-Sleuth分布式链路追踪(服务跟踪)

Cloud-Sleuth分布式链路追踪(服务跟踪)

简介

在微服务框架中,一个由客户端发起的请求在后端系统中会经过多个不同的服务节点调用来协同产生最后的请求结果,每一个前端请求都会形成一条复杂的分布式服务调用链路,链路中的任何一环出现高延时或错误都会引起整个请求最后的失败

GitHub - spring-cloud/spring-cloud-sleuth: Distributed tracing for spring cloudDistributed tracing for spring cloud. Contribute to spring-cloud/spring-cloud-sleuth development by creating an account on GitHub.icon-default.png?t=N7T8https://github.com/spring-cloud/spring-cloud-sleuthSpring Cloud Sleuth提供了一套完整的服务跟踪的解决方案

在分布式系统中提供追踪解决方案并且兼容支持了zipkin(管展现)     Cloud-Sleuth(管监控)

这么说吧:Cloud把zipkin收编了  然后取名Sleuth

Cloud构建

pom.xml

  1. <!-- 统一管理jar包版本 -->
  2. <properties>
  3. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  4. <maven.compiler.source>1.8</maven.compiler.source>
  5. <maven.compiler.target>1.8</maven.compiler.target>
  6. <junit.version>4.12</junit.version>
  7. <log4j.version>1.2.17</log4j.version>
  8. <lombok.version>1.16.18</lombok.version>
  9. </properties>
  10. <!-- 子模块继承之后,提供作用:锁定版本+子modlue不用写groupId和version -->
  11. <dependencyManagement>
  12. <dependencies>
  13. <!--spring boot 2.2.2-->
  14. <dependency>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-dependencies</artifactId>
  17. <version>2.2.2.RELEASE</version>
  18. <type>pom</type>
  19. <scope>import</scope>
  20. </dependency>
  21. <!--spring cloud Hoxton.SR1-->
  22. <dependency>
  23. <groupId>org.springframework.cloud</groupId>
  24. <artifactId>spring-cloud-dependencies</artifactId>
  25. <version>Hoxton.SR1</version>
  26. <type>pom</type>
  27. <scope>import</scope>
  28. </dependency>
  29. <!--spring cloud alibaba 2.1.0.RELEASE-->
  30. <dependency>
  31. <groupId>com.alibaba.cloud</groupId>
  32. <artifactId>spring-cloud-alibaba-dependencies</artifactId>
  33. <version>2.1.0.RELEASE</version>
  34. <type>pom</type>
  35. <scope>import</scope>
  36. </dependency>
  37. <dependency>
  38. <groupId>junit</groupId>
  39. <artifactId>junit</artifactId>
  40. <version>${junit.version}</version>
  41. </dependency>
  42. <dependency>
  43. <groupId>log4j</groupId>
  44. <artifactId>log4j</artifactId>
  45. <version>${log4j.version}</version>
  46. </dependency>
  47. <dependency>
  48. <groupId>org.projectlombok</groupId>
  49. <artifactId>lombok</artifactId>
  50. <version>${lombok.version}</version>
  51. <optional>true</optional>
  52. </dependency>
  53. </dependencies>
  54. </dependencyManagement>
  55. <build>
  56. <finalName>cloud</finalName>
  57. <plugins>
  58. <plugin>
  59. <groupId>org.springframework.boot</groupId>
  60. <artifactId>spring-boot-maven-plugin</artifactId>
  61. <version>2.2.2.RELEASE</version>
  62. <configuration>
  63. <fork>true</fork>
  64. <addResources>true</addResources>
  65. </configuration>
  66. </plugin>
  67. </plugins>
  68. </build>

搭建链路监控

SpringCloud从F版起已不需要自己构建Zipkin server了,只需要调用jar包即可

Releases · openzipkin/zipkin · GitHub

我这边用的

Release 2.12.9 · openzipkin/zipkin · GitHub

jar  maven仓库下载

Central Repository: io/zipkin/zipkin-servericon-default.png?t=N7T8https://repo1.maven.org/maven2/io/zipkin/zipkin-server/

java -jar zipkin-server-2.12.9-exec.jar

localhost:9411/

搭建生产者模块

pom.xml

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-zipkin</artifactId>
  5. </dependency>
  6. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-web</artifactId>
  10. </dependency>
  11. <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-zookeeper-discovery -->
  12. <dependency>
  13. <groupId>org.springframework.cloud</groupId>
  14. <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
  15. </dependency>
  16. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-devtools</artifactId>
  20. <scope>runtime</scope>
  21. <optional>true</optional>
  22. </dependency>
  23. <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
  24. <dependency>
  25. <groupId>org.projectlombok</groupId>
  26. <artifactId>lombok</artifactId>
  27. <optional>true</optional>
  28. </dependency>
  29. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter-test</artifactId>
  33. <scope>test</scope>
  34. </dependency>
  35. </dependencies>

application.yml

  1. server:
  2. port: 8081
  3. spring:
  4. application:
  5. name: cloud-provider-payment
  6. zipkin:
  7. base-url: http://localhost:9411
  8. sleuth:
  9. sampler:
  10. #采样率值介于01之间,1则表示全部采集
  11. probability: 1
  12. cloud:
  13. zookeeper:
  14. connect-string: 222.22.2.2:2181

启动类

业务类

构建消费者模块

pom.xml

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-zipkin</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.cloud</groupId>
  8. <artifactId>spring-cloud-starter-openfeign</artifactId>
  9. </dependency>
  10. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
  11. <dependency>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-web</artifactId>
  14. </dependency>
  15. <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-zookeeper-discovery -->
  16. <dependency>
  17. <groupId>org.springframework.cloud</groupId>
  18. <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
  19. </dependency>
  20. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-devtools</artifactId>
  24. <scope>runtime</scope>
  25. <optional>true</optional>
  26. </dependency>
  27. <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
  28. <dependency>
  29. <groupId>org.projectlombok</groupId>
  30. <artifactId>lombok</artifactId>
  31. <optional>true</optional>
  32. </dependency>
  33. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
  34. <dependency>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-starter-test</artifactId>
  37. <scope>test</scope>
  38. </dependency>
  39. </dependencies>

application.yml

  1. server:
  2. port: 80
  3. spring:
  4. application:
  5. name: cloud-consumer
  6. zipkin:
  7. base-url: http://localhost:9411
  8. sleuth:
  9. sampler:
  10. #采样率值介于01之间,1则表示全部采集
  11. probability: 1
  12. cloud:
  13. zookeeper:
  14. connect-string: 222.222.222.2:2181

启动类

业务类

多点几下

慢慢玩

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

闽ICP备14008679号