springboot+prometheus+grafana:网站可视化监控初试

环境: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
模板去官网找即可。

然后就出现数据了。
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值