1.應(yīng)用監(jiān)控系統(tǒng)介紹
SpringBoot的應(yīng)用監(jiān)控方案比較多,SpringBoot+Prometheus+Grafana是比較常用的一種解決方案,主要的監(jiān)控數(shù)據(jù)的處理邏輯如下:
- SpringBoot 的 actuator 提供了應(yīng)用監(jiān)控端點,可以對外暴露監(jiān)控數(shù)據(jù)信息。
- Prometheus 是監(jiān)控系統(tǒng),可以從 Springboot 采集監(jiān)控數(shù)據(jù),以時序數(shù)據(jù)的形式存儲,并對外提供了監(jiān)控數(shù)據(jù)查詢服務(wù)。
- Grafana 是專業(yè)的 UI 儀表盤系統(tǒng),支持非常多的數(shù)據(jù)源,自然也支持Prometheus,可以對接Prometheus,從其中獲取數(shù)據(jù),使用儀表盤展示出來。
springboot 2.X 中引入了 micrometer,它可以更方便的對接各種監(jiān)控系統(tǒng),包括 Prometheus。
2.軟件版本說明
- jdk >= 1.8.0
- springboot == 2.7.11
- prometheus == 2.45.2
- grafana == 10.1.0
3. 準(zhǔn)備SpringBoot項目
創(chuàng)建一個Springboot項目, SpringBoot版本選擇2.7.11
, 項目的pom.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.beyond</groupId>
<artifactId>beyond-monitor-admin</artifactId>
<packaging>jar</packaging>
<name>beyond-monitor-admin</name>
<description>Demo project for Spring Boot</description>
<properties>
<springboot.version>2.7.11</springboot.version>
<prometheus.version>1.9.10</prometheus.version>
</properties>
<dependencies>
<!-- SpringWeb模塊 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 對外暴露監(jiān)控端點 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 集成prometheus定時爬取web服務(wù)監(jiān)控端點數(shù)據(jù) -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<version>${prometheus.version}</version>
</dependency>
</dependencies>
<!-- 依賴聲明 -->
<dependencyManagement>
<dependencies>
<!-- SpringBoot的依賴配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<fork>true</fork> <!-- 如果沒有該配置,devtools不會生效 -->
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
配置文件application.yml
文件配置如下:
server:
port: 19090
spring:
application:
name: beyond-monitor-admin
profiles:
active: dev
logging:
config: classpath:logback-plus.xml
--- # Actuator 監(jiān)控端點的配置項
management:
endpoints:
enabled-by-default: true #暴露所有端點信息
web:
exposure:
# 這里可以指定暴露端點的范圍,示例: health,info,env,prometheus,metrics,httptrace,threaddump,heapdump,springmetrics
include: '*'
endpoint:
health:
show-details: ALWAYS
logfile:
external-file: ./logs/beyond-monitor-admin.log
啟動程序,訪問http://localhost:19090/actuator/prometheus就可以看到服務(wù)暴露的那些監(jiān)控指標(biāo)了。
OK, 這樣我們的web項目配置暴露監(jiān)控指標(biāo)就配置好了, 下面我們繼續(xù)進(jìn)行Prometheus
和Grafana
的安裝。
4.應(yīng)用安裝
4.1.Prometheus下載安裝
本文下載的是linux版本的最新穩(wěn)定版本prometheus-2.45.2.linux-amd64.tar.gz
。
下載地址:https://prometheus.io/download/
下載prometheus-2.45.2
linux系統(tǒng)64位的安裝壓縮包,上傳到服務(wù)器。
將上傳的安裝壓縮包解壓縮到指定的安裝目錄下
重命名應(yīng)用名稱為prometheus-2.45.2
, 個人習(xí)慣, 可以跳過。
通過以上步驟,prometheus-2.45.2就安裝好了。
4.2.Grafana下載安裝
本文下載的是linux版本的最新穩(wěn)定版本grafana-10.1.0.linux-amd64.tar.gz
。
下載地址:https://grafana.com/grafana/download
下載grafana-10.1.0
linux系統(tǒng)64位的安裝壓縮包,上傳到服務(wù)器。
將上傳的安裝壓縮包解壓縮到指定的安裝目錄下
通過以上步驟,grafana-10.1.0就安裝好了。
5.Prometheus配置啟動
因為我們需要使用Prometheus
從Springboot應(yīng)用暴露的監(jiān)控指標(biāo)中進(jìn)行數(shù)據(jù)采集,所以需要再Prometheus
的配置文件prometheus.yml
文件中配置數(shù)據(jù)采集的目標(biāo)信息。
vim prometheus.yml
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
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: ["localhost:9090"]
# 配置應(yīng)用監(jiān)控服務(wù)的相關(guān)信息, prometheus會從配置的應(yīng)用服務(wù)暴露的監(jiān)控端點獲取監(jiān)控數(shù)據(jù),如果有多個應(yīng)用可以配置多個job
- job_name: 'beyond-monitor-admin'
scrape_interval: 5s # 刮取的時間間隔
scrape_timeout: 5s
metrics_path: /actuator/prometheus
scheme: http
#basic_auth: #認(rèn)證信息(如果上線生產(chǎn)環(huán)境,admin項目最好配置安全認(rèn)證, 這里配置認(rèn)證賬號和密碼)
# username: admin
# password: admin
static_configs:
- targets: ["10.10.3.169:19090"] #此處填寫 Spring Boot 應(yīng)用的 IP + 端口號
更多配置信息請查看官方文檔prometheus配置。
現(xiàn)在可以啟動Prometheus了,啟動命令如下:
./prometheus --config.file=prometheus.yml
如下圖:說明啟動成功
訪問http://10.10.3.14:9090/targets
,將IPt替換成你的Prometheus服務(wù)所在那臺機(jī)器的IP,查看Spring Boot采集狀態(tài)是否正常,如下圖,說明Prometheus對接你的web應(yīng)用成功,可以成功采集到監(jiān)控信息。
6.Grafana配置啟動
6.1.啟動Grafana服務(wù)
# 進(jìn)入到grafana的安裝目錄下的bin目錄
cd /home/software/grafana-10.1.0/bin
# 啟動grafana-server服務(wù)
nohup ./grafana-server &
當(dāng)然你也可以直接將grafana-server配置到系統(tǒng)環(huán)境變量, 方便隨時啟動不用切換路徑到grafana的安裝目錄下的bin目錄,這個可以自行操作。
通過瀏覽器訪問grafana服務(wù),http://10.10.3.14:3000
, 默認(rèn)的登錄賬號密碼是
賬號:
admin
密碼:admin
使用默認(rèn)賬號密碼登錄成功過之后,系統(tǒng)要求立即修改密碼,如下圖:
修改密碼保存之后就進(jìn)入系統(tǒng)首頁了
6.2.配置Grafana可視化圖表
6.2.1.配置數(shù)據(jù)源
在配置Grafana可視化圖表之前需要先配置數(shù)據(jù)源, Grafana支持很多種數(shù)據(jù)源對接,這里我們配置
Prometheus數(shù)據(jù)源。
點擊添加數(shù)據(jù)源
選擇Prometheus數(shù)據(jù)源
配置Prometheus的應(yīng)用訪問URL地址信息
如下圖:說明數(shù)據(jù)源配置正??稍L問, 如果失敗, 大概率就是以下幾種情況導(dǎo)致的:
- Prometheus服務(wù)未正常啟動
- Prometheus服務(wù)url信息配置填寫錯誤
- 兩個服務(wù)安裝在不同的服務(wù)器,主機(jī)之間的網(wǎng)絡(luò)不通
- 兩個服務(wù)安裝在不同的服務(wù)器,防火墻服務(wù)啟動,服務(wù)無法通信
一般錯誤就是這幾種情況,一一排查基本就能解決。
之后再數(shù)據(jù)源列表就可以看到我們配置的Prometheus數(shù)據(jù)源
6.2.2.配置可視化監(jiān)控面板
對于這塊兒, 我是真不擅長, 但是Grafana有一個網(wǎng)站上提供了免費(fèi)的可視化模板可以下載, 訪問地址是:https://grafana.com/grafana/dashboards/
這里我們搜索Spring
,
找一個你喜歡的模板, 點擊Download JSON
然后再Grafana上點擊Dashboards
菜單。如下圖:
選擇從外部導(dǎo)入
上傳可視化面板的JSON文件并導(dǎo)入
載入可視化面板模板如下:
保存可視化面板
可視化面板入口一
可視化面板入口二文章來源:http://www.zghlxwxcb.cn/news/detail-813615.html
通過以上操作, 我們的SpringBoot+Prometheus+Grafana搭建應(yīng)用監(jiān)控系統(tǒng)就搭建起來了。
好了,本篇文章到此結(jié)束,如果安裝搭建過程中遇到問題,歡迎留言討論哈,最后如果文章對你有幫助,請點贊關(guān)注,送你一朵小紅花,謝謝~~~。文章來源地址http://www.zghlxwxcb.cn/news/detail-813615.html
到了這里,關(guān)于SpringBoot+Prometheus+Grafana搭建應(yīng)用監(jiān)控系統(tǒng)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!