安装基于docker的prometheus来监控java微服务的各项目性能指标。 使用docker来安装最简单省事。
拉取镜像,并查看一下本地镜像
docker pull prom/prometheus
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
prom/prometheus latest 467da9b74cee 5 days ago 223MB
j## 拿下默认配置文件,用来挂载
将配置文件拷到$PWD/prometheus下,一会挂载用
# 启动
docker run -idt --name=docker-prometheus prom/prometheus
# 持配置文件
docker cp -a docker-prometheus:/etc/prometheus/ $PWD/prometheus
prometheus目录下,有这些文件:主要的就是 prometheus.yml 这个配置文件。
console_libraries -> /usr/share/prometheus/console_libraries
consoles -> /usr/share/prometheus/consoles/
prometheus.yml
启动 prometheus 挂载刚才导出来的配置。
docker run -d --name=docker-prometheus --restart=always -p 9090:9090 -v $PWD/prometheus:/etc/prometheus prom/prometheus
docker参数:
-d
: 后台运行
--name
: 服务名称
--restart
: 失败自动重启
-p
: 端口影射
-v
: 挂载目录
一般添加prometheus是为了收集应用程序服务较多,这里做测试给springboot服务添加一个prometheus的endpoint,然后收集。
修改配置:
# 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"]
查看一下配置
给java服务添加以下maven配置:
<!-- 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>
再看看一下是否已经有指标进来了:
看下结果
# 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