Spring Boot + prometheus + Grafana应用可视化监控
springboot项目配置
maven项目中pom文件中添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<version>1.1.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
配置文件:application.properties
#启用prometheus端点
management.endpoint.prometheus.enabled=true
#暴露端点
management.endpoints.web.exposure.include=health,info,env,prometheus,metrics,httptrace,threaddump,heapdump,springmetrics
# 或者 management.endpoints.web.exposure.include=*
# 排除env beans端点
management.endpoints.web.exposure.exclude=env,beans
management.metrics.tags.application=${spring.application.name}
management.endpoint.prometheus.enabled=true
management.metrics.export.prometheus.enabled=true
management.metrics.export.prometheus.step=1m
management.metrics.export.prometheus.descriptions=true
查看度量指标是否集成成功
浏览器访问:http://localhost:8081/actuator/prometheus
注意:如果配置文件中配置 server.servlet.context-path=路径 查看度量地址为:http://localhost:8081/路径/actuator/prometheus
安装Prometheus
下载地址:https://prometheus.io/download/
解压后进入Prometheus 安装 配置prometheus.yml
- job_name: 'spring'
# job_name 可随意填写
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
#采集路径
metrics_path: /mallAdmin/actuator/prometheus
#应用地址
static_configs:
- targets: ['localhost:8081']
启动Prometheus 点击prometheus.exe
浏览器访问:[http://localhost:9090](
安装Grafana
下载地址:https://grafana.com/grafana/download
解压后进入安装根目录 conf目录下 sample.ini 复制一份重命名为custom.ini 修改 http_port 端口
启动 grafana-server.exe
浏览器访问:http://127.0.0.1:3000/login (Grafana默认端口是3000,如果修改了端口用修改后的端口)
用户名密码默认都是admin
添加数据源
在Data Sources选项中添加数据源
导入仪表盘模板
模板地址:https://grafana.com/dashboards
推荐模板ID:4701,6756