前言

安装基于docker的prometheus来监控java微服务的各项目性能指标。
使用docker来安装最简单省事。

安装

拉取镜像,并查看一下本地镜像

1
2
docker pull prom/prometheus
docker images
1
2
REPOSITORY        TAG       IMAGE ID       CREATED      SIZE
prom/prometheus latest 467da9b74cee 5 days ago 223MB

j## 拿下默认配置文件,用来挂载

将配置文件拷到$PWD/prometheus下,一会挂载用

1
2
3
4
# 启动
docker run -idt --name=docker-prometheus prom/prometheus
# 持配置文件
docker cp -a docker-prometheus:/etc/prometheus/ $PWD/prometheus

prometheus目录下,有这些文件:主要的就是 prometheus.yml 这个配置文件。

1
2
3
console_libraries -> /usr/share/prometheus/console_libraries
consoles -> /usr/share/prometheus/consoles/
prometheus.yml

启动

启动 prometheus 挂载刚才导出来的配置。

1
docker run -d --name=docker-prometheus --restart=always -p 9090:9090 -v $PWD/prometheus:/etc/prometheus prom/prometheus

docker参数:

-d: 后台运行
--name: 服务名称
--restart: 失败自动重启
-p: 端口影射
-v: 挂载目录

验证: http://127.0.0.1:9090

安装成功

配置

一般添加prometheus是为了收集应用程序服务较多,这里做测试给springboot服务添加一个prometheus的endpoint,然后收集。

修改配置:

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
27
28
29
30
31
32
33
34
35
36
37
# 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).

# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093

# 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: ["localhost:9090"]

- job_name: "springboot-test"

# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.

static_configs:
- targets: ["10.1.100.100:9527"]

查看一下配置

http://127.0.0.1:9090/config

java接入

给java服务添加以下maven配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!-- The client -->
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient</artifactId>
<version>0.16.0</version>
</dependency>
<!-- Hotspot JVM metrics-->
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_hotspot</artifactId>
<version>0.16.0</version>
</dependency>
<!-- Exposition HTTPServer-->
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_httpserver</artifactId>
<version>0.16.0</version>
</dependency>
<!-- Pushgateway exposition-->
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_pushgateway</artifactId>
<version>0.16.0</version>
</dependency>

http://127.0.0.1:9090/targets

 查看接入点

再看看一下是否已经有指标进来了:

http://127.0.0.1:9090/metrics

看下结果

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
27
28
29
30
31
32
33
34
35
36
# HELP net_conntrack_dialer_conn_attempted_total Total number of connections attempted by the given dialer a given name.
# TYPE net_conntrack_dialer_conn_attempted_total counter
net_conntrack_dialer_conn_attempted_total{dialer_name="alertmanager"} 0
net_conntrack_dialer_conn_attempted_total{dialer_name="default"} 0
net_conntrack_dialer_conn_attempted_total{dialer_name="springboot-test"} 1
net_conntrack_dialer_conn_attempted_total{dialer_name="prometheus"} 1
# HELP net_conntrack_dialer_conn_closed_total Total number of connections closed which originated from the dialer of a given name.
# TYPE net_conntrack_dialer_conn_closed_total counter
net_conntrack_dialer_conn_closed_total{dialer_name="alertmanager"} 0
net_conntrack_dialer_conn_closed_total{diler_name="default"} 0
net_conntrack_dialer_conn_closed_total{dialer_name="springboot-test"} 0
net_conntrack_dialer_conn_closed_total{dialer_name="prometheus"} 0
# HELP net_conntrack_dialer_conn_established_total Total number of connections successfully established by the given dialer a given name.
# TYPE net_conntrack_dialer_conn_established_total counter
net_conntrack_dialer_conn_established_total{dialer_name="alertmanager"} 0
net_conntrack_dialer_conn_established_total{dialer_name="default"} 0
net_conntrack_dialer_conn_established_total{dialer_name="springboot-test"} 1
net_conntrack_dialer_conn_established_total{dialer_name="prometheus"} 1
# HELP net_conntrack_dialer_conn_failed_total Total number of connections failed to dial by the dialer a given name.
# TYPE net_conntrack_dialer_conn_failed_total counter
net_conntrack_dialer_conn_failed_total{dialer_name="alertmanager",reason="refused"} 0
net_conntrack_dialer_conn_failed_total{dialer_name="alertmanager",reason="resolution"} 0
net_conntrack_dialer_conn_failed_total{dialer_name="alertmanager",reason="timeout"} 0
net_conntrack_dialer_conn_failed_total{dialer_name="alertmanager",reason="unknown"} 0
net_conntrack_dialer_conn_failed_total{dialer_name="default",reason="refused"} 0
net_conntrack_dialer_conn_failed_total{dialer_name="default",reason="resolution"} 0
net_conntrack_dialer_conn_failed_total{dialer_name="default",reason="timeout"} 0
net_conntrack_dialer_conn_failed_total{dialer_name="default",reason="unknown"} 0
net_conntrack_dialer_conn_failed_total{dialer_name="springboot-test",reason="refused"} 0
net_conntrack_dialer_conn_failed_total{dialer_name="springboot-test",reason="resolution"} 0
net_conntrack_dialer_conn_failed_total{dialer_name="springboot-test",reason="timeout"} 0
net_conntrack_dialer_conn_failed_total{dialer_name="springboot-test",reason="unknown"} 0
net_conntrack_dialer_conn_failed_total{dialer_name="prometheus",reason="refused"} 0
net_conntrack_dialer_conn_failed_total{dialer_name="prometheus",reason="resolution"} 0
net_conntrack_dialer_conn_failed_total{dialer_name="prometheus",reason="timeout"} 0
net_conntrack_dialer_conn_failed_total{dialer_name="prometheus",reason="unknown"} 0

参考链接

官网:https://prometheus.io/
dockerHub: https://hub.docker.com/u/prom
官方maven: https://mvnrepository.com/artifact/io.prometheus
官方示例: https://github.com/prometheus/client_java#counter