1 微服务架构下的问题

  • 在大型系统的微服务化构建中,一个系统会被拆分成许多模块。这些模块负责不同的功能,组合成系统,最终可以提供丰富的功能。在这种架构中,一次请求往往需要涉及到多个服务。互联网应用构建在不同的软件模块集上,这些软件模块,可能是由不同团队开发、可能使用不同的编程语言来实现、可能部署在几千台服务器上,横跨多个不同的数据中心,也就意味着这种架构形式也会存在一些问题:
    • 微服务的链路追踪Zipkin(不推荐) - 图1如何快速的发现问题?
    • 微服务的链路追踪Zipkin(不推荐) - 图2如何判断故障影响范围?
    • 微服务的链路追踪Zipkin(不推荐) - 图3如何梳理服务依赖以及依赖的合理性?
    • 微服务的链路追踪Zipkin(不推荐) - 图4如何分析链路性能问题以及实时容量规划?
  • 分布式链路追踪(Distributed Tracing),就是将一次分布式请求还原成调用链路,进行日志记录,性能监控并将一次分布式请求的调用情况集中展示。比如各个服务节点上的耗时、请求具体到达那台机器上、每个服务节点的请求状态等等。
  • 目前业界比较流行的链路追踪系统如:Twitter的Zipkin,阿里的鹰眼,美团的Mtrace,大众点评的cat等,大部分都是基于Google发表的Dapper。Dapper阐述了分布式系统,特别是微服务架构中链路追踪的概念、数据展示、埋点、传递、收集、存储和展示等技术细节。

2 Sleuth概述

2.1 简介

  • Spring Cloud Sleuth主要功能就是在分布式系统中提供追踪解决方案,并且兼容支持了zipkin,只需要在pom文件中引入相应的依赖即可。

2.2 相关概念

  • Spring Cloud Sleuth为Spring Cloud提供了分布式追踪解决方案。它大量借用了Google的Dapper的设计。需要先了解一下Sleuth中的术语和相关概念。
  • Spring Cloud Sleuth采用的是Google的开源项目Dapper的专业术语。
  • Span:基本工作单元,例如:在一个新建的span中发送一个RPC等同于发送一次回应请求给RPC,span通过一个64位ID唯一标识,trace以另一个64位ID标识,span还有其他数据信息,比如摘要、时间戳事件、关键值注释(tags)、span的ID以及进度ID(通常是IP地址),span在不断的启动和停止的同时记录了时间信息,当你创建了一个span,你必须在未来的某个时刻停止它。
  • Trace:一系列span组成的一个树状结构。例如,当你正在跑一个分布式大数据工程,你可能需要创建Trace。
  • Annotation:用来及时记录一个事件的存在,一些核心annotations用来定义一个请求的开始和结束。
    • cs-Client Server:客户端发送一个请求,这个annotation描述了这个span的开始。
    • sr-Server Received:服务端获得请求并准备开始处理它,如果将其srj减去cs时间戳便可得到网络延迟。
    • ss-Server Sent:注解表明请求处理的完成(当请求返回客户端),如果ss减去sr时间戳便可得到服务端需要的处理请求时间。
    • cr-Client Received:表明span的结束,客户端成功接收到服务端的回复,如果cr减去cs时间戳便可得到客户端从服务端获取回复的所有所需时间。

spring-cloud-sleuth.jpg

3 链路追踪Sleuth入门

3.1 在网关层、订单微服务、商品微服务导入Sleuth的依赖

  • 修改部分:
  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-sleuth</artifactId>
  4. </dependency>
  • 网关层的完整pom:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://maven.apache.org/POM/4.0.0"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>spring_cloud_demo</artifactId>
  7. <groupId>org.sunxiaping</groupId>
  8. <version>1.0</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>api_gateway_server7007</artifactId>
  12. <dependencies>
  13. <!--
  14. Spring Cloud Gateway使用的web框架是webflux,和SpringMVC不兼容。引入的限流组件是Hystrix。Redis底层不再使用jedis,而是lettuce。
  15. -->
  16. <dependency>
  17. <groupId>org.springframework.cloud</groupId>
  18. <artifactId>spring-cloud-starter-gateway</artifactId>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.springframework.cloud</groupId>
  22. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.cloud</groupId>
  26. <artifactId>spring-cloud-starter-sleuth</artifactId>
  27. </dependency>
  28. </dependencies>
  29. </project>
  • 订单微服务的完整pom:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://maven.apache.org/POM/4.0.0"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>spring_cloud_demo</artifactId>
  7. <groupId>org.sunxiaping</groupId>
  8. <version>1.0</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>order_service8003</artifactId>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework.cloud</groupId>
  15. <artifactId>spring-cloud-starter-openfeign</artifactId>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-web</artifactId>
  20. </dependency>
  21. <!-- 导入Eureka Client对应的坐标 -->
  22. <dependency>
  23. <groupId>org.springframework.cloud</groupId>
  24. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-actuator</artifactId>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework.cloud</groupId>
  32. <artifactId>spring-cloud-starter-sleuth</artifactId>
  33. </dependency>
  34. </dependencies>
  35. </project>
  • 商品微服务的完整pom:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://maven.apache.org/POM/4.0.0"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>spring_cloud_demo</artifactId>
  7. <groupId>org.sunxiaping</groupId>
  8. <version>1.0</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>product_service9004</artifactId>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-web</artifactId>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-data-jpa</artifactId>
  20. </dependency>
  21. <dependency>
  22. <groupId>mysql</groupId>
  23. <artifactId>mysql-connector-java</artifactId>
  24. </dependency>
  25. <!-- 导入Eureka Client对应的坐标 -->
  26. <dependency>
  27. <groupId>org.springframework.cloud</groupId>
  28. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter-actuator</artifactId>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework.cloud</groupId>
  36. <artifactId>spring-cloud-starter-sleuth</artifactId>
  37. </dependency>
  38. </dependencies>
  39. </project>

3.2 修改网关层、订单微服务、商品微服务的配置文件

  • 修改部分:
  1. logging:
  2. level:
  3. root: INFO
  4. org.springframework.web.servlet.DispatcherServlet: DEBUG
  5. org.springframework.cloud.sleuth: DEBUG
  • 网关层的完整application.yml:
  1. server:
  2. port: 7007
  3. spring:
  4. application:
  5. name: api-gateway-server
  6. # 配置 Spring Cloud Gateway
  7. cloud:
  8. gateway:
  9. discovery:
  10. locator:
  11. enabled: true # 开启从注册中心动态创建路由的功能,利用微服务名进行路由
  12. lower-case-service-id: true # 微服务名称以小写形式呈现
  13. routes:
  14. # 配置路由: 路由id,路由到微服务的uri,断言(判断条件)
  15. - id: product-service # 路由id
  16. # uri: http://localhost:9004
  17. uri: lb://service-product # 路由到微服务的uri。 lb://xxx,lb代表从注册中心获取服务列表,xxx代表需要转发的微服务的名称
  18. predicates: # 断言(判断条件)
  19. # - Path=/product/**
  20. - Path=/product-service/**
  21. filters: # 配置路由过滤器 http://localhost:7007/product-service/product/findById/1 --> http://localhost:7007/product/findById/1
  22. - RewritePath=/product-service/(?<segment>.*), /$\{segment} # 路径重写的过滤器
  23. # 配置 eureka
  24. eureka:
  25. instance:
  26. # 主机名称:服务名称修改,其实就是向eureka server中注册的实例id
  27. instance-id: api-gateway-server:${server.port}
  28. # 显示IP信息
  29. prefer-ip-address: true
  30. client:
  31. service-url: # 此处修改为 Eureka Server的集群地址
  32. defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  33. logging:
  34. level:
  35. root: INFO
  36. org.springframework.web.servlet.DispatcherServlet: DEBUG
  37. org.springframework.cloud.sleuth: DEBUG
  38. org.springframework.cloud.gateway: trace
  39. org.springframework.http.server.reactive: debug
  40. org.springframework.web.reactive: debug
  41. reactor.ipc.netty: debug
  • 订单微服务的完整application.yml:
  1. server:
  2. port: 8003 # 微服务的端口号
  3. spring:
  4. application:
  5. name: service-order # 微服务的名称
  6. # 配置 eureka
  7. eureka:
  8. instance:
  9. # 主机名称:服务名称修改,其实就是向eureka server中注册的实例id
  10. instance-id: service-order:${server.port}
  11. # 显示IP信息
  12. prefer-ip-address: true
  13. client:
  14. service-url: # 此处修改为 Eureka Server的集群地址
  15. defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  16. feign:
  17. hystrix: # 开启Feign中的Hystrix
  18. enabled: true
  19. # 暴露所有端点
  20. management:
  21. endpoints:
  22. web:
  23. exposure:
  24. include: '*'
  25. hystrix:
  26. command:
  27. default:
  28. execution:
  29. isolation:
  30. thread:
  31. timeoutInMilliseconds: 3000 # 默认的连接超时时间为1秒,如果1秒没有返回数据,就自动触发降级逻辑
  32. # 微服务info内容详细信息
  33. info:
  34. app.name: xxx
  35. company.name: xxx
  36. build.artifactId: $project.artifactId$
  37. build.version: $project.version$
  38. logging:
  39. level:
  40. root: INFO
  41. org.springframework.web.servlet.DispatcherServlet: DEBUG
  42. org.springframework.cloud.sleuth: DEBUG
  43. org.springframework.cloud.gateway: trace
  44. org.springframework.http.server.reactive: debug
  45. org.springframework.web.reactive: debug
  46. reactor.ipc.netty: debug
  • 商品微服务的完整application.yml:
  1. server:
  2. port: 9004 # 微服务的端口号
  3. spring:
  4. application:
  5. name: service-product # 微服务的名称
  6. datasource:
  7. url: jdbc:mysql://192.168.217.100:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
  8. driver-class-name: com.mysql.cj.jdbc.Driver
  9. username: root
  10. password: 123456
  11. jpa:
  12. generate-ddl: true
  13. show-sql: true
  14. open-in-view: true
  15. database: mysql
  16. # 配置 eureka
  17. eureka:
  18. instance:
  19. # 主机名称:服务名称修改,其实就是向eureka server中注册的实例id
  20. instance-id: service-product:${server.port}
  21. # 显示IP信息
  22. prefer-ip-address: true
  23. client:
  24. service-url: # 此处修改为 Eureka Server的集群地址
  25. defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  26. logging:
  27. level:
  28. root: INFO
  29. org.springframework.web.servlet.DispatcherServlet: DEBUG
  30. org.springframework.cloud.sleuth: DEBUG
  31. org.springframework.cloud.gateway: trace
  32. org.springframework.http.server.reactive: debug
  33. org.springframework.web.reactive: debug
  34. reactor.ipc.netty: debug
  35. # 微服务info内容详细信息
  36. info:
  37. app.name: xxx
  38. company.name: xxx
  39. build.artifactId: $project.artifactId$
  40. build.version: $project.version$

3.3 重启网关层、订单微服务、商品微服务

  • 重启之后,我们可以在控制台观察到Sleuth的日志输出。

在各个微服务加入Sleuth的日志.png

  • 其中,81a807d076c2a9f6是TraceId,后面跟着的是SpanId,依次调用有一个全局的TranceId,将调用链路串起来。仔细分析每个微服务的日志,不难看出请求的具体过程。
  • 查看日志文件并不是一个很好的方法,当微服务越来越多日志文件也会越来越多,通过ZipKin可以将日志聚合,并进行可视化展示和全文检索。

4 ZipKin概述

  • Zipkin是Twitter的一个开源项目,它基于Google Dapper实现,它致力于收集服务的定时数据,以解决微服务架构中的延迟问题,包括数据的收集、存储、查找和展现。我们可以使用它来收集各个服务器上请求链路的跟踪数据,并通过它提供的REST API接口来辅助我们查询跟踪以实现分布式系统的监控程序,从而及时的发现系统中出现的延迟升高的问题并找出系统性能瓶颈的根源。除了面向开发的API接口之外,它也提供了方便的UI组件来帮助我们直观的搜索跟踪信息和分析请求链路明细,比如:可以查询某段时间内各用户请求的处理时间等。Zipkin提供了可插拔数据存储方式:In-Memory、MySQL、Cassandra以及ElasticSearch。

zipkin.jpg

  • 上图展示了Zipkin的基础架构,它主要由4个核心组件构成:
    • Collector:收集器组件,它主要用于处理从外部系统发送过来的跟踪信息,将这些信息转换为Zipkin内部处理的span格式,以支持后续的存储、分析、展示等功能。
    • Storage:存储组件,它主要对处理收集器接收到的跟踪信息,默认会将这些信息存储在内存中,我们也可以修改此存储策略,将跟踪信息存储到数据库中。
    • Restful API:API组件,它主要用来提供外部访问接口。比如给客户端展示跟踪信息,或者外接系统访问以实现监控等。
    • Web UI:UI组件,基于API组件实现的上层应用。通过UI组件用户可以方便而直观的查询和分析跟踪信息。
  • Zipkin分为两端,一个是Zipkin服务端,一个是Zipkin客户端,客户端也就是微服务的应用。客户端会配置服务端的URL地址,一旦发生服务间的调用时候,会被配置在微服务里面的Sleuth的监听器监听,并生成相应的Trace和Span信息发送给服务端。
  • 发送的方式有两种:一种是HTTP报文的方式,另外一种是消息总线的方式如RabbitMQ。
  • 不论哪种方式,我们都需要:
    • 一个Eureka服务注册中心。
    • 一个Zipkin服务端。
    • 多个微服务,这些微服务中配置了Zipkin客户端。

5 Zipkin Server的部署和配置

5.1 Zipkin Server下载

  • 从SpringBoot2.0开始,官方就不再支持使用自建Zipkin Server的方式进行服务链路追踪,而是直接提供了编译好的jar包来给我们使用。可以从官方网站上下载Zipkin的Web UI

5.2 启动

  • 在命令行输入java -jar zipkin-server-2.12.9-exec.jar启动Zipkin Server。

命令行启动Zipkin.png

5.3 使用Docker启动Zipkin Server

  1. docker run -d -p 9411:9411 --name zipkin openzipkin/zipkin:2.12.9

6 客户端Zipkin和Sleuth整合

6.1 概述

  • 通过查看日志分析微服务的调用链路并不是一个很直观的方案,结合Zipkin可以很直观的显示微服务之间的调用关系。

6.2 客户端Zipkin和Sleuth整合

6.2.1 网关层、订单微服务和商品微服务添加Zipkin的依赖

  • 修改部分:
  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-zipkin</artifactId>
  4. </dependency>
  • 网关层的完整pom:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://maven.apache.org/POM/4.0.0"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>spring_cloud_demo</artifactId>
  7. <groupId>org.sunxiaping</groupId>
  8. <version>1.0</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>api_gateway_server7007</artifactId>
  12. <dependencies>
  13. <!--
  14. Spring Cloud Gateway使用的web框架是webflux,和SpringMVC不兼容。引入的限流组件是Hystrix。Redis底层不再使用jedis,而是lettuce。
  15. -->
  16. <dependency>
  17. <groupId>org.springframework.cloud</groupId>
  18. <artifactId>spring-cloud-starter-gateway</artifactId>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.springframework.cloud</groupId>
  22. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.cloud</groupId>
  26. <artifactId>spring-cloud-starter-sleuth</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.cloud</groupId>
  30. <artifactId>spring-cloud-starter-zipkin</artifactId>
  31. </dependency>
  32. </dependencies>
  33. </project>
  • 订单微服务的完整pom:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://maven.apache.org/POM/4.0.0"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>spring_cloud_demo</artifactId>
  7. <groupId>org.sunxiaping</groupId>
  8. <version>1.0</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>order_service8003</artifactId>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework.cloud</groupId>
  15. <artifactId>spring-cloud-starter-openfeign</artifactId>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-web</artifactId>
  20. </dependency>
  21. <!-- 导入Eureka Client对应的坐标 -->
  22. <dependency>
  23. <groupId>org.springframework.cloud</groupId>
  24. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-actuator</artifactId>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework.cloud</groupId>
  32. <artifactId>spring-cloud-starter-sleuth</artifactId>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework.cloud</groupId>
  36. <artifactId>spring-cloud-starter-zipkin</artifactId>
  37. </dependency>
  38. </dependencies>
  39. </project>
  • 商品微服务的完整pom:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://maven.apache.org/POM/4.0.0"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>spring_cloud_demo</artifactId>
  7. <groupId>org.sunxiaping</groupId>
  8. <version>1.0</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>product_service9004</artifactId>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-web</artifactId>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-data-jpa</artifactId>
  20. </dependency>
  21. <dependency>
  22. <groupId>mysql</groupId>
  23. <artifactId>mysql-connector-java</artifactId>
  24. </dependency>
  25. <!-- 导入Eureka Client对应的坐标 -->
  26. <dependency>
  27. <groupId>org.springframework.cloud</groupId>
  28. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter-actuator</artifactId>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework.cloud</groupId>
  36. <artifactId>spring-cloud-starter-sleuth</artifactId>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.springframework.cloud</groupId>
  40. <artifactId>spring-cloud-starter-zipkin</artifactId>
  41. </dependency>
  42. </dependencies>
  43. </project>

6.2.2 网关层、订单微服务和商品微服务修改配置文件

  • 修改部分:
  1. spring:
  2. # zipkin
  3. zipkin:
  4. base-url: http://192.168.217.100:9411/ # Zipkin Server端的请求地址
  5. sender:
  6. type: web # 数据的传输方式,以HTTP的形式向Zipkin Server端发送数据
  7. sleuth:
  8. sampler:
  9. probability: 1 # 采样比 默认为0.1,即10%,这里配置1,是记录全部的sleuth信息,是为了收集到更多的数据(仅供测试使用)
  • 网关层完整的application.yml:
  1. server:
  2. port: 7007
  3. spring:
  4. application:
  5. name: api-gateway-server
  6. # 配置 Spring Cloud Gateway
  7. cloud:
  8. gateway:
  9. discovery:
  10. locator:
  11. enabled: true # 开启从注册中心动态创建路由的功能,利用微服务名进行路由
  12. lower-case-service-id: true # 微服务名称以小写形式呈现
  13. routes:
  14. # 配置路由: 路由id,路由到微服务的uri,断言(判断条件)
  15. - id: product-service # 路由id
  16. # uri: http://localhost:9004
  17. uri: lb://service-product # 路由到微服务的uri。 lb://xxx,lb代表从注册中心获取服务列表,xxx代表需要转发的微服务的名称
  18. predicates: # 断言(判断条件)
  19. # - Path=/product/**
  20. - Path=/product-service/**
  21. filters: # 配置路由过滤器 http://localhost:7007/product-service/product/findById/1 --> http://localhost:7007/product/findById/1
  22. - RewritePath=/product-service/(?<segment>.*), /$\{segment} # 路径重写的过滤器
  23. # zipkin
  24. zipkin:
  25. base-url: http://192.168.217.100:9411/ # Zipkin Server端的请求地址
  26. sender:
  27. type: web # 数据的传输方式,以HTTP的形式向Zipkin Server端发送数据
  28. sleuth:
  29. sampler:
  30. probability: 1 # 采样比 默认为0.1,即10%,这里配置1,是记录全部的sleuth信息,是为了收集到更多的数据(仅供测试使用)
  31. # 配置 eureka
  32. eureka:
  33. instance:
  34. # 主机名称:服务名称修改,其实就是向eureka server中注册的实例id
  35. instance-id: api-gateway-server:${server.port}
  36. # 显示IP信息
  37. prefer-ip-address: true
  38. client:
  39. service-url: # 此处修改为 Eureka Server的集群地址
  40. defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  41. logging:
  42. level:
  43. root: INFO
  44. org.springframework.web.servlet.DispatcherServlet: DEBUG
  45. org.springframework.cloud.sleuth: DEBUG
  46. org.springframework.cloud.gateway: trace
  47. org.springframework.http.server.reactive: debug
  48. org.springframework.web.reactive: debug
  49. reactor.ipc.netty: debug
  • 订单微服务完整的application.yml:
  1. server:
  2. port: 8003 # 微服务的端口号
  3. spring:
  4. application:
  5. name: service-order # 微服务的名称
  6. # zipkin
  7. zipkin:
  8. base-url: http://192.168.217.100:9411/ # Zipkin Server端的请求地址
  9. sender:
  10. type: web # 数据的传输方式,以HTTP的形式向Zipkin Server端发送数据
  11. sleuth:
  12. sampler:
  13. probability: 1 # 采样比 默认为0.1,即10%,这里配置1,是记录全部的sleuth信息,是为了收集到更多的数据(仅供测试使用)
  14. # 配置 eureka
  15. eureka:
  16. instance:
  17. # 主机名称:服务名称修改,其实就是向eureka server中注册的实例id
  18. instance-id: service-order:${server.port}
  19. # 显示IP信息
  20. prefer-ip-address: true
  21. client:
  22. service-url: # 此处修改为 Eureka Server的集群地址
  23. defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  24. feign:
  25. hystrix: # 开启Feign中的Hystrix
  26. enabled: true
  27. # 暴露所有端点
  28. management:
  29. endpoints:
  30. web:
  31. exposure:
  32. include: '*'
  33. hystrix:
  34. command:
  35. default:
  36. execution:
  37. isolation:
  38. thread:
  39. timeoutInMilliseconds: 3000 # 默认的连接超时时间为1秒,如果1秒没有返回数据,就自动触发降级逻辑
  40. # 微服务info内容详细信息
  41. info:
  42. app.name: xxx
  43. company.name: xxx
  44. build.artifactId: $project.artifactId$
  45. build.version: $project.version$
  46. logging:
  47. level:
  48. root: INFO
  49. org.springframework.web.servlet.DispatcherServlet: DEBUG
  50. org.springframework.cloud.sleuth: DEBUG
  51. org.springframework.cloud.gateway: trace
  52. org.springframework.http.server.reactive: debug
  53. org.springframework.web.reactive: debug
  54. reactor.ipc.netty: debug
  • 商品微服务完整的application.yml:
  1. server:
  2. port: 9004 # 微服务的端口号
  3. spring:
  4. application:
  5. name: service-product # 微服务的名称
  6. datasource:
  7. url: jdbc:mysql://192.168.217.100:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
  8. driver-class-name: com.mysql.cj.jdbc.Driver
  9. username: root
  10. password: 123456
  11. jpa:
  12. generate-ddl: true
  13. show-sql: true
  14. open-in-view: true
  15. database: mysql
  16. # zipkin
  17. zipkin:
  18. base-url: http://192.168.217.100:9411/ # Zipkin Server端的请求地址
  19. sender:
  20. type: web # 数据的传输方式,以HTTP的形式向Zipkin Server端发送数据
  21. sleuth:
  22. sampler:
  23. probability: 1 # 采样比 默认为0.1,即10%,这里配置1,是记录全部的sleuth信息,是为了收集到更多的数据(仅供测试使用)
  24. # 配置 eureka
  25. eureka:
  26. instance:
  27. # 主机名称:服务名称修改,其实就是向eureka server中注册的实例id
  28. instance-id: service-product:${server.port}
  29. # 显示IP信息
  30. prefer-ip-address: true
  31. client:
  32. service-url: # 此处修改为 Eureka Server的集群地址
  33. defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  34. logging:
  35. level:
  36. root: INFO
  37. org.springframework.web.servlet.DispatcherServlet: DEBUG
  38. org.springframework.cloud.sleuth: DEBUG
  39. org.springframework.cloud.gateway: trace
  40. org.springframework.http.server.reactive: debug
  41. org.springframework.web.reactive: debug
  42. reactor.ipc.netty: debug
  43. # 微服务info内容详细信息
  44. info:
  45. app.name: xxx
  46. company.name: xxx
  47. build.artifactId: $project.artifactId$
  48. build.version: $project.version$

6.2.3 测试

  • 分别重启网关层、订单微服务和商品微服务,通过浏览器发送一次微服务请求。打开Zipkin的Web UI控制台,我们可以根据条件追踪每次请求调用过程。

Zipkin的WebUI中看到的监控信息.png

  • 点击该trace可以看到请求的细节:

点击该trace可以看到请求的细节.png

7 分析Zipkin整合Sleuth的问题

分析Zipkin整合Sleuth的问题.jpg

  • 由上图可知:
  • 微服务的链路追踪Zipkin(不推荐) - 图12链路数据如何持久化保存(当前数据保存在Zipkin服务端的内存中,断电易丢失)。
  • 微服务的链路追踪Zipkin(不推荐) - 图13如何优化数据采集过程(HTTP方式是同步阻塞方式,一旦出现网络波动等情况,可能会波及业务系统)。

8 存储跟踪数据

8.1 概述

  • Zipkin Server默认是将追踪数据信息保存到内存中,这种方式不适合生产环境。因为一旦Zipkin Server端关闭重启或者服务崩溃,就会导致历史数据小时。Zipkin支持将追踪数据持久化到MySQL数据库中或者存储到ElasticSearch中。

8.2 准备数据库

  • 可以从官网中找到Zipkin Server持久化的MySQL数据库脚本:
  1. SET NAMES utf8mb4;
  2. SET FOREIGN_KEY_CHECKS = 0;
  3. -- ----------------------------
  4. -- Table structure for zipkin_annotations
  5. -- ----------------------------
  6. DROP TABLE IF EXISTS `zipkin_annotations`;
  7. CREATE TABLE `zipkin_annotations` (
  8. `trace_id_high` bigint(20) NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit',
  9. `trace_id` bigint(20) NOT NULL COMMENT 'coincides with zipkin_spans.trace_id',
  10. `span_id` bigint(20) NOT NULL COMMENT 'coincides with zipkin_spans.id',
  11. `a_key` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT 'BinaryAnnotation.key or Annotation.value if type == -1',
  12. `a_value` blob NULL COMMENT 'BinaryAnnotation.value(), which must be smaller than 64KB',
  13. `a_type` int(11) NOT NULL COMMENT 'BinaryAnnotation.type() or -1 if Annotation',
  14. `a_timestamp` bigint(20) NULL DEFAULT NULL COMMENT 'Used to implement TTL; Annotation.timestamp or zipkin_spans.timestamp',
  15. `endpoint_ipv4` int(11) NULL DEFAULT NULL COMMENT 'Null when Binary/Annotation.endpoint is null',
  16. `endpoint_ipv6` binary(16) NULL DEFAULT NULL COMMENT 'Null when Binary/Annotation.endpoint is null, or no IPv6 address',
  17. `endpoint_port` smallint(6) NULL DEFAULT NULL COMMENT 'Null when Binary/Annotation.endpoint is null',
  18. `endpoint_service_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT 'Null when Binary/Annotation.endpoint is null',
  19. UNIQUE INDEX `trace_id_high`(`trace_id_high`, `trace_id`, `span_id`, `a_key`, `a_timestamp`) USING BTREE COMMENT 'Ignore insert on duplicate',
  20. INDEX `trace_id_high_2`(`trace_id_high`, `trace_id`, `span_id`) USING BTREE COMMENT 'for joining with zipkin_spans',
  21. INDEX `trace_id_high_3`(`trace_id_high`, `trace_id`) USING BTREE COMMENT 'for getTraces/ByIds',
  22. INDEX `endpoint_service_name`(`endpoint_service_name`) USING BTREE COMMENT 'for getTraces and getServiceNames',
  23. INDEX `a_type`(`a_type`) USING BTREE COMMENT 'for getTraces and autocomplete values',
  24. INDEX `a_key`(`a_key`) USING BTREE COMMENT 'for getTraces and autocomplete values',
  25. INDEX `trace_id`(`trace_id`, `span_id`, `a_key`) USING BTREE COMMENT 'for dependencies job'
  26. ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Compressed;
  27. -- ----------------------------
  28. -- Table structure for zipkin_dependencies
  29. -- ----------------------------
  30. DROP TABLE IF EXISTS `zipkin_dependencies`;
  31. CREATE TABLE `zipkin_dependencies` (
  32. `day` date NOT NULL,
  33. `parent` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  34. `child` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  35. `call_count` bigint(20) NULL DEFAULT NULL,
  36. `error_count` bigint(20) NULL DEFAULT NULL,
  37. PRIMARY KEY (`day`, `parent`, `child`) USING BTREE
  38. ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Compressed;
  39. -- ----------------------------
  40. -- Table structure for zipkin_spans
  41. -- ----------------------------
  42. DROP TABLE IF EXISTS `zipkin_spans`;
  43. CREATE TABLE `zipkin_spans` (
  44. `trace_id_high` bigint(20) NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit',
  45. `trace_id` bigint(20) NOT NULL,
  46. `id` bigint(20) NOT NULL,
  47. `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  48. `remote_service_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  49. `parent_id` bigint(20) NULL DEFAULT NULL,
  50. `debug` bit(1) NULL DEFAULT NULL,
  51. `start_ts` bigint(20) NULL DEFAULT NULL COMMENT 'Span.timestamp(): epoch micros used for endTs query and to implement TTL',
  52. `duration` bigint(20) NULL DEFAULT NULL COMMENT 'Span.duration(): micros used for minDuration and maxDuration query',
  53. PRIMARY KEY (`trace_id_high`, `trace_id`, `id`) USING BTREE,
  54. INDEX `trace_id_high`(`trace_id_high`, `trace_id`) USING BTREE COMMENT 'for getTracesByIds',
  55. INDEX `name`(`name`) USING BTREE COMMENT 'for getTraces and getSpanNames',
  56. INDEX `remote_service_name`(`remote_service_name`) USING BTREE COMMENT 'for getTraces and getRemoteServiceNames',
  57. INDEX `start_ts`(`start_ts`) USING BTREE COMMENT 'for getTraces ordering and range'
  58. ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Compressed;
  59. SET FOREIGN_KEY_CHECKS = 1;

8.3 命令行方式配置启动服务端

  1. # STORAGE_TYPE 存储类型
  2. # MYSQL_HOST MySQL主机地址
  3. # MYSQL_TCP_PORT MySQL端口
  4. # MYSQL_DB MySQL数据库名称
  5. # MYSQL_USER MySQL用户名
  6. # MYSQL_PASS MySQL地址
  7. java -jar zipkin-server-2.12.9-exec.jar --STORAGE_TYPE=mysql --MYSQL_HOST=192.168.217.100 --MYSQL_TCP_PORT=3306 --MYSQL_DB=zipkin --MYSQL_USER=root --MYSQL_PASS=123456

8.4 Docker方式配置启动服务端

  1. docker run -d \
  2. -p 9411:9411 \
  3. --restart always \
  4. -v /etc/localtime:/etc/localtime:ro \
  5. -e MYSQL_USER=root \
  6. -e MYSQL_PASS=123456 \
  7. -e MYSQL_HOST=192.168.217.100 \
  8. -e STORAGE_TYPE=mysql \
  9. -e MYSQL_DB=zipkin \
  10. -e MYSQL_TCP_PORT=3306 \
  11. --name zipkin \
  12. openzipkin/zipkin:2.12.9

8.5 测试

  • 配置好服务daunt之后,可以在浏览器中请求几次。回到数据库查看,会发现数据已经持久化到MySQL中了。

Zipkin数据持久化到MySQL中.png

9 基于消息中间件收集数据

9.1 概述

  • 在默认情况下,Zipkin客户端和Zipkin服务端之间是使用HTTP请求的方式进行通信(即同步的请求方式)。
  • 在网络波动,Zipkin服务端异常等情况下,可能会存在信息收集不机制的问题。
  • Zipkin支持和RabbitMQ整合完整异步消息传输。

基于消息中间件收集数据.jpg

9.2 步骤

  • 微服务的链路追踪Zipkin(不推荐) - 图16准备RabbitMQ中间件。
  • 微服务的链路追踪Zipkin(不推荐) - 图17修改Zipkin客户端,将消息发送到MQ服务器。
  • 微服务的链路追踪Zipkin(不推荐) - 图18修改Zipkin服务daunt,从MQ中拉取消息。

9.3 RabbitMQ的安装和启动

  • 以Docker形式安装和启动RabbitMQ:
  1. docker run -d --name rabbit -p 15672:15672 -p 5672:5672 rabbitmq:management

9.4 命令行方式配置启动服务端

  1. # STORAGE_TYPE 存储类型
  2. # MYSQL_HOST MySQL主机地址
  3. # MYSQL_TCP_PORT MySQL端口
  4. # MYSQL_DB MySQL数据库名称
  5. # MYSQL_USER MySQL用户名
  6. # MYSQL_PASS MySQL地址
  7. # RABBIT_ADDRESSES 指定Rabbitmq的地址
  8. # RABBIT_USER 用户名(默认为guest)
  9. # RABBIT_PASSWORD 密码(默认为guest)
  10. java -jar zipkin-server-2.12.9-exec.jar --STORAGE_TYPE=mysql --MYSQL_HOST=192.168.217.100 --MYSQL_TCP_PORT=3306 --MYSQL_DB=zipkin --MYSQL_USER=root --MYSQL_PASS=123456 --RABBIT_ADDRESSES=192.168.217.100:5672 --RABBIT_USER=guest --RABBIT_PASSWORD=guest

9.5 Docker方式启动配置启动服务端

  1. docker run -d \
  2. -p 9411:9411 \
  3. --restart always \
  4. -v /etc/localtime:/etc/localtime:ro \
  5. -e MYSQL_USER=root \
  6. -e MYSQL_PASS=123456 \
  7. -e MYSQL_HOST=192.168.217.100 \
  8. -e STORAGE_TYPE=mysql \
  9. -e MYSQL_DB=zipkin \
  10. -e MYSQL_TCP_PORT=3306 \
  11. -e RABBIT_ADDRESSES=192.168.217.100:5672 \
  12. -e RABBIT_USER=guest \
  13. -e RABBIT_PASSWORD=guest \
  14. --name zipkin \
  15. openzipkin/zipkin:2.12.9

Zipkin服务端配置消息中间件.png

9.6 客户端配置

9.6.1 导入相关jar包的Maven坐标

  • 修改部分:
  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-sleuth</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.cloud</groupId>
  7. <artifactId>spring-cloud-starter-zipkin</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>org.springframework.cloud</groupId>
  11. <artifactId>spring-cloud-sleuth-zipkin</artifactId>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-amqp</artifactId>
  16. </dependency>
  • 网关层的完整pom:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://maven.apache.org/POM/4.0.0"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>spring_cloud_demo</artifactId>
  7. <groupId>org.sunxiaping</groupId>
  8. <version>1.0</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>api_gateway_server7007</artifactId>
  12. <dependencies>
  13. <!--
  14. Spring Cloud Gateway使用的web框架是webflux,和SpringMVC不兼容。引入的限流组件是Hystrix。Redis底层不再使用jedis,而是lettuce。
  15. -->
  16. <dependency>
  17. <groupId>org.springframework.cloud</groupId>
  18. <artifactId>spring-cloud-starter-gateway</artifactId>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.springframework.cloud</groupId>
  22. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.cloud</groupId>
  26. <artifactId>spring-cloud-starter-sleuth</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.cloud</groupId>
  30. <artifactId>spring-cloud-starter-zipkin</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.cloud</groupId>
  34. <artifactId>spring-cloud-sleuth-zipkin</artifactId>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-starter-amqp</artifactId>
  39. </dependency>
  40. </dependencies>
  41. </project>
  • 订单微服务的完整pom:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://maven.apache.org/POM/4.0.0"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>spring_cloud_demo</artifactId>
  7. <groupId>org.sunxiaping</groupId>
  8. <version>1.0</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>order_service8003</artifactId>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework.cloud</groupId>
  15. <artifactId>spring-cloud-starter-openfeign</artifactId>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-web</artifactId>
  20. </dependency>
  21. <!-- 导入Eureka Client对应的坐标 -->
  22. <dependency>
  23. <groupId>org.springframework.cloud</groupId>
  24. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-actuator</artifactId>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework.cloud</groupId>
  32. <artifactId>spring-cloud-starter-sleuth</artifactId>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework.cloud</groupId>
  36. <artifactId>spring-cloud-starter-zipkin</artifactId>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.springframework.cloud</groupId>
  40. <artifactId>spring-cloud-sleuth-zipkin</artifactId>
  41. </dependency>
  42. <dependency>
  43. <groupId>org.springframework.boot</groupId>
  44. <artifactId>spring-boot-starter-amqp</artifactId>
  45. </dependency>
  46. </dependencies>
  47. </project>
  • 商品微服务的完整pom:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://maven.apache.org/POM/4.0.0"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>spring_cloud_demo</artifactId>
  7. <groupId>org.sunxiaping</groupId>
  8. <version>1.0</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>product_service9004</artifactId>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-web</artifactId>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-data-jpa</artifactId>
  20. </dependency>
  21. <dependency>
  22. <groupId>mysql</groupId>
  23. <artifactId>mysql-connector-java</artifactId>
  24. </dependency>
  25. <!-- 导入Eureka Client对应的坐标 -->
  26. <dependency>
  27. <groupId>org.springframework.cloud</groupId>
  28. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter-actuator</artifactId>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework.cloud</groupId>
  36. <artifactId>spring-cloud-starter-sleuth</artifactId>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.springframework.cloud</groupId>
  40. <artifactId>spring-cloud-starter-zipkin</artifactId>
  41. </dependency>
  42. <dependency>
  43. <groupId>org.springframework.cloud</groupId>
  44. <artifactId>spring-cloud-sleuth-zipkin</artifactId>
  45. </dependency>
  46. <dependency>
  47. <groupId>org.springframework.boot</groupId>
  48. <artifactId>spring-boot-starter-amqp</artifactId>
  49. </dependency>
  50. </dependencies>
  51. </project>

9.6.2 配置消息中间件的地址

  • 修改部分:
  1. spring:
  2. # zipkin
  3. zipkin:
  4. # base-url: http://192.168.217.100:9411/ # Zipkin Server端的请求地址
  5. sender:
  6. type: rabbit
  7. # type: web # 数据的传输方式,以HTTP的形式向Zipkin Server端发送数据
  8. sleuth:
  9. sampler:
  10. probability: 1 # 采样比 默认为0.1,即10%,这里配置1,是记录全部的sleuth信息,是为了收集到更多的数据(仅供测试使用)
  11. rabbitmq:
  12. host: 192.168.217.100
  13. port: 5672
  14. username: guest
  15. password: guest
  16. # 重试机制
  17. listener:
  18. direct:
  19. retry:
  20. enabled: true
  21. simple:
  22. retry:
  23. enabled: true
  • 网关层的完整application.yml
  1. server:
  2. port: 7007
  3. spring:
  4. application:
  5. name: api-gateway-server
  6. # 配置 Spring Cloud Gateway
  7. cloud:
  8. gateway:
  9. discovery:
  10. locator:
  11. enabled: true # 开启从注册中心动态创建路由的功能,利用微服务名进行路由
  12. lower-case-service-id: true # 微服务名称以小写形式呈现
  13. routes:
  14. # 配置路由: 路由id,路由到微服务的uri,断言(判断条件)
  15. - id: product-service # 路由id
  16. # uri: http://localhost:9004
  17. uri: lb://service-product # 路由到微服务的uri。 lb://xxx,lb代表从注册中心获取服务列表,xxx代表需要转发的微服务的名称
  18. predicates: # 断言(判断条件)
  19. # - Path=/product/**
  20. - Path=/product-service/**
  21. filters: # 配置路由过滤器 http://localhost:7007/product-service/product/findById/1 --> http://localhost:7007/product/findById/1
  22. - RewritePath=/product-service/(?<segment>.*), /$\{segment} # 路径重写的过滤器
  23. # zipkin
  24. zipkin:
  25. # base-url: http://192.168.217.100:9411/ # Zipkin Server端的请求地址
  26. sender:
  27. type: rabbit
  28. # type: web # 数据的传输方式,以HTTP的形式向Zipkin Server端发送数据
  29. sleuth:
  30. sampler:
  31. probability: 1 # 采样比 默认为0.1,即10%,这里配置1,是记录全部的sleuth信息,是为了收集到更多的数据(仅供测试使用)
  32. rabbitmq:
  33. host: 192.168.217.100
  34. port: 5672
  35. username: guest
  36. password: guest
  37. # 重试机制
  38. listener:
  39. direct:
  40. retry:
  41. enabled: true
  42. simple:
  43. retry:
  44. enabled: true
  45. # 配置 eureka
  46. eureka:
  47. instance:
  48. # 主机名称:服务名称修改,其实就是向eureka server中注册的实例id
  49. instance-id: api-gateway-server:${server.port}
  50. # 显示IP信息
  51. prefer-ip-address: true
  52. client:
  53. service-url: # 此处修改为 Eureka Server的集群地址
  54. defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  55. logging:
  56. level:
  57. root: INFO
  58. org.springframework.web.servlet.DispatcherServlet: DEBUG
  59. org.springframework.cloud.sleuth: DEBUG
  60. org.springframework.cloud.gateway: trace
  61. org.springframework.http.server.reactive: debug
  62. org.springframework.web.reactive: debug
  63. reactor.ipc.netty: debug
  • 订单微服务的application.yml
  1. server:
  2. port: 8003 # 微服务的端口号
  3. spring:
  4. application:
  5. name: service-order # 微服务的名称
  6. # zipkin
  7. zipkin:
  8. # base-url: http://192.168.217.100:9411/ # Zipkin Server端的请求地址
  9. sender:
  10. type: rabbit
  11. # type: web # 数据的传输方式,以HTTP的形式向Zipkin Server端发送数据
  12. sleuth:
  13. sampler:
  14. probability: 1 # 采样比 默认为0.1,即10%,这里配置1,是记录全部的sleuth信息,是为了收集到更多的数据(仅供测试使用)
  15. rabbitmq:
  16. host: 192.168.217.100
  17. port: 5672
  18. username: guest
  19. password: guest
  20. # 重试机制
  21. listener:
  22. direct:
  23. retry:
  24. enabled: true
  25. simple:
  26. retry:
  27. enabled: true
  28. # 配置 eureka
  29. eureka:
  30. instance:
  31. # 主机名称:服务名称修改,其实就是向eureka server中注册的实例id
  32. instance-id: service-order:${server.port}
  33. # 显示IP信息
  34. prefer-ip-address: true
  35. client:
  36. service-url: # 此处修改为 Eureka Server的集群地址
  37. defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  38. feign:
  39. hystrix: # 开启Feign中的Hystrix
  40. enabled: true
  41. # 暴露所有端点
  42. management:
  43. endpoints:
  44. web:
  45. exposure:
  46. include: '*'
  47. hystrix:
  48. command:
  49. default:
  50. execution:
  51. isolation:
  52. thread:
  53. timeoutInMilliseconds: 3000 # 默认的连接超时时间为1秒,如果1秒没有返回数据,就自动触发降级逻辑
  54. # 微服务info内容详细信息
  55. info:
  56. app.name: xxx
  57. company.name: xxx
  58. build.artifactId: $project.artifactId$
  59. build.version: $project.version$
  60. logging:
  61. level:
  62. root: INFO
  63. org.springframework.web.servlet.DispatcherServlet: DEBUG
  64. org.springframework.cloud.sleuth: DEBUG
  65. org.springframework.cloud.gateway: trace
  66. org.springframework.http.server.reactive: debug
  67. org.springframework.web.reactive: debug
  68. reactor.ipc.netty: debug
  • 商品微服务的完整application.yml
  1. server:
  2. port: 9004 # 微服务的端口号
  3. spring:
  4. application:
  5. name: service-product # 微服务的名称
  6. datasource:
  7. url: jdbc:mysql://192.168.217.100:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
  8. driver-class-name: com.mysql.cj.jdbc.Driver
  9. username: root
  10. password: 123456
  11. jpa:
  12. generate-ddl: true
  13. show-sql: true
  14. open-in-view: true
  15. database: mysql
  16. # zipkin
  17. zipkin:
  18. # base-url: http://192.168.217.100:9411/ # Zipkin Server端的请求地址
  19. sender:
  20. type: rabbit
  21. # type: web # 数据的传输方式,以HTTP的形式向Zipkin Server端发送数据
  22. sleuth:
  23. sampler:
  24. probability: 1 # 采样比 默认为0.1,即10%,这里配置1,是记录全部的sleuth信息,是为了收集到更多的数据(仅供测试使用)
  25. rabbitmq:
  26. host: 192.168.217.100
  27. port: 5672
  28. username: guest
  29. password: guest
  30. # 重试机制
  31. listener:
  32. direct:
  33. retry:
  34. enabled: true
  35. simple:
  36. retry:
  37. enabled: true
  38. # 配置 eureka
  39. eureka:
  40. instance:
  41. # 主机名称:服务名称修改,其实就是向eureka server中注册的实例id
  42. instance-id: service-product:${server.port}
  43. # 显示IP信息
  44. prefer-ip-address: true
  45. client:
  46. service-url: # 此处修改为 Eureka Server的集群地址
  47. defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  48. logging:
  49. level:
  50. root: INFO
  51. org.springframework.web.servlet.DispatcherServlet: DEBUG
  52. org.springframework.cloud.sleuth: DEBUG
  53. org.springframework.cloud.gateway: trace
  54. org.springframework.http.server.reactive: debug
  55. org.springframework.web.reactive: debug
  56. reactor.ipc.netty: debug
  57. # 微服务info内容详细信息
  58. info:
  59. app.name: xxx
  60. company.name: xxx
  61. build.artifactId: $project.artifactId$
  62. build.version: $project.version$

9.7 测试

  • 关闭Zipkin Server,并随意发送请求。打开RabbitMQ管理后台可以看到,消费已经推送到了RabbitMQ中。
  • 当Zipkin Server启动时,会自动从RabbitMQ获取消息并消费,展示追踪数据。