首先,添加依赖以下依赖:java
<dependency>spring
<groupId\>org.springframework.boot</groupId\> <artifactId\>spring-boot-starter-actuator</artifactId\> </dependency\> <dependency\> <groupId\>io.prometheus</groupId\> <artifactId\>simpleclient\_spring\_boot</artifactId\> <version\>0.0.26</version\> </dependency\>复制代码
而后,在启动类Application.java
添加以下注解:docker
@SpringBootApplication
@EnablePrometheusEndpoint
@EnableSpringBootMetricsCollector
public class Application {app
public static void main(String\[\] args) { SpringApplication.run(Application.class, args); }
}复制代码spring-boot
最后,配置默认的登陆帐号和密码,在application.yml
中:lua
security:
user:spa
name: user password: pwd复制代码
提示:不建议配置management.security.enabled: false
3d
启动应用程序后,会看到以下一系列的Mappings
code
利用帐号密码访问http://localhost:8080/application/prometheus,能够看到 Prometheus 格式的指标数据blog
首先,获取 Prometheus 的 Docker 镜像:
$ docker pull prom/prometheus复制代码
而后,编写配置文件prometheus.yml
:
global:
scrape_interval: 10s
scrape_timeout: 10s
evaluation_interval: 10m
scrape_configs:
scrape_interval: 5s
scrape_timeout: 5s
metrics_path: /application/prometheus
scheme: http
basic_auth:
username: user
password: pwd
static_configs:
接着,启动 Prometheus :
$ docker run -d \
--name prometheus \
-p 9090:9090 \
-m 500M \
-v "$(pwd)/prometheus.yml":/prometheus.yml \
-v "$(pwd)/data":/data \
prom/prometheus \
-config.file=/prometheus.yml \
-log.level=info复制代码
最后,访问http://localhost:9090/targets, 检查 Spring Boot 采集状态是否正常。
首先,获取 Grafana 的 Docker 镜像:
$ docker pull grafana/grafana复制代码
而后,启动 Grafana:
$ docker run --name grafana -d -p 3000:3000 grafana/grafana复制代码
接着,访问http://localhost:3000/配置 Prometheus 数据源:
Grafana 登陆帐号 admin 密码 admin
最后,配置单个指标的可视化监控面板:
提示,此处不能任意填写,只能填已有的指标点,具体的能够在 Prometheus 的首页看到,即http://localhost:9090/graph
多配置几个指标以后,便可有以下效果: