04 SpringBoot初体验:玩转应用监控
文章目录
背景
日志和监控也是为应用正常运行保驾护航的重要工具。对于Java
应用,通常我们会监控机器指标、Jvm
指标等,来发现及帮助定位故障。
spring-boot-actuator
为SpringBoot
应用集成了易用的指标监控。
目标
体验spring-boot-actuator
的应用指标监控玩法。结合prometheus
和grafana
搭建监控平台。
准备工作
基础知识
spring-boot-actuator
:监控指标采集micrometer
:监控指标定义prometheus
、grafana
:监控指标处理与展示
实战
- 添加
actuator
、prometheus
依赖 - 配置
actuator
- 部署
prometheus
、grafana
监控平台(单机演示版) - 配置监控面板
添加依赖
<!-- 监控 -->
<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
配置&启动
修改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
等多线程下载工具
配置&启动
grafana
需要登录,我们用默认的管理员admin
/admin
登录,首次登录需要修改密码,一样使用默认admin
即可。
[auth.anonymous]
# enable anonymous access
enabled = true
启动bin/grafana-server.exe
,访问http://localhost:3000/
连接prometheus
点开设置-数据源。添加一个DataSource
,来源是prometheus
,url
填写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
数据源,点击导入即展示大盘面板,右上角保存即可。