• 【Spring】SpringBoot+Actuator+Prometheus+Grafana监控实现及常见问题处理


    当我们添加一个服务之后,不可避免的就是对这个服务的监控,如何才能更好的监控到服务的运行状况呢,这里有一种实现方式,就是使用actuator监控服务,然后通过pushgateway推送监控数据到prometheus,然后再在Grafana上配置相应dashboards,这样我们就能更方便的看到我们服务的一些基础监控数据了

    一、实现过程

    1、依赖引入

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
                <version>${spring.boot.version}</version>
            </dependency>
            <dependency>
                <groupId>io.micrometer</groupId>
                <artifactId>micrometer-registry-prometheus</artifactId>
                <version>${micrometer.registry.prometheus.version}</version>
            </dependency>
            <dependency>
                <groupId>io.prometheus</groupId>
                <artifactId>simpleclient_pushgateway</artifactId>
                <version>${simpleclient.pushgateway.version}</version>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    2、配置

    management:
      metrics:
        export:
          prometheus:
            pushgateway:
              base-url: xxx.xxx.xxx.xxx:xxx
              enabled: true
              job: metrics-demo
              push-rate: 60s
              shutdown-operation: push
              grouping-key:
                instance: pod_name_demo
        tags:
          application: ${spring.application.name}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    配置说明:
    base-url:pushgateway的地址
    enabled:开启prometheus的数据推送模式
    job:任务名称
    push-rate:推送数据的频率,默认1m(单位分钟)
    shutdown-operation:在jvm关闭之前将数据推送出去
    instance:pushgateway的推送分组,一般为实例ip,动态的可以通过环境变量获取,也可配置在环境变量中,从环境变量中取
    application:应用名称

    3、服务重新部署

    成功上报后,我们也可通过访问/actuator/prometheus接口(示例:http://localhost:8080/actuator/prometheus)查看已上报的监控指标及对应数值

    4、grafana添加dashboards

    可自己配置需要的监控,也可直接用JVM监控模板(例如导入4701即可)

    二、常见错误

    1、报jackson、log相关错误

    是依赖冲突导致,排除冲突的依赖

    排除spring-boot-starter-actuator中的冲突依赖

     			<exclusions>
                    <exclusion>
                        <groupId>com.fasterxml.jackson.core</groupId>
                        <artifactId>jackson-databind</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>com.fasterxml.jackson.datatype</groupId>
                        <artifactId>jackson-datatype-jsr310</artifactId>
                    </exclusion>
                    <exclusion>
                        <artifactId>spring-boot-starter-logging</artifactId>
                        <groupId>org.springframework.boot</groupId>
                    </exclusion>
                </exclusions>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    2、未上报数据到prometheus

    是因为没有配置应用Tag
    配置的方式有俩种

    (1)在配置文件中添加tag配置

    management.metrics.tags.application=${spring.application.name}
    
    • 1

    (2)添加JAVA配置类

    @Configuration
    public class MetricsConfig {
        @Bean
        MeterRegistryCustomizer<MeterRegistry> configurer(@Value("${spring.application.name}") String applicationName) {
            return (registry) -> registry.config().commonTags("application", applicationName);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    二者选一种方法即可

    3、pushed metrics are invalid or inconsistent with existing metrics: collected metric

    错误示例:

    untyped:<value:10 > } is not a COUNTER
    
    • 1

    这是因为micrometer-registry-prometheus 这个依赖中MicrometerCollector类中的Collector.Type.UNTYPED这个UNTYPED不存在
    因为Collector中的Type是这些类型,没有UNTYPED
    所以将micrometer-registry-prometheus的版本升级到1.9.4就解决问题了
    升级版本后会发现micrometer-registry-prometheus 1.9.4版本中的Type.UNTYPED 改为了 Type.UNKNOWN

  • 相关阅读:
    IDL学习:语法基础-结构体
    springboot集成uid-generator生成分布式id
    1187 Candy,xtu
    C#练习题7和8
    prometheus初窥
    JavaScript入门②-函数(1)基础{浅出}
    ACCESS教程之如何实现基于关键词组合两个表、left join、字符串包含关系 excel和access联动(教程含详细操作方式)
    计组+系统02:30min导图复习 存储系统
    Python使用PyMongo4.x操作MongoDB总结
    性能测试/负载测试/压力测试之间的区别
  • 原文地址:https://blog.csdn.net/cxh6863/article/details/127591051