04 SpringBoot初体验:玩转应用监控spring-boot-starter-actuator

04 SpringBoot初体验:玩转应用监控

  1. 01 SpringBoot初体验:初始化web项目
  2. 02 SpringBoot初体验:统一响应和异常处理
  3. 03 SpringBoot初体验:Swagger接口文档
  4. 04 SpringBoot初体验:玩转应用监控

背景

日志和监控也是为应用正常运行保驾护航的重要工具。对于Java应用,通常我们会监控机器指标、Jvm指标等,来发现及帮助定位故障。

spring-boot-actuatorSpringBoot应用集成了易用的指标监控。

目标

体验spring-boot-actuator的应用指标监控玩法。结合prometheusgrafana搭建监控平台。

准备工作

基础知识

  1. spring-boot-actuator:监控指标采集
  2. micrometer:监控指标定义
  3. prometheusgrafana:监控指标处理与展示

实战

  1. 添加actuatorprometheus依赖
  2. 配置actuator
  3. 部署prometheusgrafana监控平台(单机演示版)
  4. 配置监控面板
监控指标采集
监控指标上报
监控指标处理&面板展示
指标度量定义
指标度量定义
指标度量定义
指标度量定义
指标度量定义
指标度量定义
指标度量定义
指标度量定义
监控指标上报
监控指标上报
监控指标上报
监控指标上报
监控指标上报
监控指标上报
监控指标上报
micrometer
SpringBootApplication
prometheus
grafana
Timer
Counter
Gauge
DistributionSummary
LongTaskTimer
FunctionCounter
FunctionTimer
TimeGauge
azure-monitor
cloudwatch2
datadog
dynatrace
ganglia
influx
stackdriver

添加依赖

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

配置actuator

spring-boot-actuator作为springboot生产特性,并不仅只有监控指标功能。关于spring-boot-actuator的详细介绍可以参考官方文档Production-ready Features (spring.io)。关于监控指标部分可以参考该文档6.3. Supported Metrics and Meters

application.yml中配置开启的端点(默认/actuator/prometheus)、监控指标的tag(用于区分应用服务)

spring:
  application:
    name: "new-bird-web"
management:
  endpoints:
    web:
      base-path: /actuator
      exposure:
        include: "*"
  metrics:
    tags:
      application: ${spring.application.name} # 一般使用application作为tag

启动应用,访问http://localhost:8080/actuator/prometheus

在这里插入图片描述

部署prometheus & grafana

prometheus用来采集springboot上报的的监控指标并持久化。grafana用来处理监控指标展示成监控面板。

prometheus

下载&安装

官网:https://prometheus.io/download/

https://github.com/prometheus/prometheus/releases

配置&启动

Getting started | Prometheus

修改prometheus.yml,添加springboot应用扫描配置

# my global config
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_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# 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=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090"]
      
    # 添加一个扫描任务,扫本地localhost:8080/actuator/prometheus
  - job_name: "spring-boot-application"
    metrics_path: /actuator/prometheus
    static_configs:
      - targets: ["localhost:8080"]

启动prometheus.exe,访问http://localhost:9090/

在这里插入图片描述

grafana

下载&安装

官网:https://grafana.com/grafana/download,下载好慢,windows可以试试用idm等多线程下载工具

配置&启动

With Grafana and Prometheus | Grafana documentation

grafana需要登录,我们用默认的管理员admin/admin登录,首次登录需要修改密码,一样使用默认admin即可。

[auth.anonymous]
# enable anonymous access
enabled = true

启动bin/grafana-server.exe,访问http://localhost:3000/

连接prometheus

点开设置-数据源。添加一个DataSource,来源是prometheusurl填写http://localhost:9090

监控面板

监控面板下载:https://grafana.com/grafana/dashboards/?pg=community&plcmt=topnav

SLS JVM监控大盘:https://grafana.com/grafana/dashboards/12856/revisions

grafana的监控面板可以从社区下载,这里我们演示一个Jvm监控大盘面板。

点击创建-导入,上传我们下载的jvm-micrometer_rev1.json,选择数据源为我们刚创建的prometheus数据源,点击导入即展示大盘面板,右上角保存即可。

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

theskyzero

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

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

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

打赏作者

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

抵扣说明:

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

余额充值