使用Prometheus + Grafana搭建监控系统

对任何系统就像人一样需要“知己知彼“才能做到”百战百胜”,经常在讨论高可用,一方面需要从架构设计、编码实施、质量验证等环节保证项目的高可用,另一方面能够尽可能早的发现问题也是非常的重要。目前监控生态中有大量的产品可供选择,Java生态尤其是Spring Boot生态下更是,如何选择一套适合自己的方案则显得尤为重要。

对于Spring Boot应用可以使用Spring Boot Admin进行监控,但是还不够全面,本文则主要介绍Prometheus搭配Grafana进行监控报警。大致流程图如下

Prometheus+Grafana架构

Prometheus是服务器监控系统的后起之秀,可以和Kubernetes完美结合用于监控大量集群和应用,Grafana是一款数据可视化看板,可指定多个数据源执行查询,将枯燥的数据转化为多维度的面板。两者均为开源项目,通过配置可实现直观强大的监控、报警、分析系统,实属运维神奇。

简介

Prometheus简介

Prometheus(普罗米修斯)是一款从2012年开始研发的弹性监控解决方案,该系统将其数据存储至时序数据库,且提供了多维度的数据模型和强大的查询语言来生成被监控资源的报表。最初由SpringCloud发布

特点

  • 多维数据模型
  • 灵活的查询语言
  • 不依赖分布式存储
  • 通过pull方式采集时间序列,通过http协议传输
  • 支持通过中介网关的push时间序列的方式
  • 监控数据通过服务或者静态配置
  • 支持图表和Dashboard等多种方式

组件

  • Prometheus主程序,主要负责存储、抓取、聚合、查询
  • AlertManager,主要负责实现报警
  • PushGateway,主要实现由Client push过来的指标数据,在指定的时间间隔,由主程序来抓取

Grafana简介

Grafana是一款用go语言编写的开源应用,主要用于大规模指标数据的可视化展现,有热插拔控制面板和可扩展的数据源,已经支持绝大部分常用的时序数据库,包括:Graphite、Elasticsearch、CloudWatch、InfluxDB、OpenTSDB、Prometheus等

搭建Prometheus应用

代码级配置

Maven pom.xml

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            io.micrometer
            micrometer-registry-prometheus
        

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

启动类

@SpringBootApplication
public class PrometheusApplication {

    public static void main(String[] args) {
        SpringApplication.run(PrometheusApplication.class, args);
    }
}

增加MeterRegistryCustomizer配置类

@Configuration
public class MeterRegistryConfig {

    @Value("${spring.application.name}")
    private String applicationName;

    @Bean
    MeterRegistryCustomizer metricsCommonTags() {
        return registry -> registry.config().commonTags("application", applicationName);
    }
}

配置文件

在配置文件中加入配置应用名,并且开启全部actuator端点

server:
  port: 8074

spring:
  application:
    name: prometheus-demo

management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: always

验证测试

启动项目,访问http://localhost:8074/actuator/prometheus,可以看到如下采集到的信息

prometheus actuator信息

环境搭建

本文已Mac为例进行介绍,其他系统可选择直接安装二进制包也可下载源码编译安装

安装prometheus

brew install prometheus

或从https://prometheus.io/download/ 选择合适的版本进行下载安装

安装成功后,配置文件默认在该/usr/local/etc/位置下,可进行简易配置

vim /usr/local/etc/prometheus.yml

注意:如果启动后页面无法访问,请检查配置格式是否有误(空格 or Tab)

global:
  scrape_interval: 15s
  evaluation_interval: 15s
alerting:
  alertmanagers:
  - static_configs:
    - targets:
rule_files:

scrape_configs:
  - job_name: "prometheus"
    static_configs:
    - targets: ["localhost:9090"]
  - job_name: "prometheus-demo"
    scrape_interval: 5s
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ['localhost:8074']

然后启动Prometheus就可以进行访问了,默认端口为9090(可以根据各自情况就行修改)

brew services start prometheus

打开管理页面:http://localhost:9090/graph

prometheus graph

Grafana安装

安装及启动Grafana

brew install grafana

或从https://grafana.com/get 选择合适的版本进行下载安装

配置位置: /usr/local/etc/grafana/grafana.ini
日志位置:/usr/local/var/log/grafana/grafana.log
插件位置:/usr/local/var/lib/grafana/plugins

启动服务

brew services start grafana

默认端口:3000(可以根据各自情况就行修改)
默认用户名密码:admin/admin(首次登陆会提升修改密码)


grafana初始页面

配置Grafana

首先需要创建一个数据源,这里选择type为Prometheus的数据源,名称为Prometheus-Data


grafana设置datasource

然后点击最下面的“Save & Test”按钮,当看到datasource is working的提示时,说明Prometheus已可以正常连接获取指标数据了

然后回到首页,再添加一个Dashboard,最简单直接的方式是直接点击刚才配置DataSource的第二个Tab页,然后选择推进Dashboard的一个组合,然后import,然后就有一组配置好的查看页面了,回到首页选取一个Dashboard就可以查看监控数据情况了


grafana Dashboard

示例代码

spring-boot-prometheus

你可能感兴趣的:(使用Prometheus + Grafana搭建监控系统)