close
關注我,回復關鍵字「spring」,
免費領取Spring學習資料。

1.背景
SpringBoot的應用監控方案比較多,SpringBoot+Prometheus+Grafana是目前比較常用的方案之一。它們三者之間的關係大概如下圖:
關係圖
2.開發SpringBoot應用
首先,創建一個SpringBoot項目,pom文件如下:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>io.prometheus</groupId><artifactId>simpleclient_spring_boot</artifactId><version>0.8.1</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency>
注意:這裡的SpringBoot版本是1.5.7.RELEASE,之所以不用最新的2.X是因為最新的simpleclient_spring_boot只支持1.5.X,不確定2.X版本的能否支持。
MonitorDemoApplication啟動類增加註解
@EnablePrometheusEndpoint@EnableSpringBootMetricsCollector@SpringBootApplicationpublic class MonitorDemoApplication {publicstaticvoidmain(String[] args) {SpringApplication.run(MonitorDemoApplication.class, args);}}
配置文件application.yml
server:port:8848spring:application:name: monitor-demosecurity:user:name: adminpassword:1234basic:enabled: true# 安全路徑列表,逗號分隔,此處只針對/admin路徑進行認證path: /admin# actuator暴露接口的前綴management:context-path: /admin# actuator暴露接口使用的端口,為了和api接口使用的端口進行分離port:8888security:enabled: trueroles: SUPERUSER
測試代碼TestController
@RequestMapping("/heap/test")@RestControllerpublicclassTestController {publicstaticfinal Map<String,Object> map =newConcurrentHashMap<>();@RequestMapping("")publicStringtestHeapUsed() {for(int i =0; i <10000000; i++) {map.put(i +"",newObject());}return"ok";}}
這裡的邏輯就是在請求這個接口後,創建大量對象保存到map中增加堆內存使用量,方便後面測試郵件報警。
啟動項目後,可以在IDEA中看到有很多Endpoints,如圖:
開始我的IDEA是不顯示這個Endpoints,後來發現是我使用的idea版本太老了,還是2017.1的,
而這個需要 idea2017.2版本以上才能看到。
後來只好重新下載安裝,弄了好久。
啟動完畢,訪問http://localhost:8888/admin/prometheus就可以看到服務暴露的那些監控指標了。
監控指標
注意:
由於開啟了安全認證,所以訪問這個URL的需要提示輸入賬號/密碼,如果提示404請檢查下你的請求地址是否正確,如果不設置management.context-path則默認地址是http://ip:port/prometheus
3.安裝Prometheus
本文下載的是Windows版本prometheus-2.17.2.windows-amd64.tar.gz。
下載地址:https://prometheus.io/download/
解壓後修改prometheus.yml文件,配置數據採集的目標信息。
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']-job_name:'monitor-demo'scrape_interval:5s# 刮取的時間間隔scrape_timeout:5smetrics_path: /admin/prometheusscheme: httpbasic_auth: #認證信息username: adminpassword:1234static_configs:-targets:-127.0.0.1:8888#此處填寫 Spring Boot 應用的 IP + 端口號
更多配置信息請查看官方文檔。
現在可以啟動Prometheus了,命令行輸入:
prometheus.exe--config.file=prometheus.yml
訪問http://localhost:9090/targets,查看Spring Boot採集狀態是否正常。
採集目標信息
4.安裝Grafana
本文用到的是Windows版本grafana-6.3.3.windows-amd64.zip。
下載地址:https://grafana.com/grafana/download
解壓後運行bin目錄下的grafana-server.exe啟動,遊覽器訪問http://localhost:3000即可看到登錄頁面,默認賬號密碼是admin/admin。
現在開始創建自己的可視化監控面板。
1.設置數據源
2. 創建一個Dashboard
3. 填寫採集的指標點
注意:這裡的指標點不能隨便填,必須是已有的可以在 Prometheus看到。
4.選擇圖表樣式
5.填寫標題描述
最後點擊右上角的保存,輸入Dashboad的名稱即可。
Tips: 這裡的圖表布局是可以用鼠標拖動的
5.添加郵件報警
在實際項目中當監控的某的個指標超過閾值(比如CPU使用率過高),希望監控系統自動通過短信、釘釘和郵件等方式報警及時通知運維人員,Grafana就支持該功能。
第一步:點擊[Alerting]——>[Notification channels]添加通知通道
這裡的Type有很多選項,包括webhook、釘釘等,這裡以郵件為例。
第二步:郵箱配置
Grafana默認使用conf目錄下defaults.ini作為配置文件運行,根據官方的建議我們不要更改defaults.ini而是在同級目錄下新建一個配置文件custom.ini。
以騰訊企業郵箱為例,配置如下:
################### SMTP / Emailing###################[smtp]enabled=truehost= smtp.exmail.qq.com:465user= xxxx@ininin.com# If the password contains # or ; you have to wrap it with triple quotes. Ex """#password;"""password= XXXcert_file=key_file=skip_verify=truefrom_address= xxxx@ininin.comfrom_name= Grafanaehlo_identity= ininin.com
然後需要重啟Grafana,命令
grafana-server.exe-config=E:\file\grafana-6.3.3\conf\custom.ini
第三步:為指標添加alert
功能說明:
Evaluate every
表示檢測評率,這裡為了測試效果,改為1秒
For
如果警報規則配置了For,並且查詢違反了配置的閾值,那麼它將首先從OK變為Pending。從OK到Pending Grafana不會發送任何通知。一旦警報規則的觸發時間超過持續時間,它將更改為Alerting並發送警報通知。
Conditions
when 表示什麼時間,of 表示條件,is above 表示觸發值
同時,設置了is above後會有一條紅線。
If no data or all values are null
如果沒有數據或所有值都為空,這裡選擇觸發報警
If execution error or timeout
如果執行錯誤或超時,這裡選擇觸發報警
注意:下一次觸發,比如10秒後,它不會再次觸發,防止報警風暴產生!
第四步:測試
請求http://localhost:8848/heap/test接口後,內存升高大於設置的閾值,然後就收到報警郵件。
報警郵件
這裡圖片沒有顯示出來,搞不懂為什麼。
6.總結
這套監控功能還是挺強大的,就是Prometheus的表達式有點多。
文檔:
https://prometheus.io/docs/introduction/first_steps/
https://grafana.com/docs/grafana/latest/
https://github.com/2YSP/monitor-demo
來源:https://www.cnblogs.com/2YSP/p/12827487.html

END


Spring Framework 6.0 正式 GA,新一代框架的開始
Spring Boot + Redis 實現 API 接口防刷限流
Spring Boot 關於日期時間格式化處理方式總結
Spring Boot 最流行的 16 條最佳實踐

關注後端面試那些事,回復【2022面經】

獲取最新大廠Java面經


最後重要提示:高質量的技術交流群,限時免費開放,今年抱團最重要。想進群的,關注SpringForAll社區,回復關鍵詞:加群,拉你進群。




點擊這裡領取2022大廠面經
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 鑽石舞台 的頭像
    鑽石舞台

    鑽石舞台

    鑽石舞台 發表在 痞客邦 留言(0) 人氣()