Spring Cloud Sleuth 整合 Zipkin

偏执的太偏执、 2022-05-25 03:43 329阅读 0赞

Spring Cloud Sleuth 是分布式服务跟踪,Zipkin可以帮助收集时间数据,解决在microservice架构下的延迟问题。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.example</groupId>
  5. <artifactId>server-zipkin</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <packaging>jar</packaging>
  8. <name>server-zipkin</name>
  9. <description>Demo project for Spring Cloud</description>
  10. <parent>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-parent</artifactId>
  13. <version>1.5.12.RELEASE</version>
  14. <relativePath/> <!-- lookup parent from repository -->
  15. </parent>
  16. <properties>
  17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  19. <java.version>1.8</java.version>
  20. <spring-cloud.version>Edgware.SR3</spring-cloud.version>
  21. </properties>
  22. <dependencies>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-starter-web</artifactId>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.springframework.boot</groupId>
  33. <artifactId>spring-boot-starter-test</artifactId>
  34. <scope>test</scope>
  35. </dependency>
  36. <dependency>
  37. <groupId>io.zipkin.java</groupId>
  38. <artifactId>zipkin-server</artifactId>
  39. </dependency>
  40. <dependency>
  41. <groupId>io.zipkin.java</groupId>
  42. <artifactId>zipkin-autoconfigure-ui</artifactId>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.springframework.cloud</groupId>
  46. <artifactId>spring-cloud-starter-eureka</artifactId>
  47. </dependency>
  48. <dependency>
  49. <groupId>org.springframework.boot</groupId>
  50. <artifactId>spring-boot-starter-jdbc</artifactId>
  51. </dependency>
  52. <dependency>
  53. <groupId>mysql</groupId>
  54. <artifactId>mysql-connector-java</artifactId>
  55. </dependency>
  56. <dependency>
  57. <groupId>io.zipkin.java</groupId>
  58. <artifactId>zipkin-autoconfigure-storage-mysql</artifactId>
  59. </dependency>
  60. </dependencies>
  61. <dependencyManagement>
  62. <dependencies>
  63. <dependency>
  64. <groupId>org.springframework.cloud</groupId>
  65. <artifactId>spring-cloud-dependencies</artifactId>
  66. <version>${spring-cloud.version}</version>
  67. <type>pom</type>
  68. <scope>import</scope>
  69. </dependency>
  70. </dependencies>
  71. </dependencyManagement>
  72. <build>
  73. <plugins>
  74. <plugin>
  75. <groupId>org.springframework.boot</groupId>
  76. <artifactId>spring-boot-maven-plugin</artifactId>
  77. </plugin>
  78. </plugins>
  79. </build>
  80. </project>

添加 @EnableZipkinServer

  1. @EnableZipkinServer
  2. @EnableEurekaClient
  3. @SpringBootApplication
  4. public class ServerZipkinApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(ServerZipkinApplication.class, args);
  7. }
  8. }

zipkin server 默认将跟踪信息存储在内存中,每次重启都会丢失之前的跟踪信息,可以修改为mysql存储

application.yml

  1. # 修改为mysql存储:
  2. # 1.添加依赖 <dependency>
  3. # <groupId>org.springframework.boot</groupId>
  4. # <artifactId>spring-boot-starter-jdbc</artifactId>
  5. # </dependency>
  6. # <dependency>
  7. # <groupId>mysql</groupId>
  8. # <artifactId>mysql-connector-java</artifactId>
  9. # </dependency>
  10. # <dependency>
  11. # <groupId>io.zipkin.java</groupId>
  12. # <artifactId>zipkin-autoconfigure-storage-mysql</artifactId>
  13. # </dependency>
  14. #
  15. # 2.配置 spring.datasource
  16. # 3.配置 zipkin.storage.type=mysql
  17. spring:
  18. datasource:
  19. # zipkin-autoconfigure-storage-mysql 包下有 mysql.sql 脚本,程序启动时会自动根据sql脚本创建表结构
  20. schema: classpath:/mysql.sql
  21. url: jdbc:mysql://localhost:3306/qinwei?useUnicode=true&characterEncoding=utf-8
  22. username: root
  23. password: 123456
  24. initialize: true

application.properties

  1. server.port=8090
  2. spring.application.name=server-zipkin
  3. eureka.client.service-url.defaultZone=http://localhost:8081/eureka-server/eureka/
  4. management.security.enabled=false
  5. #将zipkin server 存储切换为 mysql
  6. zipkin.storage.type=mysql

在需要跟踪的服务中添加依赖

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-zipkin</artifactId>
  4. </dependency>

在配置文件中指定Zipkin Server的地址

application.properties

  1. spring.zipkin.base-url=http://localhost:8090

创建 bean

  1. @Bean
  2. public RestTemplate getRestTemplate() {
  3. return new RestTemplate();
  4. }
  5. /** * sleuth 默认使用 PercentageBasedSampler 实现的抽样策略,以请求百分比的方式配置和收集跟踪信息. * 默认是0.1(表示收集10%的跟踪信息),可以配置spring.sleuth.sampler.percentage来修改. * <p> * 创建 AlwaysSampler 的bean,会覆盖默认的 PercentageBasedSampler 策略,收集全部的跟踪信息 */
  6. @Bean
  7. public AlwaysSampler defaultSampler() {
  8. return new AlwaysSampler();
  9. }

这里写图片描述

这里写图片描述

这里写图片描述

发表评论

表情:
评论列表 (有 0 条评论,329人围观)

还没有评论,来说两句吧...

相关阅读

    相关 26.Spring Cloud SleuthZipkin

    [Zipkin][]是一种分布式跟踪系统。 它有助于收集解决微服务架构中的延迟问题所需的时序数据。 它管理这些数据的收集和查找。 Zipkin的设计基于Google Dappe