SpringBoot 使用 Prometheus 监控指标

1. 添加依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<version>1.5.1</version>
</dependency>

2. 配置文件

1
2
3
4
5
6
7
8
9
10
11
spring:
application:
name: prometheus-boot
management:
metrics:
tags:
application: ${spring.application.name}
endpoints:
web:
exposure:
include: "*"

3. 修改启动类

1
2
3
4
5
6
7
8
9
10
11
12
@SpringBootApplication
public class PrometheusApplication {
public static void main(String[] args) {
SpringApplication.run(PrometheusApplication.class, args);
}

@Bean
MeterRegistryCustomizer<MeterRegistry> configurer(
@Value("${spring.application.name}") String applicationName) {
return (registry) -> registry.config().commonTags("application", applicationName);
}
}

4. 配置 Prometheus

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
global:
scrape_interval: 60s
evaluation_interval: 60s

scrape_configs:
- job_name: prometheus
static_configs:
- targets: ['localhost:9090']
labels:
instance: prometheus

- job_name: linux
static_configs:
- targets: ['10.4.7.155:9100']
labels:
instance: localhost

- job_name: prometheus-boot
scrape_interval: 5s
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['10.4.7.155:8080']

5. 配置 Grafana

grafana-micrometer