当前位置: 首页 > article >正文

3.8 Spring Boot监控:Actuator+Prometheus+Grafana可视化

在Spring Boot应用中,通过整合Actuator、Prometheus和Grafana可以构建完整的监控体系,实现指标采集、存储和可视化。以下是具体实现步骤:


一、Spring Boot Actuator 配置

作用:暴露应用健康指标、性能数据等监控端点。

1. 添加依赖
 

xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
2. 配置Actuator端点(application.yml)
 

yaml

management:
  endpoints:
    web:
      exposure:
        include: "health,info,prometheus"  # 暴露Prometheus格式指标
  endpoint:
    health:
      show-details: always
    prometheus:
      enabled: true
  metrics:
    export:
      prometheus:
        enabled: true
    tags:  # 自定义全局标签(如应用名)
      application: my-spring-app
3. 验证端点

访问 http://localhost:8080/actuator/prometheus,查看原始指标数据。


二、Prometheus 配置

作用:定时抓取Spring Boot的指标数据并存储。

1. 安装Prometheus
 

bash

docker run -d --name prometheus -p 9090:9090 prom/prometheus
2. 配置抓取目标(prometheus.yml)
 

yaml

scrape_configs:
  - job_name: 'spring-boot-app'
    scrape_interval: 15s
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ['host.docker.internal:8080']  # Docker中访问宿主机
        labels:
          application: 'my-spring-app'
3. 重启Prometheus
 

bash

docker restart prometheus
4. 验证数据抓取

访问 http://localhost:9090/targets,确保状态为 ​UP


三、Grafana 配置

作用:可视化展示Prometheus中的监控数据。

1. 安装Grafana
 

bash

docker run -d --name grafana -p 3000:3000 grafana/grafana
2. 添加数据源
  1. 登录Grafana(默认账号:admin/admin)。
  2. Configuration > Data Sources > Add data source,选择 ​Prometheus
  3. 配置URL:http://host.docker.internal:9090(Docker环境)。
3. 导入仪表盘模板
  1. Create > Import,输入官方模板ID:4701(JVM监控)或 11378(Spring Boot)。
  2. 选择数据源为Prometheus,调整时间范围和刷新频率。

四、自定义监控指标

通过Micrometer注册自定义业务指标:

1. 注册计数器
 

java

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;

@RestController
public class MyController {
    private final Counter apiCounter;

    public MyController(MeterRegistry registry) {
        apiCounter = Counter.builder("api.requests.total")
                .description("Total API requests")
                .tag("endpoint", "/my-api")
                .register(registry);
    }

    @GetMapping("/my-api")
    public String myApi() {
        apiCounter.increment();
        return "OK";
    }
}
2. 在Grafana中查询

使用PromQL查询自定义指标:

 

promql

sum(rate(api_requests_total[5m])) by (endpoint)

五、安全加固(可选)

1. 保护Actuator端点
 

java

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/actuator/health").permitAll()
                .antMatchers("/actuator/**").hasRole("ADMIN")
                .and().httpBasic();
    }
}
2. Prometheus认证

prometheus.yml中配置Basic Auth:

 

yaml

basic_auth:
  username: admin
  password: secret

六、监控指标示例

  1. JVM监控
    • 内存使用:jvm_memory_used_bytes
    • 线程数:jvm_threads_live
  2. HTTP请求
    • QPS:http_server_requests_seconds_count
    • 延迟:http_server_requests_seconds_sum
  3. 系统资源
    • CPU使用率:system_cpu_usage
    • 磁盘空间:disk_free_bytes

七、优化与告警

  1. Grafana Alerting:设置阈值触发通知(如CPU > 80%)。
  2. Alertmanager集成:配置邮件、Slack等通知渠道。
  3. 日志联动:结合ELK或Loki实现日志与指标关联分析。

通过以上步骤,可快速搭建Spring Boot应用的监控可视化平台,实时掌握应用健康状态和性能瓶颈。


http://www.kler.cn/a/591141.html

相关文章:

  • 3.17BUUCTF练习day1
  • Java高频面试之集合-14
  • hcia复习
  • Kafka跨集群数据备份与同步:MirrorMaker运用
  • 错排(数学层面)
  • Django:内置和自定义中间件
  • k8s资源管理介绍
  • 在 Visual Studio Code 中高效使用 Pylance:配置、技巧与插件对比
  • 【机器学习】基于conda虚拟环境的gcc、g++版本升级
  • 桌子(table、desk)以及其他常见物体的urdf模型,用于搭建机器人环境如pybullet、Gazebo
  • Vue下载与安装步骤
  • PCIe(Peripheral Component Interconnect Express)详解
  • Feign 调用接口跟调用本地方法一样,这个是怎么实现的?
  • 集成电路制造中LIMS系统的应用 内检LIMS在集成电路的作用
  • 运动焦虑锻炼贴士
  • 数据结构 -- 二叉树的存储结构
  • keepalived的工作原理和脑裂
  • ubuntu24.04虚拟机系统中挂载rootfs.img到rootfs_dir目录,使用chroot切换根目录到roofs_dir报错
  • 深度解析:通过 AIBrix 多节点部署 DeepSeek-R1 671B 模型
  • 传奇怪物素材 8方向高清怪物 PNG格式 游戏怪物 11组