Spring Boot 集成 prometheus,Grafana 监控

1 prometheus

Prometheus是一个根据应用的metrics来进行监控的开源工具。具体可见官网

2 Spring Boot 集成 prometheus

2.1 构建 spring boot 应用
  • pom 依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>${spring.boot.version}</version>
            <scope>test</scope>
        </dependency>
        
        <!-- prometheus -->
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
            <version>1.1.3</version>
        </dependency>
    </dependencies>
    
  • 配置

    spring.application.name=prometheus-metrics
    server.port=8080
    management.endpoints.web.exposure.include=*
    # 配合 Grafana 的 Dashboard 设置
    management.metrics.tags.application=${spring.application.name}
    
  • MeterRegistryCustomizer 配置

    @Configuration
    public class MetricsConfig {
        @Value("${spring.application.name}")
        String applicationName;
    
        @Bean
        MeterRegistryCustomizer<MeterRegistry> meterRegistry() {
    	      	return (registry) -> registry.config().commonTags("application", applicationName);
    	}
    }
    

    PS: 配置类名千万别叫 PrometheusConfig,会报错,已经存在这样的 bean。

SpringBoot 项目就搭建完成了,可以看到集成 Prometheus 是非常简单的。启动项目,访问http://localhost:8080/actuator/prometheus,如图所示,可以看到一些度量指标。
度量指标

2 Prometheus 搭建

  • mac 下安装 prometheus

    $ brew install prometheus
    
  • linux 安装

    # 1. 下载
    wget https://github.com/prometheus/prometheus/releases/download/v2.10.0/prometheus-2.10.0.linux-amd64.tar.gz
    # 2. 解压
    tar zxvf prometheus-2.10.0.linux-amd64.tar.gz
    # 3. 启动
    cd prometheus-2.10.0.linux-amd64
    ./prometheus --config.file=prometheus.yml
    

2. 解压

tar zxvf prometheus-2.10.0.linux-amd64.tar.gz

3. 启动

cd prometheus-2.10.0.linux-amd64
./prometheus --config.file=prometheus.yml
  • 配置 prometheus

    global:
      scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
      evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
    
    scrape_configs:
      # spring boot 应用配置
      - job_name: "prometheus-metrics"
        metrics_path: "/actuator/prometheus" # spring boot 监控访问路径
        static_configs:
        - targets: ["localhost:8080"] # 域名
      # prometheus 应用本身
      - job_name: "prometheus"
        static_configs:
        - targets: ["localhost:9090"]
    

    PS: prometheus 默认配置文件位置:/usr/local/etc/prometheus.yml

  • 启动 prometheus,后台运行

    $ prometheus --config.file=/usr/local/etc/prometheus.yml &
    

现在,可以通过浏览器访问 localhost:9090 就可以看到prometheus的监控页面。
监控
当然,在 graph 菜单栏可以查看其他监控指标。
指标监控

3 grafana

  • 安装

    brew install grafana
    
  • 启动 grafana

    brew services start grafana
    

现在就可以通过 http://localhost:3000/?orgId=1 访问 grafana 了。后面就可以通过添加 datasource,就可以定义监控和图形。比如导入 JVM 监控。
JVM 监控
JVM 监控

4 实例

4.1 监控系统信息
  • 安装 node_exporter

    下载

    tar -xvzf node_exporter-1.0.1.darwin-amd64.tar.gz
    cd node_exporter-1.0.1.darwin-amd64
    ././node_exporter &
    
  • 配置 prometheus

    - job_name: "172.16.0.116" # 主机 ip 地址
    metrics_path: "/metrics" # 可以不配,prometheus 默认从此接口获取信息
    static_configs:
     - targets: ["localhost:9100"] # node_exporter 监听端口 9100
    
  • grafana dashboard 配置

    官方查找 dashboard,然后导入 dashboard。
    系统dashboard

4.2 监控告警
4.2.1 告警系统宕机
  • 部署 Alertmanager

    下载 AlertManager

    1. 解压
    tar -zxvf 
    2.  配置alertmanager.yml
    
    # 全局配置
    global:
      resolve_timeout: 5m
      smtp_smarthost: 'xxxxxx'
      smtp_from: 'xxxx@xx.com'
      smtp_auth_username: 'xxxx@xx.com'
      smtp_auth_password: 'XXXXXX'
    # 路由配置
    route:
      receiver: 'default-receiver' # 父节点
      group_by: ['alertname'] # 分组规则
      group_wait: 10s # 为了能够一次性收集和发送更多的相关信息时,可以通过		group_wait 参数设置等待时间
      group_interval: 1m  #定义相同的Group之间发送告警通知的时间间隔
      repeat_interval: 1m
      routes: # 子路由,根据match路由
      - receiver: 'rhf-mail-receiver'
        group_wait: 10s
        match: # 匹配自定义标签
          team: rhf    
    # 告警接收者配置
    receivers:
    - name: 'default-receiver'
      email_configs:
      - to: 'xxxx@xx.com'
    - name: 'rhf-mail-receiver'
      email_configs:
      - to: 'xxxx@xx.com'
    

    目前官方内置的第三方通知集成包括:邮件、 即时通讯软件(如Slack、Hipchat)、移动应用消息推送(如Pushover)和自动化运维工具(例如:Pagerduty、Opsgenie、Victorops)。Alertmanager的通知方式中还可以支持Webhook,通过这种方式开发者可以实现更多个性化的扩展支持(钉钉、企业微信等)。

  • 启动 AlertManager
    Alermanager 会将数据保存到本地中,默认的存储路径为 data/。因此,在启动Alertmanager之前需要创建相应的目录:./alertmanager。用户也在启动 Alertmanager 时使用参数修改相关配置。–config.file 用于指定 alertmanager 配置文件路径,–storage.path 用于指定数据存储路径。

  • 设置告警规则

    在 Prometheus 目录下新建 alert-rule.yaml 来设置告警规则,内容如下:

    groups:
    - name: alert-rule
      rules:
      - alert: svc-down # 服务是否下线
        expr: sum(up{job="springboot-actuator-prometheus-test"}) == 0
        for: 1m
        labels: # 自定义标签
          severity: critical
          team: rhf # 我们小组的名字,对应上面match 的标签匹配
        annotations:
          summary: "服务已下线,请检查"
    
  • 配置 prometheus

    在 prometheus.yml 文件下,引用 alert-rule.yaml 告警规则配置,并开启 Alertmanager。

    alerting:
      alertmanagers:
      - static_configs:
        - targets:
          # alertmanage default start port 9093
          - localhost:9093  
    rule_files:
      - /data/prometheus-stack/prometheus/rule/*.yml
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值