• springboot集成zookeeper实现prometheus自动发现


    准备工作
    1、jdk环境安装
    2、zookeeper环境安装
    3、prometheus环境安装
    大家自行百度安装

    1、创建springboot项目,引入maven配置

    prometheus访问/actuator/prometheus

     
                org.springframework.boot
                spring-boot-starter-web
            
            
                org.springframework.boot
                spring-boot-starter-actuator
            
            
                org.apache.curator
                curator-recipes
                4.2.0
            
            
                io.micrometer
                micrometer-registry-prometheus
                
            
            
                org.springframework.boot
                spring-boot-starter-actuator
            
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    2、配置配置文件application.yml

    端口号,服务名称自己自定义就行

    server:
      port: 2222
    
    spring:
      application:
        name: boot-pro
    
    
    
    
    management:
      endpoints:
        web:
          exposure:
            include: "*"
      metrics:
        tags:
          application: ${spring.application.name}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    3、创建zookeeper工具类

    package com.wgl.data.util;
    
    import org.apache.curator.RetryPolicy;
    import org.apache.curator.framework.CuratorFramework;
    import org.apache.curator.framework.CuratorFrameworkFactory;
    import org.apache.curator.retry.ExponentialBackoffRetry;
    import org.apache.zookeeper.CreateMode;
    import org.apache.zookeeper.data.Stat;
    import org.springframework.stereotype.Component;
    
    import java.util.List;
    @Component
    public class ZkClientUtil {
        private static final int BASE_SLEEP_TIME_MS = 5000; //定义失败重试间隔时间 单位:毫秒
        private static final int MAX_RETRIES = 3; //定义失败重试次数
        private static final int SESSION_TIME_OUT = 5000000; //定义会话存活时间,根据业务灵活指定 单位:毫秒
        private static final String ZK_URI = "127.0.0.1:2181";//zookeeper地址
    
        /**
         * 创建连接
         * @return
         */
        public static CuratorFramework build(){
            RetryPolicy retryPolicy = new ExponentialBackoffRetry(BASE_SLEEP_TIME_MS,MAX_RETRIES);
            CuratorFramework client = CuratorFrameworkFactory
                    .builder()
                    .connectString(ZK_URI)
                    .retryPolicy(retryPolicy)
                    .sessionTimeoutMs(SESSION_TIME_OUT)
                    .build();
            return client;
        }
    
        /**
         * 创建节点
         */
        public static void create(final CuratorFramework client, final String path, String nodeData) throws Exception {
    
            client.create().creatingParentsIfNeeded().forPath(path, nodeData.getBytes());
    
        }
    
    
     
    
    
        /**
         * 设置节点值
         */
        public static void setData(final CuratorFramework client, final String path, String nodeData) throws Exception {
    
            client.setData().forPath(path, nodeData.getBytes());
    
        }
    
    
        /**
         * 删除节点
         */
        public static void delete(final CuratorFramework client, final String path) throws Exception {
    
            client.delete().deletingChildrenIfNeeded().forPath(path);
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66

    4、springboot启动类

    我本地启动了两个项目,端口号分别是2222和3333,这里就占一个就行

    @SpringBootApplication
    public class MainApplication {
    
    
        public static void main(String[] args) throws Exception {
            ConfigurableApplicationContext context = SpringApplication.run(MainApplication.class, args);
            ZkClientUtil zk = context.getBean(ZkClientUtil.class);
            CuratorFramework build = zk.build();
            build.start();
            //创建节点
            //{\"serviceEndpoint\": {\"host\": \"127.0.0.1\", \"port\": 2222}, \"additionalEndpoints\": {}, \"status\": \"ALIVE\"} 
            //一定要是这个顺序,不要想着json自己拼装顺序,不然行不通,具体得去看源码
            zk.delete(build,"/prometheus/monitoring/service1");
            zk.create(build,"/prometheus/monitoring/service1","");
            zk.setData(build,"/prometheus/monitoring/service1","{\"serviceEndpoint\": {\"host\": \"127.0.0.1\", \"port\": 2222}, \"additionalEndpoints\": {}, \"status\": \"ALIVE\"}");
            build.close();
    
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    5、修改prometheus配置文件prometheus.yml

    global:
      scrape_interval: 15s
    
    scrape_configs:
      - job_name: "prometheus"
        static_configs:
        - targets: ["localhost:9090"]
      - job_name: "springboot_prometheus" //自定义job名称
        scrape_interval: 5s
        metrics_path: '/actuator/prometheus' //采集的指标地址
        serverset_sd_configs:
          - servers: ['127.0.0.1:2181']  //zookeeper地址
            paths: ['/prometheus/monitoring']  //node path,自动发现
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    6、本地启动项目

    访问localhost:2222/actuator/prometheus和localhost:3333/actuator/prometheus
    查看zookeeper,我使用的工具是ZooInspector
    在这里插入图片描述
    启动prometheus,查看localhost:9090
    两台机器自动发现成功
    在这里插入图片描述

  • 相关阅读:
    6、Linux驱动开发:设备-更简单的设备注册
    《微机原理》——微处理器内部及外部结构
    linux创建用户和组、授权、禁止root远程登录、限制SSH的IP登录
    UVM实战——02构建一个简单的UVM平台_1 UVM平台中的关键组件
    深度学习之基于Python+OpenCV(DNN)性别和年龄识别系统
    开发问题总结
    【quartus13.1/Verilog】swjtu西南交大:计组课程设计
    【每日运维】文件系统损坏:shutting down filesystem
    Flink学习18:算子介绍filter
    Linux的vim自动生成开头
  • 原文地址:https://blog.csdn.net/qq5632281/article/details/126608257