Spring Boot中使用Micrometer进行指标监控

本文涉及的产品
EMR Serverless StarRocks,5000CU*H 48000GB*H
可观测可视化 Grafana 版,10个用户账号 1个月
可观测监控 Prometheus 版,每月50GB免费额度
简介: Spring Boot中使用Micrometer进行指标监控

Spring Boot中使用Micrometer进行指标监控

今天我们来聊一聊如何在Spring Boot中使用Micrometer进行指标监控。

1. 引言

在现代应用中,监控是确保系统健康和性能的关键。Micrometer是一个应用度量库,专为JVM应用设计,支持多种监控系统,如Prometheus、Grafana、New Relic等。Micrometer提供了一致的API,帮助开发者在应用中轻松收集指标并进行监控。本文将介绍如何在Spring Boot项目中集成和使用Micrometer进行指标监控。

2. 引入依赖

首先,我们需要在pom.xml中添加Micrometer的依赖。

<dependencies>
    <!-- Micrometer Core -->
    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-core</artifactId>
    </dependency>
    <!-- Micrometer for Prometheus -->
    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-registry-prometheus</artifactId>
    </dependency>
    <!-- Spring Boot Actuator -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

3. 配置Micrometer

在Spring Boot中,配置Micrometer非常简单。通过Spring Boot Actuator,我们可以轻松集成Micrometer并配置各种监控系统。

application.yml中,添加如下配置来启用Prometheus:

management:
  endpoints:
    web:
      exposure:
        include: health,info,prometheus
  metrics:
    export:
      prometheus:
        enabled: true

4. 创建自定义指标

Micrometer允许我们定义自定义指标。我们可以通过@Bean注解和MeterRegistry来创建和注册自定义指标。以下是一个示例:

package cn.juwatech.monitoring;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MetricsConfig {
   

    @Bean
    public Counter exampleCounter(MeterRegistry registry) {
   
        return Counter.builder("example.counter")
                .description("An example counter")
                .register(registry);
    }
}

在上述代码中,我们定义了一个名为example.counter的计数器,并将其注册到MeterRegistry中。

5. 在应用中使用自定义指标

接下来,我们可以在应用中使用这个自定义计数器。以下是一个控制器示例:

package cn.juwatech.controller;

import io.micrometer.core.instrument.Counter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MetricsController {
   

    private final Counter exampleCounter;

    @Autowired
    public MetricsController(Counter exampleCounter) {
   
        this.exampleCounter = exampleCounter;
    }

    @GetMapping("/increment")
    public String incrementCounter() {
   
        exampleCounter.increment();
        return "Counter incremented";
    }
}

在这个控制器中,每次调用/increment端点时,exampleCounter计数器都会增加1。

6. 查看指标

启动应用后,可以访问http://localhost:8080/actuator/prometheus查看Prometheus格式的指标输出。在这些指标中,我们可以看到example.counter的值。

7. 集成Prometheus和Grafana

为了更好地展示和分析指标数据,我们可以将Prometheus和Grafana集成到我们的系统中。

7.1 配置Prometheus

创建一个prometheus.yml文件,添加以下配置:

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'spring-boot'
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ['host.docker.internal:8080']
7.2 运行Prometheus和Grafana

使用Docker运行Prometheus和Grafana:

docker run -d --name prometheus -p 9090:9090 -v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
docker run -d --name grafana -p 3000:3000 grafana/grafana
7.3 配置Grafana

在Grafana中,添加Prometheus数据源,并创建仪表板来展示我们在应用中收集的指标。

8. 总结

通过本文的介绍,我们了解了如何在Spring Boot中集成和使用Micrometer进行指标监控。Micrometer提供了一致且强大的API,支持多种监控系统,使得应用的监控和性能分析变得更加容易。在实际开发中,我们可以根据需求创建和注册自定义指标,通过Prometheus和Grafana等工具进行可视化和分析,从而更好地保证系统的健康和性能。

相关实践学习
通过可观测可视化Grafana版进行数据可视化展示与分析
使用可观测可视化Grafana版进行数据可视化展示与分析。
相关文章
|
5天前
|
SQL 监控 druid
springboot-druid数据源的配置方式及配置后台监控-自定义和导入stater(推荐-简单方便使用)两种方式配置druid数据源
这篇文章介绍了如何在Spring Boot项目中配置和监控Druid数据源,包括自定义配置和使用Spring Boot Starter两种方法。
|
3月前
|
监控 Java 数据库连接
Spring Boot中的健康检查和监控
Spring Boot中的健康检查和监控
消息中间件 缓存 监控
81 0
|
2月前
|
Java Spring 监控
Spring Boot Actuator:守护你的应用心跳,让监控变得触手可及!
【8月更文挑战第31天】Spring Boot Actuator 是 Spring Boot 框架的核心模块之一,提供了生产就绪的特性,用于监控和管理 Spring Boot 应用程序。通过 Actuator,开发者可以轻松访问应用内部状态、执行健康检查、收集度量指标等。启用 Actuator 需在 `pom.xml` 中添加 `spring-boot-starter-actuator` 依赖,并通过配置文件调整端点暴露和安全性。Actuator 还支持与外部监控工具(如 Prometheus)集成,实现全面的应用性能监控。正确配置 Actuator 可显著提升应用的稳定性和安全性。
53 0
|
2月前
|
Java Spring 容器
【Azure Spring Cloud】在Azure Spring Apps上看见 App Memory Usage 和 jvm.menory.use 的指标的疑问及OOM
【Azure Spring Cloud】在Azure Spring Apps上看见 App Memory Usage 和 jvm.menory.use 的指标的疑问及OOM
|
3月前
|
监控 druid Java
spring boot 集成配置阿里 Druid监控配置
spring boot 集成配置阿里 Druid监控配置
190 6
|
3月前
|
监控 Java 微服务
Spring Boot微服务部署与监控的实战指南
【7月更文挑战第19天】Spring Boot微服务的部署与监控是保障应用稳定运行和高效维护的重要环节。通过容器化部署和云平台支持,可以实现微服务的快速部署和弹性伸缩。而利用Actuator、Prometheus、Grafana等监控工具,可以实时获取应用的运行状态和性能指标,及时发现并解决问题。在实际操作中,还需根据应用的具体需求和场景,选择合适的部署和监控方案,以达到最佳效果。
|
3月前
|
Prometheus 监控 Cloud Native
使用Spring Boot和Prometheus进行监控
使用Spring Boot和Prometheus进行监控
|
3月前
|
运维 Prometheus 监控
Spring Boot中使用Actuator监控应用状态
Spring Boot中使用Actuator监控应用状态
|
3月前
|
监控 Java Spring
Spring Boot中使用Actuator进行监控
Spring Boot中使用Actuator进行监控