应用监控的最佳实战【Java + Spring Boot为例】

背景:

云原生和容器化是趋势、我们需要更方便的工具来监控咱们的应用状态,常用的有:

例如prometheus, grafana, open tracing, open telementry等开源的监控工具。

还有比较常用的开源监控:例如zabbix,更适合硬件类、物理机、交换机等

下面我们来探究 技术栈: Java + Spring Boot为例, 来详细阐述应用监控的最佳实践

监控软件选型

采集, 存储, 查询: Prometheus

在云原生领域, Prometheus几乎已经是监控的标准, 它也有着丰富的生态, 支持从操作系统, 数据库, 到各类中间件的监控, 对各种语言也都提供了相应的SDK, 关于Prometheus的介绍我们不再赘述. 本文就选择Prometheus作为基础的监控软件, 围绕它展开监控最佳实践
但Prometheus也有它的问题: 如数据无法长时间存储, 大数据量下无法拓展, 只能单机运行等
而SLS的时序存储恰好解决了这些问题, 支持PromQL, 并且支持Prometheus的API, 可以直接替换开源的Prometheus的存储查询层

可视化: Grafana

Prometheus自身提供的可视化能力很弱, 因此通常会选择Grafana对接Prometheus去配置dashboard, 这两个可以说是最佳搭档

通知: alertmanager

alertmanager是Prometheus提供的一个组件, 它提供了丰富的通知路由, 发送方式

监控分层

按监控对象分:
操作系统监控(OS)
中间件/数据库监控(MySQL, Kafka, ES, Hbase, etc)
应用监控(JVM, Tomcat, Spring)
业务监控(SDK, Log)

按数据流分:

暴露指标 -> 采集 -> 存储 -> 可视化 -> 分析 -> 报警

 

主机监控

主机监控, 即操作系统层面的监控有很多种姿势:

  1. Prometheus官方有node_exporter, 如果你是非阿里云的机器, 跑的是主流的Linux, 那么这是比较推荐的方式来暴露指标
  2. 云主机有自带的监控插件
  3. 非云主机可以使用zabbix,或者方法一,也可以使用云厂商的插件,在云平台可视化。

中间件/数据库监控

也可以去复习我前面提到的中间件的日志采集监控:

各组件exporter+promethus+grafana_向往风的男子的博客-优快云博客

各类主流的开源中间件通常也都和开源的监控软件结合的比较好, 例如MySQL可以使用telegraf进行采集, Prometheus exporter对大部分开源软件的支持也都比较好: Exporters and integrations | Prometheus
同主机监控, 如果你使用的是阿里云提供的数据库或中间件, 那很可能云监控上已经有相关的指标, 因此也可以选择导入云监控数据

下面进入正题:应用监控

应用监控

Spring Boot Actuator

Spring Boot作为最主流的Java Web框架, 自然也少不了对监控的支持, 那就是Actuator, 要使用Actuator需要先添加依赖:

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>

Actuator默认提供了13个接口:


这些接口默认只开放了/heath/info, 可以修改配置打开其他的接口:

management:
  endpoints:
    web:
      exposure:
        include: '*'

在Spring Boot 2.0以上, 它使用了micrometer作为底层的度量工具, micrometer是监控度量的门面, 相当于slf4j在日志框架中的作用,它能支持按照各种格式来暴露数据, 其中就有Prometheus.
我们引入一个很小的依赖来暴露Prometheus数据:

<dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
            <version>1.1.3</version>
</dependency>

这个依赖的作用就是会开启一个endpoint, 输出兼容Prometheus exporter的结果, 方便Prometheus来采集
同时记得修改spring boot配置:

server:
  port: 8080
spring:
  application:
    name: spring-demo
management:
  endpoints:
    web:
      exposure:
        include: 'prometheus' # 暴露/actuator/prometheus
  metrics:
    tags:
      application: ${spring.application.name} # 暴露的数据中添加application label

然后启动应用, 访问http://localhost:8080/actuator/prometheus 应该会得到如下结果:

# HELP jvm_memory_committed_bytes The amount of memory in bytes that is committed for the Java virtual machine to use
# TYPE jvm_memory_committed_bytes gauge
jvm_memory_committed_bytes{application="spring-demo",area="heap",id="PS Eden Space",} 1.77733632E8
jvm_memory_committed_bytes{application="spring-demo",area="nonheap",id="Metaspace",} 3.6880384E7
jvm_memory_committed_bytes{application="spring-demo",area="heap",id="PS Old Gen",} 1.53092096E8
jvm_memory_committed_bytes{application="spring-demo",area="heap",id="PS Survivor Space",} 1.4680064E7
jvm_memory_committed_bytes{application="spring-demo",area="nonheap",id="Compressed Class Space",} 5160960.0
jvm_memory_committed_bytes{application="spring-demo",area="nonheap",id="Code Cache",} 7798784.0
# HELP jvm_classes_unloaded_classes_total The total number of classes unloaded since the Java virtual machine has started execution
# TYPE jvm_classes_unloaded_classes_total counter
jvm_classes_unloaded_classes_total{application="spring-demo",} 0.0
# HELP jvm_memory_max_bytes The maximum amount of memory in bytes that can be used for memory management
jvm_memory_max_bytes{application="spring-demo",area="nonheap",id="Code Cache",} 2.5165824E8
# HELP jvm_classes_loaded_classes The number of classes that are currently loaded in the Java virtual machine
# TYPE jvm_classes_loaded_classes gauge
jvm_classes_loaded_classes{application="spring-demo",} 7010.0
# HELP jvm_threads_daemon_threads The current number of live daemon threads
# TYPE jvm_threads_daemon_threads gauge
jvm_threads_daemon_threads{application="spring-demo",} 24.0
# HELP jvm_threads_states_threads The current number of threads having NEW state

# 太长, 后面省略

这就是Prometheus exporter的格式

JVM监控

我们看到里面暴露了很详细的jvm的指标, 这样我们就可以来配置jvm监控了
首先Prometheus需要增加对http://localhost:8080/actuator/prometheus的采集, 我们修改一下配置:

global:
  scrape_interval: 15s
scrape_configs:
  - job_name: "spring-demo"
    metrics_path: "/actuator/prometheus"
    static_configs:
    - targets: ["localhost:8080"]

启动Prometheus, 没报错的话应该就已经在正常采集了, 我们访问prometheus的web ui看一下数据:http://localhost:9090/graph


看到这样的结果说明数据采集正常, 写入Prometheus后通过他的remote write协议可以把数据持久化到SLS中, 可参见文档: 采集Prometheus监控数据
然后可以把SLS时序库作为Grafana的数据源来配置, 同样有相关文档: 时序数据对接Grafan
那么一切准备就绪了, 我们就可以开始配置dashboard了, 我们在grafana.com上传了一个模板dashboard, 直接在grafana中导入即可:
选择+ -> Import -> 粘贴url:
1 SLS JVM监控大盘 | Grafana Labs

这里提一下,本人在这里没有使用阿里云的sls日志服务,直接将promethus作为数据源在grafana上可视化即可,有需要查看原始数据的,可以将其放到阿里云上面,也可以自建一个elk,导到自己自建的elk上,方便自己查看原始日志。
然后选择上面创建的Prometheus数据源, 即可导入, 完整的效果如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值