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

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应用的监控可视化平台,实时掌握应用健康状态和性能瓶颈。

相关文章:

  • 【vue2 + Cesium】使用Cesium、添加第三方地图、去掉商标、Cesium基础配置、地图放大缩小事件、获取可视区域、层级、高度
  • Python第六章01:列表(lsit)定义语法
  • ESP32(3)UDP通信
  • 【Linux篇】:进程抢占式调度的量子纠缠--状态,优先级与上下文切换的三角博弈
  • python基础8 单元测试
  • 【算法】一维差分
  • 【Linux】Makefile秘籍
  • 深度解读 | AI驱动下的新型金融对冲策略:稀疏奖励强化学习的应用
  • 1.angular介绍
  • 第九步:web-js
  • Go基础语法阶段核心内容(5天)
  • ESP32(4)TCP通信
  • 免费实用工具,wps/office/永中通吃!
  • Matlab 高效编程:用矩阵运算替代循环
  • 淘宝商品详情商品评论数据爬取的技术之旅
  • 数据结构 -- 树和二叉树
  • 系统架构图
  • tongweb7控制台无法访问
  • 第59天:Web攻防-XSS跨站反射型存储型DOM型接受输出JS执行标签操作SRC复盘
  • Linux|静态库 共享库
  • 移动互联网未成年人模式正式发布
  • 中国海油总裁:低油价短期影响利润,但也催生资产并购机会
  • 夜读丨跷脚牛肉乐翘脚
  • 总书记考察的上海“模速空间”,要打造什么样的“全球最大”?
  • 成都世运会倒计时100天,中国代表团运动员规模将创新高
  • 暗蓝评《性别打结》丨拆解性别之结需要几步?