SpringBoot整合Prometheus構(gòu)建高效、靈活的監(jiān)控系統(tǒng)
對于任何一個應(yīng)用系統(tǒng)來說,監(jiān)控都是至關(guān)重要的一個組成部分。如果應(yīng)用系統(tǒng)在運(yùn)行過程當(dāng)中出現(xiàn)故障(如網(wǎng)絡(luò)中斷、內(nèi)存溢出、磁盤空間不足、軟件自身Bug),任何一個方面出現(xiàn)問題都有可能影響到整個系統(tǒng)的正常運(yùn)行,甚至給企業(yè)帶來資損,因此監(jiān)控必不可少。那么SpringBoot項目如何實(shí)現(xiàn)一套完整、成熟的監(jiān)控系統(tǒng)呢?
首先,SpringBoot是一個可以快速創(chuàng)建獨(dú)立、可運(yùn)行的、生產(chǎn)級別的Spring應(yīng)用的項目框架,Prometheus是一個開源的監(jiān)控和告警工具,廣泛應(yīng)用于微服務(wù)架構(gòu)的監(jiān)控。將SpringBoot應(yīng)用與Prometheus整合,可以實(shí)現(xiàn)應(yīng)用的實(shí)時監(jiān)控和性能分析,幫助開發(fā)人員快速定位和解決問題。下面我們來實(shí)現(xiàn)SpringBoot整合prometheus。
1、SpringBoot項目添加相關(guān)的依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--prometheus-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
</dependencies>
2、SpringBoot項目添加yml配置
server:
port: 8081
spring:
application:
name: prometheus-demo
#prometheus
management:
endpoints:
web:
exposure:
include: '*'
在Springboot整合Prometheus的時候,項目啟動后會開啟Actuator服務(wù),SpringBoot Actuator會自動配置一個URL為 /actuator/Pprometheus的http服務(wù)來供prometheus抓取數(shù)據(jù)。
3、添加測試controller
@RestController
@RequestMapping("/test")
public class OrderController {
@GetMapping("/test1")
public String test1(){
return "success";
}
@GetMapping("/test2")
public String test2(){
List<byte[]> byteList = new ArrayList<>();
while (true){
byteList.add(new byte[1024*1024*2]);
try {
Thread.sleep(2000);
} catch (InterruptedException e){
}
}
}
}
4、啟動Prometheus后檢查springBoot項目是否正常
在SpringBoot配置好Prometheus配置文件后,啟動SpringBoot項目,我們需要檢查項目在Prometheus上的狀態(tài)是否正常,如下圖Springboot項目的啟動圖:
待Springboot項目啟動完成后,在Prometheus中查詢Springboot項目的監(jiān)控狀態(tài),如下圖所示的正常監(jiān)控狀態(tài):
圖片
項目在Prometheus上的狀態(tài)是綠色表示是正常的狀態(tài)。
5、prometheus的配置文件中添加SpringBoot監(jiān)控任務(wù)
圖片
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ["Prometheus_ip:9090"]
- job_name: 'springboot-prometheus'
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ["springboot項目的ip:8081"]
6、Grafana配置prometheus數(shù)據(jù)源
6.1 添加prometheus
圖片
6.2 配置prometheus
圖片
點(diǎn)擊Save&Test可以看到如下的提示就是表示添加Prometheus數(shù)據(jù)源成功:
圖片
6.3 挑選可視化的dashborad
通過訪問https://grafana.com/grafana/dashboards這個鏈接去挑選可視化的dashborad,下圖是一個典型的SpringBoot項目的監(jiān)控可視化的dashborad:
圖片
然后獲取dashborad的編號:
圖片
圖片
6.4 添加dashborad到Grafana中
在Grafana中添加dashborad,如下圖所示:
圖片
輸入dashborad編號
圖片
然后選擇prometheus數(shù)據(jù)源
圖片
配置好之后可以就看到Granafa的監(jiān)控面板
圖片
7、測試監(jiān)控
圖片
請求執(zhí)行死循環(huán)的代碼,我們可以發(fā)現(xiàn)請求正在被執(zhí)行,如下圖所示:
圖片
執(zhí)行一段時間后,再觀察Granafa監(jiān)控面板的效果:
圖片
通過監(jiān)控我們發(fā)現(xiàn)內(nèi)消耗一直不斷增加,代碼出現(xiàn)問題我們可以第一時間快速的發(fā)現(xiàn),然后定位解決。
總結(jié):
(1)SpringBoot整合Prometheus的原理是SpringBoot應(yīng)用將metrics數(shù)據(jù)上報給Prometheus,數(shù)據(jù)上報有基于Pushgateway方式和直接上報方式。
(2)基于Pushgateway方式適用于短期運(yùn)行的應(yīng)用,將metrics數(shù)據(jù)推送到Pushgateway中,由Pushgateway統(tǒng)一推送至Prometheus。
(3)直接上報方式適用于長期運(yùn)行的應(yīng)用,將metrics數(shù)據(jù)直接上報給Prometheus中。直接上報方式是通過配置文件的配置,將SpringBoot應(yīng)用會把metrics數(shù)據(jù)暴露在/actuator/prometheus路徑下,Prometheus通過抓取該路徑的數(shù)據(jù)即可獲取應(yīng)用的metrics數(shù)據(jù)。