【SpringBoot系列】集成Prometheus Grafana实现应用可视化监控

1 篇文章 0 订阅
1 篇文章 0 订阅

前言

因项目需要最近调研了使用Prometheus+Grafana实现应用程序可视化监控,现将调研成果记录下来以便后续查找,同时也分享给读者,希望能起到一些作用。

快速开始

本文演示环境:

  • Spring Boot 2.0.1.RELEASE
  • jdk 8

1. Spring Boot应用暴露监控指标

首先添加核心依赖如下:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
	<groupId>io.micrometer</groupId>
	<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

然后在application.properties配置监控指标:

server.port=8090
server.servlet.context-path=/monitoring
management.endpoints.web.exposure.include=*

启动应用程序,访问http://localhost:8090/monitoring/actuator,可以看到springboot actuator相关指标:

因为集成的是Prometheus,找到并访问http://localhost:8090/monitoring/actuator/prometheus,可以看到Prometheus格式的指标数据:

2. Prometheus采集应用指标数据

这里我们使用docker镜像的方式部署Prometheus。

首先,获取Prometheus的Docker镜像:

docker pull prom/prometheus

然后,编写prometheus.yml配置文件:

#Global configurations
global:
  scrape_interval: 10s
  scrape_timeout: 10s
  evaluation_interval: 10m
scrape_configs:
  - job_name: prometheus
    scrape_interval: 5s
    scrape_timeout: 5s
    metrics_path: /monitoring/actuator/prometheus
    scheme: http
    static_configs:
      - targets:
        - localhost:8090 #此处为Spring Boot应用程序的IP+端口

接下来,启动Prometheus:

docker run -d --name prometheus \
-p 9090:9090 -m 500M \
-v "$(pwd)/prometheus.yml":/prometheus.yml \
-v "$(pwd)/data":/data \
prom/prometheus \
--config.file=/prometheus.yml \
--log.level=info

$(pwd)表示刚才编写的prometheus.yml存放位置,我的存放在:/opt/prometheus/prometheus.yml

最后,访问http://localhost:9090/targets,检查Spring Boot采集状态是否正常:

成功采集到Spring Boot指标了,UP 表示状态正常。

3、Grafana 可视化监控数据

首先,获取Grafana的Docker镜像

docker pull grafana/grafana

然后,启动Grafana

docker run --name grafana -d -p 3000:3000 grafana/grafana

接着,访问http://localhost:3000,配置数据源

默认登录帐号密码为:admin/admin

最后,配置单个指标的监控面板

指标名只能填写已有的指标,具体可以在Prometheus看到,即http://localhost:9090/graph

下图为配置好的Grafana监控面板图,读者可自行多配置几个

以上就是本次调研成果,如有什么地方不对的,欢迎指正。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hyatt1024

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值