搭建prometheus+grafana監控SpringBoot應用入門
1. springBoot 應用准備
pom.xml依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--prometheus-->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<version>1.0.3</version>
</dependency>
並且主動向prometheus 上報application名.
/**
* 上報application 到 Prometheus
*/
@Bean
MeterRegistryCustomizer<MeterRegistry> metricsCommonTags() {
return registry -> registry.config().commonTags("application", "yourappName");
}
SpringBoot actuator端開啟 (自行調整,我這里全部開啟):
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
prometheus:
enabled: true
最好啟動一下查看端點暴露的情況,尤其檢查 /actuator/prometheus 是否成功暴露
2. 下載並安裝prometheus
最新下載地址 : https://prometheus.io/download/
配置prometheus.yml(安裝完成后的目錄下面有)
重點是配置好基礎的job_name,metrics_path,還有應用的ip和端口號,例如 :
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'test-application'
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['localhost:9527']
配置好之后,可以啟動prometheus,並且訪問prometheus,默認訪問地址 http://localhost:9090
此時可以看到prometheus的情況,點擊Status的targets,查看應用實例情況
3. 下載並安裝grafana(windows的安裝過程中最好關閉殺毒軟件,可能導致安裝失敗)
最新下載地址 : https://grafana.com/grafana/download
運行啟動grafana,默認訪問地址 : http://localhost:3000, 默認用戶名/密碼 admin/admin
配置datasource(Configuration里面),選擇Prometheus,配置好prometheus,本機運行的prometheus,則access選擇Server即可。然后點擊Save&Test
配置Dashboard,可以定制,可以自選(種類繁多,花樣不勝枚舉),這里直接選造好的SpringBoot的Dashboard導入(拿來即用)
直接地址欄輸入
https://grafana.com/grafana/dashboards/6756 或者 6756,然后點擊load。
此時點擊Dashboard :