本文章不介绍 Prometheus 和 Grafana 的安装

版本说明

SpringCloud 2021.0.3
SpringBoot 2.6.11
SpringCloudAlibaba 2021.1

1. 改造 SCG (Spring Cloud Gateway)

注意:
必须要 spring-boot-starter-actuator ,nacos-consul-adapter 是针对 promethus 本身是不支持直接通过nacos 监控,模拟 consul 的 api; 这里需要监控的话都需要打开监控端点,配置文件都需要
management.endpoints.web.exposure.include=*
参考: https://github.com/chen-gliu/nacos-consul-adapter

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-actuator' // 监控模块
    implementation 'org.springframework.cloud:spring-cloud-starter-gateway' // 当前依赖已经包含 webflux
    implementation 'org.springframework.cloud:spring-cloud-starter-loadbalancer' // 需要显式引用 loadbalancer
    implementation libs.reactor.pool
    // nacos 作为注册中心
    implementation ('com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery'){
        exclude group: 'com.alibaba.nacos',module:'nacos-client'
    }
    implementation libs.nacos.client
    implementation 'com.github.ben-manes.caffeine:caffeine' // 本地缓存
    // 配合 promethus
    implementation 'io.github.chen-gliu:nacos-consul-adapter:0.0.5.M'
    // prometheus + actuator 才能配合使用,对 SCG 本身也进行监控
    implementation libs.micrometer.registry.prometheus
}

2. 改造每个 SpringCloud 服务

引入依赖

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-actuator") // 监控模块
    // nacos 作为注册中心
    implementation ('com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery'){
        exclude group: 'com.alibaba.nacos',module:'nacos-client'
    }
    implementation libs.nacos.client
    implementation ('org.springframework.boot:spring-boot-starter-web'){
        exclude group:'org.springframework.boot',module:'spring-boot-starter-tomcat'
    }
    implementation 'org.springframework.boot:spring-boot-starter-undertow'
    // prometheus + actuator 才能配合使用
    implementation libs.micrometer.registry.prometheus
}

3. Prometheus 配置文件修改 prometheus.yml

scrape_configs:
  - job_name: 'consul-prometheus-test'
    metrics_path: '/actuator/prometheus'
    consul_sd_configs:
    #这里是nacos-consul-adapter适配器的服务,也就是我的网关的ip和端口号
    - server: '127.0.0.1:9091'

启动完成查看服务是否正常
http://localhost:9090/targets
image

Grafana

数据源配置
image-1661401518499
引入模板 id:10280
最终完成
image-1661401575210