首页 > 其他分享 >zookeeper_demo

zookeeper_demo

时间:2023-08-12 22:33:38浏览次数:37  
标签:zooKeeper String demo zookeeper new path import

参考:全网最详细的zookeeper实战教程,看完你就知道有多值 - 知乎 (zhihu.com)

ZooKeeper入门,看这篇就够了! - 知乎 (zhihu.com)

windows安装zk:windows环境下安装zookeeper教程详解(单机版)_风轩雨墨的博客-CSDN博客 

zk的watch机制:Zookeeper——Watch机制原理_zookeeper watch机制_庄小焱的博客-CSDN博客

命令行使用zk:

 

javaAPI使用zk:

znode增删改查demo:

CRUDMain.java

package com.hmb;

import org.apache.zookeeper.*;

import java.io.IOException;

public class CRUDMain {
    public static void main(String[] args) throws InterruptedException, KeeperException, IOException {
        String SERVER_HOST = "127.0.0.1:2181";
        int SESSION_TIME_OUT = 2000;
        ZooKeeper zooKeeper = new ZooKeeper(SERVER_HOST, SESSION_TIME_OUT, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                Event.KeeperState state = watchedEvent.getState();
                if (state == Event.KeeperState.SyncConnected) {
                    Event.EventType type = watchedEvent.getType();
                    if (type == Event.EventType.None) {
                        System.out.println("zk client connect server successfully...");
                    }
                }
            }
        });

        String path = "/java";
        zooKeeper.create(path, "HelloWorld".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        System.out.println("add znode successfully.");
        System.out.println(new String(zooKeeper.getData(path, false, null)));
        zooKeeper.setData(path, "hhhh".getBytes(), -1);
        System.out.println(new String(zooKeeper.getData(path, false, null)));
        zooKeeper.delete(path, -1);
        System.out.println(zooKeeper.exists(path, false));
        zooKeeper.close();
    }
}

  

运行结果:

 

服务器上下线动态感知demo:

ZkClient.java

package com.hmb;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

public class ZkClient {
    private static ZooKeeper zooKeeper;
    private static String path = "/";
    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
         zooKeeper = new ZooKeeper("127.0.0.1:2181", 5000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                if (event.getType() == Event.EventType.NodeChildrenChanged && event.getPath().equals("/")) {
                    try {
                        showNodeList();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    } catch (KeeperException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });

         showNodeList();
         Thread.sleep(Long.MAX_VALUE);
    }

    private static void showNodeList() throws InterruptedException, KeeperException {
        List<String> nodeList = zooKeeper.getChildren(path, true);
        System.out.println("====nodeList====");
        for (String node : nodeList) {
            System.out.println("nodeName:" + node);
        }
    }
}

  

先运行程序,然后到客户端中增删znode,回来可见程序成功监控着“/”目录下znode的变化

 

标签:zooKeeper,String,demo,zookeeper,new,path,import
From: https://www.cnblogs.com/hemeiwolong/p/17613041.html

相关文章

  • kafka单机版+zookeeper单机版部署
    部署zookeeper官网下载zookeeper二进制包wgethttps://dlcdn.apache.org/zookeeper/zookeeper-3.8.2/apache-zookeeper-3.8.2-bin.tar.gz解压tarzxvfapache-zookeeper-3.8.2-bin.tar.gz修改配置文件cdapache-zookeeper-3.8.2-bin/confmvzoo_sample.cfgzoo.cfg[root@......
  • ZooKeeper(外部)实例 + SolrCloud(tomcat)实例
    Solr学习(三)单独ZooKeeper(外部)实例+SolrCloud(tomcat)实例博客分类: JavaSolrLucenesolr4.2.0ZooKeeperSolrSolrCloud 开场白:简单讲述如何配置独立的外部ZooKeeper集群管理组件来管理solr集群(多实例solr)本章建立在 Solr学习(一)  、Soer学习(二)基础上......
  • KubeSphere 部署 Zookeeper 实战教程
    前言知识点定级:入门级如何利用AI助手辅助运维工作单节点Zookeeper安装部署集群模式Zookeeper安装部署开源应用选型思想实战服务器配置(架构1:1复刻小规模生产环境,配置略有不同)主机名IPCPU内存系统盘数据盘用途ks-master-0192.168.9.9148501......
  • 分布式服务框架 Zookeeper -- 管理分布式环境中的数据
    简介:Zookeeper分布式服务框架是ApacheHadoop的一个子项目,它主要是用来解决分布式应用中经常遇到的一些数据管理问题,如:统一命名服务、状态同步服务、集群管理、分布式应用配置项的管理等。本文将从使用者角度详细介绍Zookeeper的安装和配置文件中各个配置项的意义,以及分析Zo......
  • prometheus简易推送demo
    publicstaticvoidmain(String[]args)throwsException{InetAddressia=InetAddress.getLocalHost();Map<String,String>map=newHashMap<>();map.put("serverip",ia.getHostAddress());ma......
  • LVGL Demo测试
    准备移植好lvgl的工程一、修改 lv_port_disp.c1、使能lv_port_disp.c修改头晚间包含lv_port_disp_template改为lv_port_disp 编译报错和警告,没有定义水平MY_DISP_HOR_RES、垂直分辨率MY_DISP_VER_RES和最大水平分辨率MY_DISP_VER_RES 通过这里我们可以看到如果我们没......
  • LVGL8.1运行demo
    前言LVGL能实现非常简约美观的UI界面,比如上面这张动图就是LVGL的官方demo,前面移植好显示和触摸后,就可以尝试跑一下lv_demo。有时间还可以研究一下demo是怎么实现的。 一、移植前准备硬件(带触摸屏幕的STM32F407VE核心板)移植好LVGL-8.1显示和触摸的Keil工程,实现屏幕显示、按......
  • zookeeper常见问题解决
     注意:自zk3.5.5版本以后,已编译的jar包,尾部有bin标识,应该使用的是apache-zookeeper-3.x.x-bin.tar.gz 错误一:Startingzookeeper…FAILEDTOSTART版本问题,自3.5以上的版本,随着版本的更新,3.5版本以后的压缩包分成了两种我们需要使用文件名带有bin的那个压缩包,例如:ap......
  • Windows11使用docker desktop安装kafka&zookeeper集群
    docker-compose安装zookeeper集群参考文章:http://t.csdn.cn/TtTYIhttps://blog.csdn.net/u010416101/article/details/122803105?spm=1001.2014.3001.5501准备工作:​ 在开始新建集群之前,新建好文件夹,用来挂载kafka、zookeeper数据,如下:zookeeper文件夹D:\soft\docker\zookee......
  • Zookeeper 集群 + Kafka 集群
    目录一、Zookeeper概述1.Zookeeper定义2.Zookeeper工作机制3.Zookeeper特点4.Zookeeper数据结构5.Zookeeper应用场景6.Zookeeper选举机制(1)第一次启动选举机制(2)非第一次启动选举机制二、部署Zookeeper集群一、Zookeeper概述1.Zookeeper定义Zookeeper是一个开......