springboot开启prometheus可采集的指标配置

发布时间 2023-08-14 14:09:42作者: 打工人小余

1、引包

        <!-- 实现对 Actuator 的自动化配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- Micrometer 对 Prometheus 的支持 -->
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
        </dependency>
spring-boot-starter-actuator包不需要配置版本,跟着你配置的 spring-boot版本 保持一致就好
micrometer-registry-prometheus包需要查看你项目中依赖的 spring-boot-dependencies 项目的pom文件中 micrometer.version 版本是多少,保持一致即可

例如我的springboot版本是2.6.11

 对应的 micrometer-registry-prometheus版本就是1.8.9

2、配置文件

#  endpoints config
management:
  endpoint:
    health: # 请求 http://localhost:48082/testService/actuator/health
      show-details: always
  endpoints:
    enabled-by-default: true
    web:
      base-path: /actuator
      exposure:  #请求 http://localhost:48082/testService/actuator
        include: '*' # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 * ,可以开放所有端点。
  metrics:
    tags: # 通用标签
      application: ${spring.application.name}  # 请求 http://localhost:48082/testService/actuator/prometheus

  请求的时候一定要带上配置

server:
  port: 48082
  servlet:
    context-path: /testService