• springboot Actuator整合prometheus并使用grafana可视化(prometheus,grafana使用docker搭建)


    springboot 版本2.7.4

    springboot

    依赖

     		<dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-actuatorartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
    
            <dependency>
                <groupId>io.micrometergroupId>
                <artifactId>micrometer-registry-prometheusartifactId>
                <scope>runtimescope>
            dependency>
    

    application.yaml

    management:
      endpoint:
        health:
          show-details: always
      endpoints:
        web:
          exposure:
            include:
              - prometheus
              - health
              - info
    
      metrics:
        tags:
          application: tcoding-demo
    

    main 方法

    @SpringBootApplication
    @RestController
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    
        @GetMapping
        public String hello(){
            return "hello world";
        }
    }
    

    prometheus

    拉取镜像

    docker pull bitnami/prometheus:2.38.0
    

    运行

    方式一:进入容器修改prometheus.yml

     docker run -itd --name prom -p 9090:9090 bitnami/prometheus:2.38.0
    

    进入容器修改prometheus.yml

    docker exec -it -u 0  prom /bin/bash 
    
    apt-get update -y
    
    apt-get install -y vim
    

    修改metrics_path和static_configs

    host.docker.internal 是容器访问宿主机hostname

    scrape_configs:
      # The job name is added as a label `job=` to any timeseries scraped from this config.
      - job_name: "tcoding-demo"
    
        metrics_path: '/actuator/prometheus'
        # scheme defaults to 'http'.
    
        static_configs:
          - targets: ["host.docker.internal:8080"]
    

    然后重启 docker restart prom

    方式二

    本地新建prometheus.yml

    # 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=` to any timeseries scraped from this config.
      - job_name: "tcoding-demo"
    
        metrics_path: '/actuator/prometheus'
        # scheme defaults to 'http'.
    
        static_configs:
          - targets: ["host.docker.internal:8080"]
    
     docker run -itd --name prom -p 9090:9090 \
     -v /your path/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus \
     bitnami/prometheus:2.38.0 
    

    grafana

    docker搭建

    docker pull grafana/grafana:9.1.6 
    
    docker run \
          -itd \
          -p 3000:3000 \
          --name=grafana \
          --link=prom \
          grafana/grafana:9.1.6
    

    grafana配置Prometheus

    访问 http://localhost:3000/
    在这里插入图片描述
    修改数据源url
    在这里插入图片描述

    Dashboards配置

    下载配置

    https://grafana.com/grafana/dashboards/
    在这里插入图片描述

    我是用的是jvm

    在这里插入图片描述
    上传刚刚下载的json文件

    测试

    • 启动springboot

    • grafana切换一下时间范围,太长显示有问题(没有历史数据)
      在这里插入图片描述

    • 效果预览
      在这里插入图片描述

    源码地址

    https://github.com/googalAmbition/hello-spring-boot/tree/main/44-actuator-prometheus

  • 相关阅读:
    冥想第五百一十六天
    Go with Protobuf
    微信小程序---组件开发与使用
    数据比对总结
    【SQL性能优化】Hash索引的底层原理是什么?
    How to install PostgreSQL in Centos8
    期货期权基础知识
    MyBatis-plus组件学习
    【Linux】线程池
    常见的软件测试面试题,千万别答错了
  • 原文地址:https://blog.csdn.net/qq_23934475/article/details/127044915