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

相关推荐
打工的小王17 分钟前
Spring Boot(三)Spring Boot整合SpringMVC
java·spring boot·后端
毕设源码-赖学姐19 分钟前
【开题答辩全过程】以 高校体育场馆管理系统为例,包含答辩的问题和答案
java·spring boot
vx_Biye_Design21 分钟前
【关注可免费领取源码】房屋出租系统的设计与实现--毕设附源码40805
java·spring boot·spring·spring cloud·servlet·eclipse·课程设计
翱翔-蓝天1 小时前
为什么“看起来很规范”的后端项目反而臃肿且性能下降
spring boot
80530单词突击赢2 小时前
JavaWeb进阶:SpringBoot核心与Bean管理
java·spring boot·后端
long3163 小时前
Aho-Corasick 模式搜索算法
java·数据结构·spring boot·后端·算法·排序算法
独断万古他化3 小时前
【SSM开发实战:博客系统】(三)核心业务功能开发与安全加密实现
spring boot·spring·mybatis·博客系统·加密
rannn_1113 小时前
【苍穹外卖|Day4】套餐页面开发(新增套餐、分页查询、删除套餐、修改套餐、起售停售)
java·spring boot·后端·学习
qq_12498707533 小时前
基于JavaWeb的大学生房屋租赁系统(源码+论文+部署+安装)
java·数据库·人工智能·spring boot·计算机视觉·毕业设计·计算机毕业设计
倒流时光三十年4 小时前
SpringBoot 数据库同步 Elasticsearch 性能优化
数据库·spring boot·elasticsearch