使用Prometheus和Grafana搭建SpringBoot应用监控系统

最近公司项目介绍的时候看到了类似监控系统的展示页面,比如资源利用、GC次数、Kafka生产消费等,清晰明了,页面十分酷炫。“这是怎么实现的呢??”

使用Prometheus和Grafana搭建SpringBoot应用监控系统

原来是Grafana!!这么好的东西,竟然现在才看到。

简介:Grafana是一款Go语言开发的开源数据可视化工具,可以做数据监控和数据统计,还提供了很多“仪表板”,可以实现很多炫酷且实用的可视化指标。

技术点:Spring boot actuator/micrometer(收集)、Prometheus(存储聚合)、Grafana(可视化)

个人工作中目前应该不涉及到这方面的开发,所以暂时不做深究,先只是满足下好奇心,简单记录下基本的使用过程,更多细节原理,大家可自行了解。

推荐这篇参考文章:https://www.cnblogs.com/throwable/p/13257557.html,有关于度量指标框架Micrometer的详细介绍。

核心思路

以spring boot项目为例,Spring Boot Actuator提供了很多监控指标,可以通过RestFul的形式访问查看,但原始的为json数据,而非上图展示的页面。并且只是瞬时值,不能提供段时间内数据的的聚合分析等。当然这些也可以自行通过数据持久化,并开发前端页面的方式实现。而通过了解,目前已有这样的轮子,就是Prometheus,内部实现时序数据库,可以将收集到的监控数据存储,并且可以结合Grafana进行数据可视化,目前Prometheus已经成为热门且通用的监控解决方案。


关键组件之Exporter:作用类似转换器,各种被监控的对象可以基于共同的Prometheus提供的规范进行实现,从而让自己都能够接入到Prometheus。详细原理可参考文章https://zhuanlan.zhihu.com/p/273229856

新建spring boot 项目

使用Prometheus和Grafana搭建SpringBoot应用监控系统

添加依赖

添加actuator是支持输出监控信息,micrometer-registry-prometheus是能够把actuator监控信息,转化为prometheus能够处理的格式。



  io.micrometer
  micrometer-registry-prometheus


  org.springframework.boot
  spring-boot-starter-actuator

修改application.yml配置文件

在配置文件中,配置endpoint暴露Prometheus,并允许将指标metrics导入到Prometheus中。

server:
  port: 10010

spring:
  application:
    # 应用名,后续会以此展示
    name: Spring Boot Monitor

# 监控相关配置
# 开启监控
management:
  endpoints:
    web:
      exposure:
        include: "*"
  # 端点
  endpoint:
    prometheus:
      enabled: true
    health:
      show-details: always
  # 指标
  metrics:
    export:
      prometheus:
        enabled: true

添加指定的应用名

可以直接通过配置文件引用

metrics:
  tags:
    application: ${spring.application.name}

也可通过启动类,注入

package com.panda00hi.springbootmonitor;

import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class SpringBootMonitorApplication {
    
    public static void main(String[] args) {
        SpringApplication.run(SpringBootMonitorApplication.class, args);
    }
    /**
    * 注册应用名,后续监控中展示该应用名
    *
    * @param applicationName 取配置文件中的应用名
    */
    @Bean
    MeterRegistryCustomizer configure(@Value("${spring.application.name}") String applicationName) {
        return (registry -> registry.config().commonTags("application", applicationName));
    }
}

启动项目

访问http://localhost:10010/actuator,已经可以得到监控信息,并且包含prometheus的可以访问

使用Prometheus和Grafana搭建SpringBoot应用监控系统

访问http://localhost:10010/actuator/prometheus,也可以得到监控信息(prometheus格式的)

使用Prometheus和Grafana搭建SpringBoot应用监控系统

配置Prometheus

利用docker搭建。

参考官方文档:https://prometheus.io/docs/prometheus/latest/getting_started/

下载镜像

docker pull prom/prometheus

配置文件

创建本地用于挂载的数据卷目录/mydata/prometheus,并新建配置文件。

参考官方的配置。最后配置自己的应用配置。

global:
  scrape_interval:     15s # By default, scrape targets every 15 seconds.

  # Attach these labels to any time series or alerts when communicating with
  # external systems (federation, remote storage, Alertmanager).
  external_labels:
    monitor: 'codelab-monitor'

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=` to any timeseries scraped from this config.
  - job_name: 'prometheus'

    # Override the global default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s

    static_configs:
      - targets: ['localhost:9090']

    
  # 额外添加的任务配置,每隔五秒钟抓取数据
  - job_name: 'springboot-prometheus'
    scrape_interval: 5s
    metrics_path: '/actuator/prometheus'
    static_configs:
    # spring boot服务运行的服务器地址。我是物理机运行的,填写的是本机地址
      - targets: ['192.168.3.47:10010']  

启动容器

docker run -d --name=prometheus 
    -p 9090:9090 
    -v /mydata/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml 
    -d prom/prometheus

浏览器访问prometheus默认地址http://172.16.224.100:9090,可以正常访问首页,http://172.16.224.100:9090/metrics可以获取到数据。

选择【status】-》【targets】,即可看到之前配置文件中配置的springboot信息。

使用Prometheus和Grafana搭建SpringBoot应用监控系统

使用Prometheus和Grafana搭建SpringBoot应用监控系统

配置Grafana

下载镜像

docker pull grafana/grafana

启动容器

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

访问页面

默认端口是3000,默认用户名密码都是admin

172.16.224.100:3000

使用Prometheus和Grafana搭建SpringBoot应用监控系统

登陆成功,有那种感觉了,不得不说页面UI颜值真的很高。

使用Prometheus和Grafana搭建SpringBoot应用监控系统

添加prometheus数据源

使用Prometheus和Grafana搭建SpringBoot应用监控系统

添加数据源,选择prometheus,并配置URL

使用Prometheus和Grafana搭建SpringBoot应用监控系统

点击保存按钮,成功会提示

使用Prometheus和Grafana搭建SpringBoot应用监控系统

选择合适的仪表盘

官方提供了很多https://grafana.com/grafana/dashboards/

如,Grafana提供的JVM面板,记住右侧的编码。在自己的Grafana页面导入该编码即可。

使用Prometheus和Grafana搭建SpringBoot应用监控系统

导入编码

使用Prometheus和Grafana搭建SpringBoot应用监控系统

使用Prometheus和Grafana搭建SpringBoot应用监控系统

选择数据源prometheus,导入完成后即可看到效果。

使用Prometheus和Grafana搭建SpringBoot应用监控系统

展开阅读全文

页面更新:2024-05-04

标签:机运   仪表板   数据源   指标   页面   地址   项目   官方   数据   信息

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号

Top