您好,登錄后才能下訂單哦!
Spring Boot 自帶監(jiān)控功能 Actuator,可以幫助實(shí)現(xiàn)對(duì)程序內(nèi)部運(yùn)行情況監(jiān)控,比如監(jiān)控狀況、Bean加載情況、環(huán)境變量、日志信息、線(xiàn)程信息等。這一節(jié)結(jié)合 Prometheus 、Grafana 來(lái)更加直觀的展示這些信息。
服務(wù)名 | 地址 | 端口 |
---|---|---|
Prometheus | 172.16.2.101 | 9090 |
Grafana | 172.16.2.101 | 3000 |
Spring Boot Demo | 172.16.2.204 | 8080 |
創(chuàng)建用于測(cè)試的 Spring Boot 項(xiàng)目,主要代碼如下。
<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>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
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<MeterRegistry> metricsCommonTags() {
return registry -> registry.config().commonTags("application", "springboot-actuator-prometheus-demo");
}
*/
}
在 prometheus.yml 中添加針對(duì)該 Spring Boot 應(yīng)用 的監(jiān)控 job
- job_name: 'actuator-demo'
metrics_path: '/prometheus'
static_configs:
- targets: ['172.16.2.204:8080']
運(yùn)行 Prometheus 和 Grafana:
docker start prometheus grafana
訪(fǎng)問(wèn) Prometheus UI http://172.16.2.101:9090 ,查看 targets ,可以看到 job 處于 UP 狀態(tài),說(shuō)明配置成功了。
Grafana UI http://172.16.2.101:3000,通過(guò)Grafana的 + 圖標(biāo)導(dǎo)入(Import) JVM (Micrometer) dashboard:
查看JVM (Micormeter) dashboard:
可以看到應(yīng)用的 JVM 的 堆棧、 線(xiàn)程、 IO 等等信息。
https://github.com/gf-huanchupk/SpringBootLearning/tree/master/springboot-actuator-prometheus
https://micrometer.io/docs/registry/prometheus
https://prometheus.io/docs/prometheus
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。