圖文并茂,新手入門教程,建議收藏
SpringBoot2.x整合Prometheus+Grafana【附源碼】
附源碼+視頻
@[toc]
工程簡介
總體框架圖
[圖片上傳失敗...(image-f44049-1655265347123)]
簡介
Prometheus
Prometheus,是一個開源的系統監控和告警的工具包,其采用Pull方式采集時間序列的度量數據(也支持push方式),通過Http協議傳輸。它的工作方式是被監控的服務需要公開一個Prometheus端點,這端點是一個HTTP接口,該接口公開了度量的列表和當前的值,然后Prometheus應用從此接口定時拉取數據,一般可以存放在時序數據庫中,然后通過可視化的Dashboard(e.g.Grafana)進行數據展示。
支持的prometheus metrics
Counter,Gauge,Histogram,Summary,untyped等等。需要注意的是counter只能增不能減,適用于服務請求量,用戶訪問數等統計,但是如果需要統計有增有減的指標需要用Gauge。
exporter
支持的 exporter 很多,可以方便的監控很多應用,同時也可以自定義開發非官方提供的exporter。
grafana
grafana,是一個開源的dashboard展示工具,可以支持很多主流數據源,包括時序性的和非時序性的。其提供的展示配置以及可擴展性能滿足絕大部分時間序列數據展示需求,是一個比較優秀的工具。
支持的數據源
prometheus,inflexdb,elasticsearch,mysql,postgreSQL,openTSDB等,更多數據源:https://grafana.com/grafana/plugins/?type=datasource
SpringBoot工程初始化
springboot加速初始化:https://start.aliyun.com/
[圖片上傳失敗...(image-d88626-1655265347123)]
添加依賴
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
配置信息
在application.yml增加以下配置項
##SpringBoot2.x整合Prometheus+Grafana
##源碼:https://github.com/Rodert/SpringBoot-javapub
management:
metrics:
export:
prometheus:
enabled: true
step: 1m
descriptions: true
web:
server:
auto-time-requests: true
endpoints:
prometheus:
id: springmetrics
web:
exposure:
include: health,info,env,prometheus,metrics,httptrace,threaddump,heapdump,springmetrics
server:
port: 8080
啟動SpringBoot應用
http://localhost:8080/actuator/prometheus
打開即可看到暴露的信息
[圖片上傳失敗...(image-d1d6e9-1655265347123)]
環境安裝
如安裝包下載不成功,可以在公眾號回復【prometheus安裝包】or【grafana安裝包】領取
Prometheus安裝
下載地址:https://prometheus.io/download/
[圖片上傳失敗...(image-a35267-1655265347123)]
修改配置:
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'.
#暴露路徑
metrics_path: /actuator/prometheus
static_configs:
#SpringBoot的ip和端口號
- targets: ['localhost:8080']
啟動Prometheus
prometheus.exe
[圖片上傳失敗...(image-c61f6-1655265347123)]
測試訪問
[圖片上傳失敗...(image-50a99-1655265347123)]
jvm_memory_used_bytes
[圖片上傳失敗...(image-947aa1-1655265347123)]
Grafana安裝
下載地址:https://mirrors.huaweicloud.com/grafana/
[圖片上傳失敗...(image-35838d-1655265347123)]
啟動grafana
[圖片上傳失敗...(image-b9cd9a-1655265347123)]
測試
[圖片上傳失敗...(image-9eea5-1655265347123)]
默認賬號:admin 密碼:admin
[圖片上傳失敗...(image-c17eb-1655265347123)]
整合
增加數據源
[圖片上傳失敗...(image-70dd6a-1655265347123)]
[圖片上傳失敗...(image-55d082-1655265347123)]
- Name填一個
- URL填的Prometheus訪問地址
[圖片上傳失敗...(image-84ab3b-1655265347123)]
添加圖表
[圖片上傳失敗...(image-7c0e69-1655265347123)]
[圖片上傳失敗...(image-1eaecf-1655265347123)]
指定數據源、指定監控指標 jvm_memory_used_bytes
[圖片上傳失敗...(image-fac927-1655265347123)]
切換圖標
[圖片上傳失敗...(image-6c3013-1655265347123)]
[圖片上傳失敗...(image-758355-1655265347123)]
源碼地址:https://github.com/Rodert/spring-boot-prometheus-grafana
視頻地址:https://space.bilibili.com/404747369
延伸閱讀
- SpringBoot自定義注解
- SpringBoot整合docker入門
- SpringBoot整合ElasticSearch
- SpringBoot快速整合Excel
- SpringBoot整合MyBatis-支持批量更新
- SpringBoot實現鏈路追蹤spring-boot-trace
- SpringBoot2.x整合Prometheus+Grafana【附源碼】
中級篇
實戰篇
將支持:Activiti + Flowable 工作流; 第三方登錄; 支付; 短信; 支持 RBAC 動態權限、數據權限;監接;商城。SpringBoot Spring Security JWT MyBatis Druid Vue Vuex Element-ui Axios Sass Quill docker-compose、Kafka
[圖片上傳失敗...(image-af5787-1655265347123)]
<center>
<font face="黑體" color="red" size="4">關注公眾號,回復1024,獲取Java學習路線思維導圖、加入萬粉計劃學習交流群</font>
</center>