Spring Boot應(yīng)用與Prometheus和Grafana整合方案
SpringBoot監(jiān)控終極方案:整合Prometheus+Grafana
一、引言
在當(dāng)今的軟件開發(fā)領(lǐng)域,Spring Boot 以其快速開發(fā)、簡化配置等優(yōu)勢(shì),成為了構(gòu)建微服務(wù)架構(gòu)的熱門選擇。然而,隨著應(yīng)用的不斷上線和規(guī)模的逐漸擴(kuò)大,對(duì)應(yīng)用進(jìn)行有效的監(jiān)控變得至關(guān)重要。監(jiān)控不僅可以幫助我們及時(shí)發(fā)現(xiàn)系統(tǒng)中的問題,還能為系統(tǒng)的優(yōu)化和性能調(diào)優(yōu)提供有力的數(shù)據(jù)支持。
Prometheus 是一款開源的監(jiān)控系統(tǒng)和時(shí)間序列數(shù)據(jù)庫,它具有強(qiáng)大的查詢語言和靈活的告警機(jī)制。Grafana 則是一個(gè)開源的可視化工具,能夠?qū)?Prometheus 收集到的數(shù)據(jù)以直觀的圖表和儀表盤的形式展示出來。本文將詳細(xì)介紹如何將 Spring Boot 應(yīng)用與 Prometheus 和 Grafana 進(jìn)行整合,實(shí)現(xiàn)對(duì) Spring Boot 應(yīng)用的全面監(jiān)控。
二、準(zhǔn)備工作
2.1 環(huán)境要求
- JDK 1.8 及以上
- Maven 3.x 及以上
- Spring Boot 2.x 及以上
- Docker(可選,用于快速部署 Prometheus 和 Grafana)
2.2 創(chuàng)建 Spring Boot 項(xiàng)目
我們可以使用 Spring Initializr(https://start.spring.io/) 來快速創(chuàng)建一個(gè) Spring Boot 項(xiàng)目,添加以下依賴:
<dependencies>
<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-boot-starter-web 用于創(chuàng)建一個(gè)簡單的 Web 應(yīng)用,micrometer-registry-prometheus 用于將 Spring Boot 應(yīng)用的指標(biāo)暴露給 Prometheus。
三、Spring Boot 應(yīng)用配置
3.1 配置 Micrometer
在 application.properties 或 application.yml 中添加以下配置:
management.endpoints.web.exposure.include=* management.metrics.tags.application=my-spring-boot-app
或者使用 YAML 格式:
management:
endpoints:
web:
exposure:
include: '*'
metrics:
tags:
application: my-spring-boot-appmanagement.endpoints.web.exposure.include=*:將所有的管理端點(diǎn)暴露出來,包括 Prometheus 指標(biāo)端點(diǎn)。management.metrics.tags.application=my-spring-boot-app:為所有的指標(biāo)添加一個(gè)application標(biāo)簽,方便在 Prometheus 中進(jìn)行篩選和查詢。
3.2 創(chuàng)建簡單的 Web 服務(wù)
創(chuàng)建一個(gè)簡單的控制器類:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}3.3 啟動(dòng) Spring Boot 應(yīng)用
啟動(dòng) Spring Boot 應(yīng)用后,訪問 http://localhost:8080/actuator/prometheus,可以看到 Prometheus 格式的指標(biāo)數(shù)據(jù)。
四、Prometheus 配置與部署
4.1 下載和啟動(dòng) Prometheus
可以從 Prometheus 官方網(wǎng)站(https://prometheus.io/download/) 下載適合自己操作系統(tǒng)的版本。下載完成后,解壓文件,進(jìn)入解壓后的目錄,編輯 prometheus.yml 配置文件:
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: 'spring-boot-app'
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['localhost:8080']scrape_interval和evaluation_interval:分別表示數(shù)據(jù)采集間隔和規(guī)則評(píng)估間隔,這里設(shè)置為 15 秒。scrape_configs:定義了要采集的目標(biāo),job_name為任務(wù)名稱,metrics_path為指標(biāo)數(shù)據(jù)的訪問路徑,targets為要采集的目標(biāo)地址。
啟動(dòng) Prometheus:
./prometheus --config.file=prometheus.yml
4.2 使用 Docker 部署 Prometheus
如果使用 Docker 部署 Prometheus,可以使用以下命令:
docker run -d -p 9090:9090 -v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
其中 /path/to/prometheus.yml 為本地 prometheus.yml 文件的路徑。
4.3 驗(yàn)證 Prometheus 配置
訪問 http://localhost:9090,可以看到 Prometheus 的 Web 界面。在搜索框中輸入 up,點(diǎn)擊 Execute 按鈕,如果看到 spring-boot-app 對(duì)應(yīng)的指標(biāo)值為 1,表示 Prometheus 已經(jīng)成功采集到 Spring Boot 應(yīng)用的指標(biāo)數(shù)據(jù)。
五、Grafana 配置與部署
5.1 下載和啟動(dòng) Grafana
可以從 Grafana 官方網(wǎng)站(https://grafana.com/grafana/download) 下載適合自己操作系統(tǒng)的版本。下載完成后,啟動(dòng) Grafana 服務(wù):
- 在 Linux 系統(tǒng)上:
sudo systemctl start grafana-server
- 在 Windows 系統(tǒng)上,直接運(yùn)行
grafana-server.exe。
5.2 使用 Docker 部署 Grafana
使用 Docker 部署 Grafana 更加方便:
docker run -d -p 3000:3000 grafana/grafana
5.3 配置 Grafana 數(shù)據(jù)源
訪問 http://localhost:3000,使用默認(rèn)用戶名 admin 和密碼 admin 登錄 Grafana。登錄后,點(diǎn)擊左側(cè)菜單欄的 Configuration -> Data Sources,點(diǎn)擊 Add data source,選擇 Prometheus,在 URL 字段中輸入 http://localhost:9090,然后點(diǎn)擊 Save & Test,如果提示 Data source is working,表示數(shù)據(jù)源配置成功。
5.4 創(chuàng)建 Grafana 儀表盤
點(diǎn)擊左側(cè)菜單欄的 + -> Dashboard,點(diǎn)擊 Add a new panel,在 Query 標(biāo)簽頁中選擇之前配置的 Prometheus 數(shù)據(jù)源,輸入查詢語句,例如 http_server_requests_seconds_count,然后點(diǎn)擊 Apply,可以看到相應(yīng)的指標(biāo)數(shù)據(jù)以圖表的形式展示出來??梢愿鶕?jù)需要對(duì)圖表進(jìn)行進(jìn)一步的配置和調(diào)整,如設(shè)置圖表類型、時(shí)間范圍等。
六、高級(jí)配置與優(yōu)化
6.1 自定義指標(biāo)
在 Spring Boot 應(yīng)用中,可以使用 Micrometer 自定義指標(biāo)。例如,創(chuàng)建一個(gè)自定義的計(jì)數(shù)器:
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
public class CustomMetrics {
private final MeterRegistry meterRegistry;
private Counter customCounter;
public CustomMetrics(MeterRegistry meterRegistry) {
this.meterRegistry = meterRegistry;
}
@PostConstruct
public void init() {
customCounter = meterRegistry.counter("custom_counter");
}
public void incrementCustomCounter() {
customCounter.increment();
}
}在控制器中使用自定義指標(biāo):
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CustomMetricsController {
@Autowired
private CustomMetrics customMetrics;
@GetMapping("/increment")
public String increment() {
customMetrics.incrementCustomCounter();
return "Custom counter incremented!";
}
}6.2 告警配置
在 Prometheus 中可以配置告警規(guī)則。編輯 prometheus.yml 文件,添加以下告警規(guī)則:
rule_files: - 'alert.rules.yml'
創(chuàng)建 alert.rules.yml 文件:
groups:
- name: spring-boot-alerts
rules:
- alert: HighRequestRate
expr: rate(http_server_requests_seconds_count[5m]) > 100
for: 5m
labels:
severity: critical
annotations:
summary: "High request rate detected"
description: "The request rate of the Spring Boot application has exceeded 100 requests per second for the last 5 minutes."在 Grafana 中配置告警通知渠道,如郵件、Slack 等。點(diǎn)擊左側(cè)菜單欄的 Configuration -> Alerting,選擇相應(yīng)的通知渠道進(jìn)行配置。
七、總結(jié)
通過將 Spring Boot 應(yīng)用與 Prometheus 和 Grafana 進(jìn)行整合,我們可以實(shí)現(xiàn)對(duì) Spring Boot 應(yīng)用的全面監(jiān)控和可視化展示。Prometheus 負(fù)責(zé)收集和存儲(chǔ)應(yīng)用的指標(biāo)數(shù)據(jù),Grafana 則將這些數(shù)據(jù)以直觀的圖表和儀表盤的形式展示出來,方便我們及時(shí)發(fā)現(xiàn)系統(tǒng)中的問題和進(jìn)行性能調(diào)優(yōu)。同時(shí),通過自定義指標(biāo)和告警配置,我們可以根據(jù)實(shí)際需求對(duì)監(jiān)控系統(tǒng)進(jìn)行進(jìn)一步的擴(kuò)展和優(yōu)化。
到此這篇關(guān)于Spring Boot應(yīng)用與Prometheus和Grafana整合方案的文章就介紹到這了,更多相關(guān)springboot prometheus+grafana整合內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring中11個(gè)最常用的擴(kuò)展點(diǎn)總結(jié),你知道幾個(gè)
我們知道IOC(控制反轉(zhuǎn))和AOP(面向切面編程)是spring的基石,除此之外spring的擴(kuò)展能力非常強(qiáng),下面這篇文章主要給大家介紹了關(guān)于Spring中11個(gè)最常用的擴(kuò)展點(diǎn)的相關(guān)資料,需要的朋友可以參考下2022-12-12
Java調(diào)用DeepSeek?API的8個(gè)高頻坑與解決方法
現(xiàn)在大模型開發(fā)特別火,DeepSeek?因?yàn)橹形睦斫夂?、反?yīng)快、還便宜,不少?Java?開發(fā)者都用它,本文整理了最常踩的?8?個(gè)坑,希望對(duì)大家有所幫助2025-12-12
jar包運(yùn)行一段時(shí)間后莫名其妙掛掉線上問題及處理方案
這篇文章主要介紹了jar包運(yùn)行一段時(shí)間后莫名其妙掛掉線上問題及處理方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
SpringBoot熱部署啟動(dòng)關(guān)閉流程詳解
Spring?Boot啟動(dòng)熱部署是一種技術(shù),它能讓開發(fā)者在不重啟應(yīng)用程序的情況下實(shí)時(shí)更新代碼。這樣可以提高開發(fā)效率,避免頻繁重啟應(yīng)用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-04-04
IDEA連接mysql數(shù)據(jù)庫報(bào)錯(cuò)的解決方法
這篇文章主要介紹了IDEA連接mysql數(shù)據(jù)庫報(bào)錯(cuò)的解決方法,文中有非常詳細(xì)的圖文示例,對(duì)出現(xiàn)Server returns invalid timezone. Go to ‘Advanced‘ tab and set ‘serverTimezone‘ prope報(bào)錯(cuò)的小伙伴們很有幫助喲,需要的朋友可以參考下2021-05-05
SpringBoot2零基礎(chǔ)到精通之?dāng)?shù)據(jù)與頁面響應(yīng)
SpringBoot是一種整合Spring技術(shù)棧的方式(或者說是框架),同時(shí)也是簡化Spring的一種快速開發(fā)的腳手架2022-03-03
完美解決java.lang.OutOfMemoryError處理錯(cuò)誤的問題
下面小編就為大家?guī)硪黄昝澜鉀Qjava.lang.OutOfMemoryError處理錯(cuò)誤的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
基于Java實(shí)現(xiàn)動(dòng)態(tài)切換ubuntu壁紙功能
這篇文章主要為大家詳細(xì)介紹了如何使用 Java 在 Ubuntu Linux 系統(tǒng)中實(shí)現(xiàn)自動(dòng)切換壁紙的示例程序,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-11-11

