Prometheus & Grafana

prepare

springboot metrics config

使用Docker下载和运行Prometheus

下载Prometheus

1
docker pull prom/prometheus

Prometheus配置(prometheus.yml)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
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: ['127.0.0.1:9090']

- job_name: 'spring-actuator'
metrics_path: '/actuator/prometheus'
scrape_interval: 5s
static_configs:
- targets: ['192.168.50.93:9093']

上面中比较重要的配置项是spring-actuator job中的scrape_configs选项。

metrics_path是Actuator中prometheus endpoint中的路径。targes包含了Spring Boot应用的HOST和PORT

请确保替换HOST_IP为你Spring Boot应用运行的电脑的IP地址。值得注意的是,localhost将不起作用,因为我们将从docker container中连接HOST机器。你必须设置网络IP地址。

使用Docker运行Prometheus

1
2
3
4
docker run -d --name=prometheus -p 9090:9090 -v /root/apps/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus --config.file=/etc/prometheus/prometheus.yml

# docker stop prometheus
# docker rm prometheus

在Prometheus仪表盘中可视化Spring Boot Metrics

1
http://192.168.50.28:9090

使用Docker下载和运行Grafana

###下载Grafana

1
docker run -d --name=grafana -p 3000:3000 grafana/grafana

可以访问http://192.168.50.28:3000,并且使用默认的账户名(admin)密码(admin)来登录Grafana

参考文章

<<<<<<< HEAD

评论