前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【SpringBoot系列】微服务下的指标监测及自定义指标

【SpringBoot系列】微服务下的指标监测及自定义指标

原创
作者头像
Freedom123
发布2024-04-22 16:39:42
1280
发布2024-04-22 16:39:42
举报
文章被收录于专栏:SpringSpring

toc


介绍

可观测性是微服务架构的关键特征,应用程序指标是程序可观察性的一个维度,当应用程序在生产环境中运行时,我们可能想知道各种操作指标,如内存、CPU、线程池使用率等,以及业务指标,例如对特定操作发出了多少请求。

一、配置指标

要添加对指标的支持,我们需要添加maven依赖:

代码语言:shell
复制
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

我们还需要启用指标端点,如下所示 -

代码语言:yaml
复制
management:
  endpoints:
    web:
      exposure:
        include: metrics

下面我们通过url地址访问:

代码语言:shell
复制
curl localhost:8080/actuator/metrics | jq .

获取返回结果如下:

代码语言:json
复制
{
  "names": [
    "application.ready.time",
    "application.started.time",
    "disk.free",
    "disk.total",
    "executor.active",
    "executor.completed",
    "executor.pool.core",
    "executor.pool.max",
    "executor.pool.size",
    "executor.queue.remaining",
    "executor.queued",
    "hikaricp.connections",
    "hikaricp.connections.acquire",
    "hikaricp.connections.active",
    "hikaricp.connections.creation",
    "hikaricp.connections.idle",
    "hikaricp.connections.max",
    "hikaricp.connections.min",
    "hikaricp.connections.pending",
    "hikaricp.connections.timeout",
    "hikaricp.connections.usage",
    "http.server.requests",
    "jdbc.connections.active",
    "jdbc.connections.idle",
    "jdbc.connections.max",
    "jdbc.connections.min",
    "jvm.buffer.count",
    "jvm.buffer.memory.used",
    "jvm.buffer.total.capacity",
    "jvm.classes.loaded",
    "jvm.classes.unloaded",
    "jvm.gc.live.data.size",
    "jvm.gc.max.data.size",
    "jvm.gc.memory.allocated",
    "jvm.gc.memory.promoted",
    "jvm.gc.overhead",
    "jvm.gc.pause",
    "jvm.memory.committed",
    "jvm.memory.max",
    "jvm.memory.usage.after.gc",
    "jvm.memory.used",
    "jvm.threads.daemon",
    "jvm.threads.live",
    "jvm.threads.peak",
    "jvm.threads.states",
    "logback.events",
    "process.cpu.usage",
    "process.files.max",
    "process.files.open",
    "process.start.time",
    "process.uptime",
    "system.cpu.count",
    "system.cpu.usage",
    "system.load.average.1m",
    "tomcat.sessions.active.current",
    "tomcat.sessions.active.max",
    "tomcat.sessions.alive.max",
    "tomcat.sessions.created",
    "tomcat.sessions.expired",
    "tomcat.sessions.rejected"
  ]
}

这些是 spring boot 开箱即用提供的指标。我们可以看到它包括 jvm 内存、线程、cpu 使用率等。

以下请求将显示已使用的 jvm 内存:

请求地址:

代码语言:shell
复制
curl 'http://localhost:8080/actuator/metrics/jvm.memory.max'| jq .

返回相应:

代码语言:shell
复制
 {
  "name": "jvm.memory.max",
  "description": "The maximum amount of memory in bytes that can be used for memory management",
  "baseUnit": "bytes",
  "measurements": [
    {
      "statistic": "VALUE",
      "value": 5620367357
    }
  ],
  "availableTags": [
    {
      "tag": "area",
      "values": [
        "heap",
        "nonheap"
      ]
    },
    {
      "tag": "id",
      "values": [
        "CodeHeap 'profiled nmethods'",
        "G1 Old Gen",
        "CodeHeap 'non-profiled nmethods'",
        "G1 Survivor Space",
        "Compressed Class Space",
        "Metaspace",
        "G1 Eden Space",
        "CodeHeap 'non-nmethods'"
      ]
    }
  ]
}

指标可以有多个维度。例如,jvm.memory.max具有堆大小和非堆大小。我们可以使用其标签向下钻取到指标,例如 .

请求地址:

代码语言:shell
复制
curl 'http://localhost:8080/actuator/metrics/jvm.memory.max?tag=area:heap' | jq .

返回相应:

代码语言:shell
复制
{
  "name": "jvm.memory.max",
  "description": "The maximum amount of memory in bytes that can be used for memory management",
  "baseUnit": "bytes",
  "measurements": [
    {
      "statistic": "VALUE",
      "value": 4294967294
    }
  ],
  "availableTags": [
    {
      "tag": "id",
      "values": [
        "G1 Old Gen",
        "G1 Survivor Space",
        "G1 Eden Space"
      ]
    }
  ]
}

到目前为止,我们知道 spring boot 公开了指标,我们可以请求指标端点来获取这些指标,如果需要,我们可以使用可用的标签向下钻取到此指标。

二、自定义指标

如果我们需要更多指标怎么办?千分尺 (https://micrometer.io/),负责生成和公开指标。MeterRegistry 是容纳多个米的千分尺的核心概念。我们可以简单地在我们的自定义指标提供程序中注入 MeterRegistry 的实例,如下所示:

代码语言:java
复制
@Component
public class InventoryMetrics{
    private Counter inventoryCounter;

    public InventoryMetrics(MeterRegistry meterRegistry){
        inventoryCounter = Counter.builder("products")
                .description("Product Count")
                .register(meterRegistry);
    }

    public void inventoryAdded(int count){
        inventoryCounter.increment(count);
    }
}

在这里,我们创建了一个名为 products 的新指标,每次添加新产品时,我们都会递增值。现在,如果我们请求我们的指标,则会得到产品计数

请求地址:

代码语言:shell
复制
curl 'http://localhost:8080/actuator/metrics/products' | jq .

返回相应:

代码语言:shell
复制
{
  "name": "products",
  "description": "Product Count",
  "baseUnit": null,
  "measurements": [
    {
      "statistic": "COUNT",
      "value": 2
    }
  ],
  "availableTags": []
}

三、指标存储

在生产环境中,我们希望将指标流式传输到数据存储,例如 elasticsearch、influxdb 等。 Spring Boot 支持开箱即用的各种数据接收器。本篇文章中,我在本地运行了一个 influxdb docker 映像,我们可以看到我们的自定义指标被流入推送进来:

在应用程序端,配置如下所示 -

代码语言:yaml
复制
management:
  metrics:
    export:
      influx:
        uri: 'http://localhost:8086'
        token: '<your-token>'
        bucket: '<your bucket>'
        org: '<your org>'
        autoCreateDb: false

四、其他指标

  • 计数器:计数器是单调递增的指标。它可以按固定的量递增,该量始终为正数。
  • 仪表: 仪表是瞬时指标,可以上升或下降。
  • 计时器:计时器是持续时间较短的延迟和事件频率。

小节

本节我们学习的时候微服务中的指标监测,指标是微服务中重要组成部分,Spring Boot 可以轻松收集指标并将其暴露给各种数据接收器。在生产中,我们需要了解指标的数量,如果数量和频率很高,我们的仪表板可能会变得非常慢,所以我们还应该考虑指标数值的保留策略,请不要存储不必要的旧数据。这将有助于节省一些存储空间。

我正在参与2024腾讯技术创作特训营最新征文,快来和我瓜分大奖!

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 介绍
  • 一、配置指标
  • 二、自定义指标
  • 三、指标存储
  • 四、其他指标
  • 小节
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档