Spring Boot + Prometheus + Grafana 打造可視化監(jiān)控,一目了然!
互聯(lián)網(wǎng)架構(gòu)師后臺(tái)回復(fù) 2T 有特別禮包
上一篇:一邊裁員,一邊招人。。
作者:煙味i
鏈接:https://www.cnblogs.com/2YSP/p/12827487.html
一、背景

二、開發(fā)SpringBoot應(yīng)用
首先,創(chuàng)建一個(gè)SpringBoot項(xiàng)目,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>
<!-- https://mvnrepository.com/artifact/io.prometheus/simpleclient_spring_boot -->
<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>
MonitorDemoApplication啟動(dòng)類增加注解
package cn.sp;
import io.prometheus.client.spring.boot.EnablePrometheusEndpoint;
import io.prometheus.client.spring.boot.EnableSpringBootMetricsCollector;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnablePrometheusEndpoint
@EnableSpringBootMetricsCollector
@SpringBootApplication
public class MonitorDemoApplication {
public static void main(String[] args) {
SpringApplication.run(MonitorDemoApplication.class, args);
}
}
server:
port: 8848
spring:
application:
name: monitor-demo
security:
user:
name: admin
password: 1234
basic:
enabled: true
# 安全路徑列表,逗號(hào)分隔,此處只針對(duì)/admin路徑進(jìn)行認(rèn)證
path: /admin
# actuator暴露接口的前綴
management:
context-path: /admin
# actuator暴露接口使用的端口,為了和api接口使用的端口進(jìn)行分離
port: 8888
security:
enabled: true
roles: SUPERUSER
@RequestMapping("/heap/test")
@RestController
public class TestController {
public static final Map<String, Object> map = new ConcurrentHashMap<>();
@RequestMapping("")
public String testHeapUsed() {
for (int i = 0; i < 10000000; i++) {
map.put(i + "", new Object());
}
return "ok";
}
}

開始我的IDEA是不顯示這個(gè)Endpoints,后來發(fā)現(xiàn)是我使用的idea版本太老了,還是2017.1的, 而這個(gè)需要 idea2017.2版本以上才能看到。另外,搜索公眾號(hào)互聯(lián)網(wǎng)架構(gòu)師后臺(tái)回復(fù)“面試”,獲取一份驚喜禮包。
啟動(dòng)完畢,訪問http://localhost:8888/admin/prometheus就可以看到服務(wù)暴露的那些監(jiān)控指標(biāo)了。
注意:
由于開啟了安全認(rèn)證,所以訪問這個(gè)URL的需要提示輸入賬號(hào)/密碼,如果提示404請(qǐng)檢查下你的請(qǐng)求地址是否正確,如果不設(shè)置management.context-path則默認(rèn)地址是http://ip:port/prometheus
三、安裝Prometheus
下載地址點(diǎn)擊這里,本文下載的是Windows版本prometheus-2.17.2.windows-amd64.tar.gz。
解壓后修改prometheus.yml文件,配置數(shù)據(jù)采集的目標(biāo)信息。
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 # 刮取的時(shí)間間隔
scrape_timeout: 5s
metrics_path: /admin/prometheus
scheme: http
basic_auth: #認(rèn)證信息
username: admin
password: 1234
static_configs:
- targets:
- 127.0.0.1:8888 #此處填寫 Spring Boot 應(yīng)用的 IP + 端口號(hào)
現(xiàn)在可以啟動(dòng)Prometheus了,命令行輸入:prometheus.exe --config.file=prometheus.yml 訪問http://localhost:9090/targets,查看Spring Boot采集狀態(tài)是否正常。

四、安裝Grafana
下載地址點(diǎn)擊這里,本文用到的是Windows版本grafana-6.3.3.windows-amd64.zip。
解壓后運(yùn)行bin目錄下的grafana-server.exe啟動(dòng),游覽器訪問http://localhost:3000即可看到登錄頁面,默認(rèn)賬號(hào)密碼是admin/admin。
現(xiàn)在開始創(chuàng)建自己的可視化監(jiān)控面板。
1.設(shè)置數(shù)據(jù)源

2. 創(chuàng)建一個(gè)Dashboard



4.選擇圖表樣式


最后點(diǎn)擊右上角的保存,輸入Dashboad的名稱即可。

Tips: 這里的圖表布局是可以用鼠標(biāo)拖動(dòng)的
五、添加郵件報(bào)警

創(chuàng)建通道

第二步: 郵箱配置
#################################### SMTP / Emailing #####################
[smtp]
enabled = true
host = smtp.exmail.qq.com:465
user = [email protected]
# If the password contains # or ; you have to wrap it with triple quotes. Ex """#password;"""
password = XXX
cert_file =
key_file =
skip_verify = true
from_address = [email protected]
from_name = Grafana
ehlo_identity = ininin.com
然后需要重啟Grafana,命令grafana-server.exe -config=E:\file\grafana-6.3.3\conf\custom.ini
第三步: 為指標(biāo)添加alert

配置預(yù)警規(guī)則

配置通知方式和信息
Evaluate every
表示檢測(cè)評(píng)率,這里為了測(cè)試效果,改為1秒
For
如果警報(bào)規(guī)則配置了For,并且查詢違反了配置的閾值,那么它將首先從OK變?yōu)镻ending。從OK到Pending Grafana不會(huì)發(fā)送任何通知。另外,搜索公眾號(hào)互聯(lián)網(wǎng)架構(gòu)師后臺(tái)回復(fù)“面試”,獲取一份驚喜禮包。一旦警報(bào)規(guī)則的觸發(fā)時(shí)間超過持續(xù)時(shí)間,它將更改為Alerting并發(fā)送警報(bào)通知。
Conditions
when 表示什么時(shí)間,of 表示條件,is above 表示觸發(fā)值 同時(shí),設(shè)置了is above后會(huì)有一條紅線。
If no data or all values are null
如果沒有數(shù)據(jù)或所有值都為空,這里選擇觸發(fā)報(bào)警
If execution error or timeout
如果執(zhí)行錯(cuò)誤或超時(shí),這里選擇觸發(fā)報(bào)警
注意: 下一次觸發(fā),比如10秒后,它不會(huì)再次觸發(fā),防止報(bào)警風(fēng)暴產(chǎn)生!
第四步: 測(cè)試
請(qǐng)求http://localhost:8848/heap/test接口后,內(nèi)存升高大于設(shè)置的閾值,然后就收到報(bào)警郵件。

這里圖片沒有顯示出來,搞不懂為什么。
六、總結(jié)
這套監(jiān)控功能還是挺強(qiáng)大的,就是Prometheus的表達(dá)式有點(diǎn)多。
感謝您的閱讀,也歡迎您發(fā)表關(guān)于這篇文章的任何建議,關(guān)注我,技術(shù)不迷茫!小編到你上高速。
正文結(jié)束
1.心態(tài)崩了!稅前2萬4,到手1萬4,年終獎(jiǎng)扣稅方式1月1日起施行~
2.深圳一普通中學(xué)老師工資單曝光,秒殺程序員,網(wǎng)友:敢問是哪個(gè)學(xué)校畢業(yè)的?
3.從零開始搭建創(chuàng)業(yè)公司后臺(tái)技術(shù)棧
5.清華大學(xué):2021 元宇宙研究報(bào)告!
6.為什么國(guó)內(nèi) 996 干不過國(guó)外的 955呢?

