Spring Cloud 集成 Prometheus Grafana

本文默认认为读者已搭建好完整的Spring Cloud 服务,本文仅在已有服务集群中引入 Prometheus 监控项目

注册发现基于Eureka

  1. Eureka Server 导入依赖
    Prometheus 不支持 Eureka ,需要使用consul 适配器模拟consul服务
        <dependency>
            <groupId>at.twinformatics</groupId>
            <artifactId>eureka-consul-adapter</artifactId>
            <version>1.3.0</version>
        </dependency>

业务服务修改

  1. 引入依赖

        <!-- Micrometer Prometheus registry  -->
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
        </dependency>

  1. 添加Bean

    @Configuration
    @ConditionalOnClass({MeterRegistry.class})
    static class MeterRegistryConfig {
        @Value("${spring.application.name}")
        private String applicationName = "APP_NAME";

        @Bean
        MeterRegistryCustomizer<MeterRegistry> appMetricsCommonTags() {
            return registry -> registry.config().commonTags("application", applicationName);
        }
    }
  1. application.yml 添加配置
    开放 Prometheus Endpoint
management:
  endpoints:
    web:
      # https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#production-ready-endpoints-exposing-endpoints
      exposure:
        include:  prometheus

安装 Prometheus

配置文件:
# my global config                                                                                                                                                           
global:                                                                                                                                                                      
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.                                                                         
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.                                                                                 
  # scrape_timeout is set to the global default (10s).                                                                                                                       
                                                                                                                                                                             
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.                                                                              
rule_files:                                                                                                                                                                  
  # - "first_rules.yml"                                                                                                                                                      
  # - "second_rules.yml"                                                                                                                                                     
                                                                                                                                                                             
# A scrape configuration containing exactly one endpoint to scrape:                                                                                                          
# Here it's Prometheus itself.                                                                                                                                               
scrape_configs:                                                                                                                                                              
  - job_name: 'spring-actuator'                                                                                                                                              
    metrics_path: '/actuator/prometheus'                                                                                                                                     
    scrape_interval: 5s                                                                                                                                                      
    scheme: http                                                                                                                                                             
    consul_sd_configs:                                                                                                                                                       
    #consul 地址                                                                                                                                                             
      - server: '192.168.1.3:8761'                                                                                                                                        
        #username: 'username'    eureka 如有权限控制需要打开                                                                                                                                               
        #password: 'pwd'                                                                                                                                                   
        scheme: http                                                                                                                                                         
        services: [SERVICE_NAME1,SERVICE_NAME2]         
        
启动脚本

docker run -d --net host -v /data/prometheus-data:/prometheus-data prom/prometheus --config.file=/prometheus-data/prometheus.yml

安装 Grafana

启动脚本

docker run
-d
–net host
–name=grafana
-e “GF_SERVER_ROOT_URL=http://192.168.1.2:3000”
-e “GF_SECURITY_ADMIN_PASSWORD=YOUR_PWD”
grafana/grafana

添加 Spring Boot 监控面板
  1. 打开 https://grafana.com/dashboards 搜索 spring
    在这里插入图片描述
  2. 选中Spring Boot 2.1 Statistics面板插件

在这里插入图片描述

  1. 此插件ID为 10280
  2. 打开已安装的Grafana

在这里插入图片描述

  1. 填写ID
    在这里插入图片描述
  2. 选择数据源
    在这里插入图片描述
  3. 界面如下
    在这里插入图片描述

关注公众号获取最新资讯
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值