在SpringBoot中如何通過Prometheus實時監(jiān)控系統(tǒng)各項指標
環(huán)境:springboot2.4.12 + prometheus1.6.7 + grafana7.5.7
什么是Prometheus
Prometheus 是一個開源的服務監(jiān)控系統(tǒng)和時間序列數(shù)據(jù)庫。
圖片
prometheus存儲的是時序數(shù)據(jù),即按相同時序(相同名稱和標簽),以時間維度存儲連續(xù)的數(shù)據(jù)的集合。
時序(time series)是由名字(Metric)以及一組key/value標簽定義的,具有相同的名字以及標簽屬于相同時序。
配置依賴
<dependencies>
<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>
</dependency>
</dependencies
spring:
application:
name: app-prometheus
---
management:
server:
port: 9999
endpoints:
enabled-by-default: true
web:
exposure:
include: '*'
注冊MeterRegistry
@Bean
public MeterRegistryCustomizer<MeterRegistry> configurer(@Value("${spring.application.name}") String name) {
return (registry) -> registry.config().commonTags("application", name);
}
訪問Prometheus actuator
圖片
Springboot與Prometheus的整合完成。
Prometheus配置安裝
Prometheus下載
圖片
通過如上地址下載自己需要的版本。
配置Prometheus
scrape_configs:
- job_name: 'app-prometheus'
scrape_interval: 5s
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['localhost:9999']
localhost:9999為項目的Actuator訪問地址。
啟動Prometheus
圖片
訪問
圖片
查看監(jiān)控的應用
圖片
圖片
自定義meter
@Resource
private MeterRegistry registry ;
private Counter counter ;
@PostConstruct
public void init() {
counter = this.registry.counter("vistor") ;
}
@GetMapping("/count")
public String count() {
this.counter.increment() ;
return "訪問次數(shù):" + this.counter.count() ;
}
先多訪問幾次該接口,通過Prometheus查看
圖片
Grafana安裝配置
下載
圖片
通過上面的地址下載grafana
啟動服務
圖片
默認用戶名密碼:admin/admin
添加Prometheus數(shù)據(jù)源
圖片
查看數(shù)據(jù)
圖片
圖片
這里展示了visitor中的統(tǒng)計信息
監(jiān)控數(shù)據(jù)庫連接池
圖片
先在grafana上搜索
圖片
通過id導入
圖片
圖片
圖片
項目中配置hikari數(shù)據(jù)庫連接池,grafana自動會展示數(shù)據(jù)庫連接信息
圖片
完畢?。?!