Spring Boot 實(shí)現(xiàn)應(yīng)用監(jiān)控和報(bào)警

Spring Boot 的應(yīng)用監(jiān)控方案比較多,Spring Boot+Prometheus+Grafana是目前比較常用的方案之一。它們?nèi)咧g的關(guān)系大概如下圖:

01 開發(fā) Spring Boot 應(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>注意: 這里的 Spring Boot 版本是 1.5.7.RELEASE,之所以不用最新的2.X是因?yàn)樽钚碌?simpleclient_spring_boot 只支持1.5.X,不確定2.X版本的能否支持。
MonitorDemoApplication 啟動(dòng)類增加注解:
@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)分隔,此處只針對/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";
}
}

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)




4.選擇圖表樣式





第二步: 郵箱配置
Grafana默認(rèn)使用conf目錄下defaults.ini作為配置文件運(yùn)行,根據(jù)官方的建議我們不要更改defaults.ini而是在同級(jí)目錄下新建一個(gè)配置文件custom.ini。
以騰訊企業(yè)郵箱為例,配置如下:
#################################### 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

配置通知方式和信息


附上幾個(gè)鏈接:
Prometheus官方文檔:
推薦閱讀:
世界的真實(shí)格局分析,地球人類社會(huì)底層運(yùn)行原理
企業(yè)IT技術(shù)架構(gòu)規(guī)劃方案
華為內(nèi)網(wǎng)最火的文章:什么是內(nèi)卷?
不是你需要中臺(tái),而是一名合格的架構(gòu)師(附各大廠中臺(tái)建設(shè)PPT)
【中臺(tái)實(shí)踐】華為大數(shù)據(jù)中臺(tái)架構(gòu)分享.pdf
阿里達(dá)摩院《機(jī)器學(xué)習(xí)算法學(xué)習(xí)指南》火了,限時(shí)開放下載!
