SpringBoot 2.x 集成prometheus:
添加依赖
<!-- spring boot 健康监控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 整合 Prometheus -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
添加配置
application.yml
server:
port: 8080
spring:
application:
name: spring-boot-prometheus-demo
management:
# server:
# port: 8081
endpoints:
web:
# base-path: /metrics
exposure:
include: '*'
metrics:
export:
jmx:
enabled: true
prometheus:
enabled: true
tags:
application: ${spring.application.name}
# 添加 http_server_requests_seconds_bucket 直方图配置
distribution:
percentiles-histogram:
http:
server:
requests: true
启动并验证
Linux 安装
下载
使用
# 创建并进入 prometheus 安装目录
mkdir /usr/local/prometheus
cd /usr/local/prometheus
# 下载 prometheus node_exporter
wget https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz
# 解压
tar -xzvf node_exporter-1.0.1.linux-amd64.tar.gz
# 手动启动
nohup /usr/local/prometheus/node_exporter-1.0.1.linux-amd64/node_exporter >/dev/null 2>&1 &
默认端口为 9100
访问
http://192.168.8.160:9100/metrics
设置开机启动
编写服务脚本
vim /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=prometheus node_exporter
After=network.target
[Service]
Restart=always
Type=forking
ExecStart=/usr/local/prometheus/node_exporter-1.0.1.linux-amd64/node_exporter >/dev/null 2>&1 &
[Install]
WantedBy=multi-user.target
将服务加入开机启动
# 刷新服务列表
systemctl daemon-reload
# 设置开机启动
systemctl enable node_exporter.service
# 启动服务
systemctl start node_exporter.service
Windows 安装
下载
使用
双击启动 windows_exporter-0.15.0-amd64.exe
即可
访问
http://127.0.0.1:9182/metrics
mysqld_exporter 安装
解压
tar -zxvf mysqld_exporter-0.12.1.linux-amd64.tar.gz
移动
在 mysqld_exporter 添加mysql配置文件 .my.cnf
[client]
user=user_mysqld
password=123456
启动
nohup mysqld_exporter --config.my-cnf=./.my.cnf &
确认启动
lsof -i:9104
如果是部署在一台服务器的话只需要再原来的配置中添加
- targets: ['localhost:9090','localhost:9100','localhost:9104']