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

相关推荐
香吧香1 小时前
Spring boot 中 CommandLineRunner 在服务启动完成后自定义执行
java·spring boot·spring cloud
一 乐1 小时前
美食推荐|基于springboot+vue的美食分享系统设计与实现(源码+数据库+文档)
前端·数据库·vue.js·spring boot·后端·美食
qq_12498707531 小时前
基于springboot+vue+mysql的校园博客系统(源码+论文+部署+安装)
java·vue.js·spring boot·mysql·毕业设计
韩立学长2 小时前
基于Springboot民族文化与旅游网站j9x74dt2(程序、源码、数据库、调试部署方案及开发环境)系统界面展示及获取方式置于文档末尾,可供参考。
数据库·spring boot·旅游
是梦终空2 小时前
计算机毕业设计248—基于Java+Springboot+vue的博物馆预约系统(源代码+数据库+开发文档)
java·spring boot·vue·毕业设计·jwt·博物馆预约系统·博物馆网站
7哥♡ۣۖᝰꫛꫀꪝۣℋ4 小时前
Spring Boot ⽇志
java·spring boot·后端
新手程序员大大4 小时前
springCloudGateway+Nacos注册与转发Netty+WebSocket
spring boot
即将进化成人机4 小时前
springboot项目创建方式
java·spring boot·后端
vx_bisheyuange5 小时前
基于SpringBoot的游戏交易系统
spring boot·后端·游戏·毕业设计
毕设源码-朱学姐5 小时前
【开题答辩全过程】以 基于SpringBoot的流行音乐网站的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端