prometheus监控应用数据(二)

prometheus监控应用数据(二)

本文承接上篇文章prometheus监控应用数据(一)。使用开源软件prometheus对上篇文章中提及到的数据进行可视化监控。

  1. 本文使用docker进行prometheus的部署,准备以下两个文件,以及文件内内容如下所示:

    • .env文件

      # Container Timezone 时区
      TZ=Asia/Shanghai
      
      # Prometheus 镜像版本号,映射端口号,
      PROMETHEUS_WEB_PORT=9090
      
    • docker-compose.yml文件

      version: '3.7'
      
      networks:
        demo:
          driver: bridge
      
      services:
        prometheus:
          image: prom/prometheus
          container_name: prometheus
          ports:
            - "${PROMETHEUS_WEB_PORT}:9090"
          environment:
            TZ: "${TZ}"
          volumes:
            # 挂载 prometheus 配置文件到容器中
            - ./prometheus:/etc/prometheus/
            - ./prometheus/prometheus.log:/etc/prometheus/prometheus.log
          command:
            # 指定 Prometheus 使用挂载的配置文件
            - '--config.file=/etc/prometheus/prometheus.yml'
            # 指定 Prometheus 输出日志到挂载的文件夹中
            - '--log.level=info'
            - '--log.format=logfmt'
      
  2. 在上面两个文件所在目录新建prometheus目录,在新建的目录下新建prometheus.logprometheus.yml文件。其中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=<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: "classInfo"
        static_configs:
          # 如果prometheus部署在docker环境中,应用程序部署在宿主机中, 就不要用localhost, 用localhost docker容器无法采集到宿主机
          # 应用进程某端口上的数据
          - targets: ["host.docker.internal:2112"] 
    
  3. 在上面两个文件所在目录下执行命令docker-compose -p classroom up -d,构建prometheus环境,并启动。前提是本机已经安装好docker并已启动。

  4. 启动前一篇文章prometheus监控应用数据(一)中的程序TestPrometheus,模拟业务过程。

  5. 浏览器中访问地址: http://localhost:2112/metrics, 可以看到被监控的业务数据,数据格式参见前一篇文章prometheus监控应用数据(一)

  6. 浏览器中访问地址: http://localhost:9090/ 来访问docker中部署的容器prometheus,访问效果如下

    • 初始界面
      在这里插入图片描述

    • 点击Add Panel按钮,会出来一个面板,点击Execute左边的按钮,会出来一个弹框,选择需要展示的监控指标,指标在前一篇文章prometheus监控应用数据(一)中已经指定,分别是EveryStudentDetailStudentNumber,选择好,点击按钮Execute,即可展示对应指标的数据,如下所示:
      在这里插入图片描述

    在这里插入图片描述

  7. 至此,就完成了对使用prometheus采集业务数据的可视化监视

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值