首先,添加依赖如下依赖:

org.springframework.boot
spring-boot-starter-actuator
采集应用的指标信息,我们使用的是prometheus,相应的我们引入包:
io.prometheus
simpleclient_spring_boot                     0.0.26
然后,在启动类 Application.java 添加如下注解:
@SpringBootApplication
@EnablePrometheusEndpoint
@EnableSpringBootMetricsCollector
public class Application {
public static void main(String[] args) {        SpringApplication.run(Application.class, args);
}
}
最后,配置默认的登录账号和密码,在 application.yml 中:
security:
user:
name:user
password: pwd

启动应用程序后,会看到如下一系列的 Mappings

java监控本地文件夹_面试

img

利用账号密码访问 http://localhost:8080/application/prometheus ,可以看到 Prometheus 格式的指标数据

java监控本地文件夹_java监控本地文件夹_02

2、Prometheus 采集 Spring Boot 指标数据

首先,获取 Prometheus 的 Docker 镜像:

$ docker pull prom/prometheus
然后,编写配置文件 prometheus.yml :
global:
scrape_interval: 10s
scrape_timeout: 10s
evaluation_interval: 10m
scrape_configs:
- job_name: spring-boot
scrape_interval: 5s
scrape_timeout: 5s
metrics_path: /application/prometheus
scheme: http
basic_auth:
username: admin
password: 123456
static_configs:
- targets:

- 192.168.11.54:8099 #此处填写 Spring Boot 应用的 IP + 端口号

接着,启动 Prometheus :

docker run -d --name prometheus -p 9090:9090