【云原生&微服务>SCG网关篇十】Spring Cloud Gateway集成Actuator、Zipkin详细案例

一、前言

至此微服务网关系列文章已出:

  1. 【云原生&微服务>SCG网关篇一】为什么要有网关、生产环境如何选择网关
  2. 云原生&微服务>SCG网关篇二】生产上那些灰度发布方式
  3. 【云原生&微服务>SCG网关篇三】Spring Cloud Gateway是什么、详细使用案例
  4. 云原生&微服务>SCG网关篇四】Spring Cloud Gateway内置的11种PredicateFactory如何使用
  5. 【云原生&微服务>SCG网关篇五】Spring Cloud Gateway自定义PredicateFactory
  6. 【云原生&微服务>SCG网关篇六】Spring Cloud Gateway内置的18种Filter使用姿势
  7. 【云原生&微服务>SCG网关篇七】Spring Cloud Gateway基于内置Filter实现限流、熔断、重试
  8. 【云原生&微服务>SCG网关篇八】Spring Cloud Gateway三种自定义Filter、GlobalFilter的方式
  9. 【云原生&微服务>SCG网关篇九】Spring Cloud Gateway集成Nacos详细案例

聊了以下问题:

  1. 为什么要有网关?网关的作用是什么?
  2. 网关的分类?
  3. 网关的技术选型?
  4. 使用网关时常用的灰度发布方式有哪些?
  5. Spring Cloud Gateway是什么?详细使用案例?
  6. Spring Cloud Gateway内置的11种PredicateFactory
  7. 如何自定义PredicateFactory?
  8. Spring Cloud Gateway内置的18种常用的Filter
  9. Spring Cloud Gateway基于内置Filter实现限流、熔断、重试
  10. Spring Cloud Gateway三种自定义Filter、GlobalFilter的方式
  11. Spring Cloud Gateway集成Nacos案例

本文接着聊Spring Cloud Gateway如何集成actuator实现服务状态追踪、集成zipkin实现服务调用链路信息追踪?

PS:SpringCloud版本信息:

<properties>
    <spring-boot.version>2.4.2</spring-boot.version>
    <spring-cloud.version>2020.0.1</spring-cloud.version>
    <spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring-boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <!--整合spring cloud-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <!--整合spring cloud alibaba-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>${spring-cloud-alibaba.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

二、集成Actuator监控网关

接着【云原生&微服务>SCG网关篇九】Spring Cloud Gateway集成Nacos详细案例继续操作;

gateway可以通过网关指标过滤器(GatewayMetricsFilter)给出Gateway的调用情况,

1> 在pom中引入spring-boot-starter-actuator依赖:

<!--集成actuator-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2> 在application.yml文件中指定监控的路径:

# 集成actuator,使用http://xxxx/actuator/metrics查看请求信息
management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: always

3> 使用http://127.0.0.1:9999/actuator/metrics查看请求监控信息的全部分类:

在这里插入图片描述

4> 使用http://127.0.0.1:9999/actuator/spring.cloud.gateway.requests查看gateway相关请求监控信息:

查看gateway相关请求监控信息之前,必须先请求一次gateway。

在这里插入图片描述

三、集成Zipkin

1> 在pom中引入zipkin和sleuth依赖:

<!--集成zipkin, ZipKin中包含sleuth-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>

2> 在application.yml中配置zipkin、sleuth信息:

spring:
    # 集成zipkin
  zipkin:
    base-url: http://127.0.0.1:9411/
    # zipkin不注册到nacos
    discovery-client-enabled: false
  sleuth:
    sampler:
      # 抽样率。默认是0.1(10%),即丢弃90%的数据
      probability: 1.0

3> 调用请求之后,访问http://127.0.0.1:9411/zipkin/追踪服务调用链路:

在这里插入图片描述
针对每个请求,可以点击右侧的SHOW按钮查看具体的服务调用链路信息:

在这里插入图片描述

四、总结

本文属于纯实操文章,讲了在Spring Cloud Gateway中如何集成actuator监控服务状态、集成zipkin / sleuth做服务调用链路追踪。

后面还有两篇实操文章:Gateway解决跨域问题、Gateway集成Sentinel实现限流;然后进入源码分析文章;分析Gateway的原理。

猜你喜欢

转载自blog.csdn.net/Saintmm/article/details/125834960