栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

PushGateway + Prometheus + grafana 搭建应用监控demo

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PushGateway + Prometheus + grafana 搭建应用监控demo

1、推送模式:

Prometheus变相的实现了推送数据的方式。

为什么说是变相呢。因为Prometheus获取数据的方式一直是拉取方式,官方并没有提供推送数据的功能。但是官方为了兼容推送这种方式,增加了一个PushGateway组件。

这个组件相当于一个代理服务,独立部署。它没有数据抓取功能,只能被动的等待数据推送。应用把数据推送到PushGateway后,Prometheus再从PushGateway抓取。

适用场景:

  • Prometheus 采用定时拉取模式,可能由于子网络或者防火墙的原因,不能直接拉取各个Target的指标数据,此时可以采用各个Target往PushGateway上推送数据,然后Prometheus去PushGateway上定时拉取
  • 在监控各个业务数据时,需要将各个不同的业务数据进行统一汇总,此时也可以采用PushGateway来统一收集,然后Prometheus来统一拉取
2、搭建

Pushgateway分docker安装和普通安装两种

GitHub:https://github.com/prometheus/pushgateway

比如:

docker pull prom/pushgateway
docker run -d -p 9091:9091 prom/pushgateway

启动之后,游览器访问:http://localhost:9091/#

启动成功!

prometheus 配置文件加入:

- job_name: 'pushgateway'
  scrape_interval: 10s # 每过10秒拉取一次
  honor_labels: true
  static_configs:
  - targets: ['pushgateway:9091']
    labels:
      instance: pushgateway

最终的docker-compose.yaml:

version: '3.4'
services:
  prometheus:
    image: prom/prometheus
    container_name: prometheus
    hostname: prometheus
    ports:
      - 9090:9090
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
  grafana:
    image: grafana/grafana
    container_name: grafana
    hostname: grafana
    ports:
      - 3000:3000
    volumes:  
      - ./grafana.ini:/etc/grafana/grafana.ini
  pushgateway:
    image: prom/pushgateway
    container_name: pushgateway
    hostname: pushgateway
    ports:
      - 9091:9091

prometheus.yml:

global:
  scrape_interval: 10s

scrape_configs:
- job_name: 'pushgateway'
  scrape_interval: 10s # 每过10秒拉取一次
  honor_labels: true
  static_configs:
  - targets: ['pushgateway:9091']
    labels:
      instance: pushgateway

3、java应用接入

加入依赖:


    io.prometheus
    simpleclient_pushgateway
    0.12.0


测试代码:

public static void main(String[] args) throws Exception {
        String url = "localhost:9091";
        PushGateway pg = new PushGateway(url);

        Counter counterDemo = Counter.build()
                .name("counterChanger2").labelNames("nullRequest")
                .help("Counter 实例").register();
        Random random = new Random();
        for (int i = 0; i < 10000; i++) {
            int num = 1;
            int i1 = random.nextInt(1000);
            System.out.println(i1);
            if (i1 < 100) {

                num = 0;
            }
            counterDemo.labels(String.valueOf(num)).inc();
            pg.push(counterDemo, "my_job");
            Thread.sleep(1000);
        }
    }

pushgateway:

出现这个说明数据已经推送到pushagteway

然后查看 prometheus,up即为正常:

然后可以在grafana中创建面板:

1、添加面板

2、编辑PromQL和标题

3、编辑Y坐标单位

4、点击apply,完成保存

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/680544.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号