基于prometheus搭建SpringBoot应用监控

安装Prometheus

  1. 下载Prometheus相关安装包 https://prometheus.io/download/

  2. 修改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).
    
    # Alertmanager configuration
    alerting:
      alertmanagers:
        - static_configs:
            - targets:
              # - alertmanager:9093
    
    # 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:
      # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
      - job_name: "prometheus"
    
        # metrics_path defaults to '/metrics'
        # scheme defaults to 'http'.
    
        static_configs:
          - targets: ["localhost:9090"]
          
      - job_name: 'spring-boot-actuator-exporter'
        # 指定抓取的路径 默认Spring Aactua暴露端点地址如下
        metrics_path: '/actuator/prometheus'
        static_configs:
        # 配置需要监控的应用地址
        - targets: ['localhost:8080','localhost:8081']
        # 自定义标签
          labels:
            nodename: 'spring-boot-actuator'
    
    
  3. 启动prometheus

    bash ./prometheus.sh
    
  4. 查看prometheus WEB界面保证启动成功 http://localhost:9090

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-H2oV7xFk-1655264375552)(https://gitee.com/jianimaoju/image-storage-service/raw/master/image/202206/15/110224-997938.png)]

搭建SpringBoot项目

  1. 创建SpringBoot应用

  2. 添加pom依赖

        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>io.micrometer</groupId>
                <artifactId>micrometer-registry-prometheus</artifactId>
                <scope>runtime</scope>
            </dependency>
        </dependencies>
    
  3. 修改application配置文件

    spring:
      application:
        name: promethus_springboot
    
    management:
      endpoint:
        prometheus:
          enabled: true
      metrics:
        tags:
          application: ${spring.application.name}
      endpoints:
        web:
          exposure:
            include: prometheus # 暴露prometheus端点
    
  4. 查看端点信息是否正常 访问http://localhost:8080/actuator/prometheus

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pVMJVVpV-1655264375553)(https://gitee.com/jianimaoju/image-storage-service/raw/master/image/202206/15/105156-832585.png)]

Spring Boot自定义监控项

业务系统相关统计项一般用基于Spring Bean去实现,建议通过实现MeterBinder接口自动注册到Spring容器中

统计工具参考Meter框架中一些常用的统计工具 如Counter Gauge Timer等

@Component
public class MyMeterBinder implements MeterBinder {
    // 普通的Spring Service可用于业务系统自定义
    @Autowired
    private StatiscService statiscService;

    @Value("${spring.application.name}")
    private String applicationName;

    @Override
    public void bindTo(MeterRegistry meterRegistry) {
        Gauge.builder("MyGauge", () -> statiscService.statisticsTest())
                .baseUnit("ge")
                .description("测试我的prometheus gauge")
                .strongReference(true)
                .tag("application", applicationName)
                .register(meterRegistry);
    }
}

@Service
public class StatiscService {

    public Long statisticsTest() {
        long randomLong = ThreadLocalRandom.current().nextLong(500L);
        System.out.println(randomLong);
        return randomLong;
    }
}

通过端点正确性访问http://localhost:8080/actuator/prometheus

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6hUqSG3N-1655264375553)(https://gitee.com/jianimaoju/image-storage-service/raw/master/image/202206/15/112756-901437.png)]

prometheus上查询结果如下

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pzEklYdY-1655264375553)(https://gitee.com/jianimaoju/image-storage-service/raw/master/image/202206/15/112823-868888.png)]

安装grafana实现监控可视化

  1. 下载grafana https://grafana.com/get/?plcmt=top-nav&cta=downloads&tab=self-managed

  2. 启动grafana bash ./grafana-server.sh

  3. 登陆grafana http://localhost:3000/ admin/admin

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0JEfeT2Z-1655264375554)(https://gitee.com/jianimaoju/image-storage-service/raw/master/image/202206/15/113259-965382.png)]

  4. 配置Datasource

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QF6ydbed-1655264375554)(https://gitee.com/jianimaoju/image-storage-service/raw/master/image/202206/15/113555-180186.png)]

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hah4230Z-1655264375554)(https://gitee.com/jianimaoju/image-storage-service/raw/master/image/202206/15/113627-709824.png)]

  5. 下载SpringBoot相关的Dashboard的json https://grafana.com/grafana/dashboards/

  6. 搜索需要的仪表盘 以SpringBoot为例

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0yj4Yu2b-1655264375554)(https://gitee.com/jianimaoju/image-storage-service/raw/master/image/202206/15/113420-18094.png)]

  7. 下载Json

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FP4f4QZh-1655264375554)(https://gitee.com/jianimaoju/image-storage-service/raw/master/image/202206/15/113445-974982.png)]

  8. 导入仪表盘

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vkOdQAF0-1655264375555)(https://gitee.com/jianimaoju/image-storage-service/raw/master/image/202206/15/113723-572783.png)]

  9. 选择数据源和分类

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tsEWqyJ0-1655264375555)(https://gitee.com/jianimaoju/image-storage-service/raw/master/image/202206/15/113751-414673.png)]
    在这里插入图片描述

  10. 查看仪表盘效果
    在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值