Prometheus + Grafana 监控系统搭建使用指南-接入SpringBoot微服务监控

Prometheus 接入SpringBoot微服务监控

系列文章目录

  1. Prometheus 的安装部署
  2. Grafana的安装部署
  3. Linux服务器接入Prometheus监控-Node Exporter 安装指南
  4. Prometheus 接入SpringBoot微服务监控
  5. Mysql 接入 Prometheus
  6. RocketMQ 接入Prometheus 监控
  7. ElasticSearch 接入 Prometheus
  8. Nacos 接入 Prometheus 监控
  9. Redis 接入 Prometheus 监控系统
  10. Prometheus + Grafana 监控系统-告警规则配置
  11. Prometheus + Grafana 监控系统-PrometheusAlert安装与配置指南

注意:

  • 如果你的项目是SpringBoot 2.x 以上,那么建议基于 SpringBoot 2.x Actuator + Micrometer 来实现监控

  • 如果你的项目是SpringBoot 1.x 以下, 那么建议使用 dropwizard.metrics 来实现

SpringBoot Actuator 2.x + Micrometer 实现接入Prometheus 监控

  • 适用于SpringBoot 2.x 以上的项目
  1. pom.xml
    Springboot 暴露指标数据给 prometheus :
    添加 micrometer-registry-prometheus 依赖,使用 Micrometer 作为你的度量库

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    
    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-registry-prometheus</artifactId>
    </dependency>
    
    • Actuator 提供了一系列内置端点,用于显示运行应用的性能信息,如健康状况、指标等。Micrometer Prometheus registry 会将这些指标格式化为 Prometheus 可读格式。
  2. application.properties
    为了启用micrometer-registry-prometheus的 Prometheus端点,你需要在application.properties中添加以下内容:

# 必须配置
management.endpoints.web.exposure.include=prometheus  
# 也可以暴露更多 health,info,metrics,prometheus
# management.endpoints.web.exposure.include=health,info,metrics,prometheus

# 可选配置
# 配置Prometheus端点的具体路径(默认是/actuator/prometheus)  
# management.endpoints.web.path-mapping.prometheus=/prometheus 
# 如果你想要使用更安全的端点暴露方式,可以配置management.endpoints.web.base-path和security  
# (注意:这需要Spring Security的支持)  
# management.endpoints.web.base-path=/actuator
# management.security.enabled=true
# 配置端点的端口(可选,如果需要与主应用使用不同端口)  
management.server.port=8081  
  • 为了确保安全,你应该遵循以下最佳实践:
    • 不要在 info 端点中包含任何敏感信息。
    • 仔细审查自定义的健康指示器和度量,确保它们不包含敏感数据。
    • 限制对Actuator端点的访问。即使你只暴露了少数几个端点,也应该确保只有受信任的用户或系统可以访问它们。你可以使用Spring Security来配置访问控制。
    • 考虑使用HTTPS来保护Actuator端点的通信。这可以防止中间人攻击和数据在传输过程中的篡改。
    • 监控和审计对Actuator端点的访问,以便及时发现任何可疑活动。
  1. 修改Prometheus配置文件 (prometheus.yml)

    • 方案一: 直接修改
      Prometheus配置文件 (prometheus.yml):
      global:  
        scrape_interval:     15s # 抓取数据的间隔  
        evaluation_interval: 15s # 评估规则的间隔  
        
      scrape_configs:  
        # ECS服务器的Node Exporter配置  
        - job_name: 'ecs-node'  
          static_configs:  
            - targets: ['<ECS_SERVER_IP>:9100'] # 替换<ECS_SERVER_IP>为ECS服务器的IP地址  
              labels:  
                instance: 'ecs-server'  
        
        # 第一个Spring Boot应用程序的Actuator配置  
        - job_name: 'springboot-app-1'  
          metrics_path: '/actuator/prometheus' # 假设Spring Boot Actuator暴露了Prometheus格式的metrics  
          static_configs:  
            - targets: ['<ECS_SERVER_IP>:8080'] # 替换<ECS_SERVER_IP>和端口号(如果端口不是默认的8080)  
              labels:  
                app: 'springboot-app-1'  
        
        # 第二个Spring Boot应用程序的Actuator配置  
        - job_name: 'springboot-app-2'  
          metrics_path: '/actuator/prometheus' # 假设Spring Boot Actuator暴露了Prometheus格式的metrics  
          static_configs:  
            - targets: ['<ECS_SERVER_IP>:8081'] # 替换<ECS_SERVER_IP>和端口号(如果端口不是默认的8080或其他)  
              labels:  
                app: 'springboot-app-2'  
      
    • 方案二: 基于服务发现
      • prometheus.yml
        global:  
          scrape_interval:     15s # 抓取数据的间隔  
          evaluation_interval: 15s # 评估规则的间隔  
          
        scrape_configs:  
          - job_name: "node"
            static_configs:
              - targets: ['xxx-backend-01:9100']
        
          - job_name: "micro-services"
            metrics_path: '/actuator/prometheus'
            file_sd_configs:
              - files: ['targets.yml']
                refresh_interval: 5m
        
        • refresh_interval: 5m 表示五秒检测一次 ‘targets.yml’ 当有变更则直接生效
      • prometheus.yml 同级目录下 targets.yml
        - targets: ['business-management-api.com:20022']
          labels:
            env: stg
            group: micro-services
        
  2. 在Grafana中添加仪表盘

    • springboot 官方: https://grafana.com/grafana/dashboards/10280-microservices-spring-boot-2-1
    • springboot 官方-中文版: https://grafana.com/grafana/dashboards/21319-springboot-apm-dashboard/
      在这里插入图片描述
  • 33
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值