springboot项目ip:192.168.168.1
测试服务器ip:192.168.168.81
文章来自互联网,自己略微整理下,更容易上手,方便自己,方便大家
最终效果:
node
springboot
1.下载镜像
docker pull prom/node-exporter
docker pull prom/mysqld-exporter
docker pull google/cadvisor
docker pull prom/prometheus
docker pull grafana/grafana
2.启动
2.1node-exporter
docker run -d -p 19100:9100 \-v /proc:/host/proc:ro \-v /sys:/host/sys:ro \-v /:/rootfs:ro \prom/node-exporter
访问url:
http://192.168.168.81:19100/metrics
2.2启动mysqld-exporter:
docker run -d --name mysqld_exporter -p 19104:9104 -e DATA_SOURCE_NAME="数据库用户:数据库密码@(数据库ip:端口)/" prom/mysqld-exporter
ps:经过测试需要指定版本,最新的版本不识别配置
指定版本
docker run -d --name mysqld_exporter \
-p 19104:9104 \
-e DATA_SOURCE_NAME="root:123456@(192.168.168.81:3306)/" \
prom/mysqld-exporter:v0.14.0
访问url:
http://192.168.168.81:19104/metrics
2.3启动cadvisor
docker run -v /:/rootfs:ro -v /var/run:/var/run:rw -v /sys:/sys:ro -v /var/lib/docker/:/var/lib/docker:ro -v /dev/disk/:/dev/disk:ro -p 18080:8080 -d --name=cadvisor --restart=always google/cadvisor:latest
访问url:
http://127.0.0.1:18080/metrics
2.4启动prometheus
新建prometheus目录,并编辑prometheus.yml文件
mkdir -p /opt/prometheus/data
cd /opt/prometheus/data
vim prometheus.yml
配置如下内容:
global:scrape_interval: 60sevaluation_interval: 60sscrape_configs:- job_name: prometheusstatic_configs:- targets: [192.168.168.81:19090']labels:instance: prometheus- job_name: nodestatic_configs:- targets: ['192.168.168.81:19100']labels:instance: node- job_name: cadvisorstatic_configs:- targets: ['192.168.168.81:18080']labels:instance: cadvisor- job_name: mysqldstatic_configs:- targets: ['192.168.168.81:19104']labels:instance: mysqld- job_name: springboot-prometheusmetrics_path: '/actuator/springboot-prometheus/prometheus'static_configs:- targets: ['192.168.168.1:8080']labels:instance: springboot-prometheus
启动prometheus:
sudo docker run -d -p 19090:9090 --name prom -v /usr/local/prometheus/data:/data prom/prometheus --config.file=/data/prometheus.yml
访问url:
http://192.168.168.81:19090/
2.5、启动grafana
创建文件夹并设置权限:
mkdir /opt/grafana-storage
chmod 777 -R /opt/grafana-storage
启动grafana:
docker run -d \-p 13000:3000 \--name=grafana \-v /opt/grafana-storage:/var/lib/grafana \grafana/grafana
3.使用
访问url:
http://192.168.168.81:13000/
账号:admin/admin
跳转到登录页面后,默认的用户名和密码都是admin,第一次登陆可重置密码后进去首页
设置data source:
点击Save & Test按钮,出现绿色提示,说明配置成功
3.1、docker模板
搜索导入193模板
3.2、springboot模板
搜索导入4701/6756模板
3.3、主机监控
搜索导入9276/8919模板
3.4、mysql模板
搜索导入7362模板
4、springboot 集成prometheus
4.1、引入依赖
在spring boot工程中引入actuator以及micrometer-registry-prometheus的依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency><groupId>io.micrometer</groupId><artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
4.3、配置文件
暴露prometheus的接口,暴露metrics.tags,和spring.application.name一致
spring:application:name: springboot-prometheus
management:endpoints:web:exposure:include: 'prometheus'base-path: /actuator/${spring.application.name}metrics:tags:application: ${spring.application.name}
项目指标访问地址是:192.168.168.1:8080/actuator/springboot-prometheus/prometheus
这个路径是项目路径+"management.endpoints.web.exposure.base-path"+prometheus