结合 Spring Boot Actuator 和 Micrometer 提供 Prometheus 指标。
编辑 pom.xml 文件,添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<version>1.3.2</version>
</dependency>
编辑 application.properties 文件,添加配置:
management.endpoints.web.exposure.include=prometheus
management.endpoint.prometheus.enabled=true
management.metrics.tags.application=${APPLICTION_NAME}
配置项 management.endpoints.web.exposure.include 指定使用 HTTP 开放的 endpoint;
配置项 management.endpoint.prometheus.enabled 用于启用 Prometheus 的 endpoint;
配置项 management.metrics.tags.application 用于指定自定义标签。
编辑 prometheus.yaml 添加抓取的 target:
scrape_configs:
- job_name: "spring-boot"
scrape_interval: 5s
metrics_path: "/actuator/prometheus"
static_configs:
- targets: ["HOST:PORT"]
配置 Prometheus 数据源,导入 JVM (Micrometer) 仪表盘。