Spring Boot 自帶監(jiān)控功能 Actuator,可以幫助實現(xiàn)對程序內(nèi)部運行情況監(jiān)控,比如監(jiān)控狀況、Bean加載情況、環(huán)境變量、日志信息、線程信息等。這一節(jié)結合 Prometheus 、Grafana 來更加直觀的展示這些信息。
創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供蒙自網(wǎng)站建設、蒙自做網(wǎng)站、蒙自網(wǎng)站設計、蒙自網(wǎng)站制作等企業(yè)網(wǎng)站建設、網(wǎng)頁設計與制作、蒙自企業(yè)網(wǎng)站模板建站服務,十載蒙自做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡服務。
服務名 | 地址 | 端口 |
---|---|---|
Prometheus | 172.16.2.101 | 9090 |
Grafana | 172.16.2.101 | 3000 |
Spring Boot Demo | 172.16.2.204 | 8080 |
創(chuàng)建用于測試的 Spring Boot 項目,主要代碼如下。
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter-web
io.micrometer
micrometer-registry-prometheus
org.springframework.boot
spring-boot-starter-test
test
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
metrics:
tags:
application: actuator-demo
@SpringBootApplication
@RestController
public class SpringbootActuatorPrometheusDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootActuatorPrometheusDemoApplication.class, args);
}
@RequestMapping(value = "/hello")
public String sayHello() {
for (int i = 1 ; i <= 10 ; i++) {
Thread t = new Thread(() -> {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} , "HelloThread - " + i);
t.start();
}
return "ok";
}
/**
@Bean
MeterRegistryCustomizer metricsCommonTags() {
return registry -> registry.config().commonTags("application", "springboot-actuator-prometheus-demo");
}
*/
}
在 prometheus.yml 中添加針對該 Spring Boot 應用 的監(jiān)控 job
- job_name: 'actuator-demo'
metrics_path: '/prometheus'
static_configs:
- targets: ['172.16.2.204:8080']
運行 Prometheus 和 Grafana:
docker start prometheus grafana
訪問 Prometheus UI http://172.16.2.101:9090 ,查看 targets ,可以看到 job 處于 UP 狀態(tài),說明配置成功了。
Grafana UI http://172.16.2.101:3000,通過Grafana的 +圖標導入(Import) JVM (Micrometer) dashboard:
查看JVM (Micormeter) dashboard:
可以看到應用的 JVM 的 堆棧、 線程、 IO 等等信息。
https://github.com/gf-huanchupk/SpringBootLearning/tree/master/springboot-actuator-prometheus
https://micrometer.io/docs/registry/prometheus
https://prometheus.io/docs/prometheus