Spring Boot 實現(xiàn)應用監(jiān)控和報警

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

01 開發(fā) Spring Boot 應用
首先,創(chuàng)建一個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>
<!-- 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是因為最新的 simpleclient_spring_boot 只支持1.5.X,不確定2.X版本的能否支持。
MonitorDemoApplication 啟動類增加注解:
@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
# 安全路徑列表,逗號分隔,此處只針對/admin路徑進行認證
path: /admin
# actuator暴露接口的前綴
management:
context-path: /admin
# actuator暴露接口使用的端口,為了和api接口使用的端口進行分離
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 # 刮取的時間間隔
scrape_timeout: 5s
metrics_path: /admin/prometheus
scheme: http
basic_auth: #認證信息
username: admin
password: 1234
static_configs:
- targets:
- 127.0.0.1:8888 #此處填寫 Spring Boot 應用的 IP + 端口號




4.選擇圖表樣式





第二步: 郵箱配置
Grafana默認使用conf目錄下defaults.ini作為配置文件運行,根據(jù)官方的建議我們不要更改defaults.ini而是在同級目錄下新建一個配置文件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

配置通知方式和信息


附上幾個鏈接:
Prometheus官方文檔:
推薦閱讀:
華為內(nèi)網(wǎng)最火的文章:什么是內(nèi)卷?
不是你需要中臺,而是一名合格的架構師(附各大廠中臺建設PPT)
評論
圖片
表情
