环境:win10 + java1.8 + springboot 2.4.3
参考:https://blog.csdn.net/yangshangwei/article/details/88385783
说明:springboot开发项目,prometheus负责监控,grafana只负责展示prometheus的数据。用grafana是因为,虽然prometheus能抓到很多metrics,但是可视化做得不咋样,所以才借助grafana以期画出更酷炫的图。
安装prometheus
下载地址:https://github.com/prometheus/prometheus/releases/
我的操作:下载 prometheus-2.26.0.windows-amd64,解压,然后修改prometheus.yml以实现配置(目前暂时不修改),然后双击 prometheus.exe 运行,最后访问localhost:9090查看。
Status → Targets 可以查看监控对象
Springboot集成prometheus
修改pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
修改application.yml
spring:
application:
name: demo1
management:
endpoints:
web:
exposure:
include: '*'
metrics:
tags:
application: ${spring.application.name}
其中 spring.application.name 是pom文件前面、项目的名字,比如我这里是demo1。
修改启动类
@Bean
MeterRegistryCustomizer<MeterRegistry> configurer(@Value("${spring.application.name}")String applicationName) {
return (registry) -> registry.config().commonTags("application", applicationName);
}
修改prometheus.yml以接入项目
注意targets是你项目使用的端口,比如我用的是3000端口。
# 配置springboot项目
- job_name: 'demo1'
scrape_interval: 5s
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['localhost:3000']
然后启动springboot项目,启动prometheus,此时:
说明:prometheus默认会监控自己,这个不仅在Targets下有体现,在prometheus.yml和Status→Configuration也可以看到。
配置grafana
我下载的是 grafana-7.5.3.windows-amd64.msi,然后傻瓜式安装即可。
grafana默认的端口是3000,与我的冲突,所以我开放8088给它。当然要先用netstat -ano|findstr "8088"查看占用情况。
win10开放端口的方法直接百度即可。
修改 \GrafanaLabs\grafana\conf\defaults.ini
启动的话是在bin下双击 grafana-server.exe。
默认用户名和密码是admin,第一次登录会让你改密码。
grafana接入prometheus数据
左侧的configuration → add data source → prometheus
URL写的是 http://localhost:9090 其他没改
然后回主页,左侧+下 import,粘贴想要的模板,我这里用:(或者10280也行)
https://grafana.com/grafana/dashboards/10280
模板去官网找即可。
然后就出现数据了。