java操作zookeeper

java操作zookeeper

文档

  1. linux安装java -centos安装java -linux配置java环境变量
  2. zookeeper单机安装
  3. zookeeper集群安装
  4. zookeeper客户端命令行操作、节点类型及监听器
  5. zookeeper集群写数据原理
  6. java操作zookeeper

依赖信息

xml 复制代码
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.8.2</version>
</dependency>
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.5.7</version>
</dependency>

log4j配置文件

在resources下创建log4j.properties文件

properties 复制代码
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d   %p  [%c]    -   %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d   %p  [%c]    -   %m%n

java操作zookeeper参考代码

  • 包括连接zookeeper、创建节点、获取子节点列表、判断节点是否存在、监听等方法
  • 注释中包含注意事项
java 复制代码
package xin.yangshuai.zookeeper01.client;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;

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

public class ZkClient {

    private ZooKeeper zkClient;

    // 注意:逗号左右不能有空格
    private String connectString = "192.168.145.132:2181,192.168.145.133:2181,192.168.145.134:2181";
    // 2000毫秒
    private int sessionTimeout = 2000;

    /**
     * 连接zookeeper
     *
     * @throws IOException
     */
    public void init() throws IOException {
        zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                // 初始化时,会执行一次
                System.out.println(watchedEvent.getPath() + "-------------");
                try {
                    // 注册一次,监听一次,如果想持续监听,可重新注册
                    // getChildren()方法设置了监听,所以每次调用将重新注册监听
                    getChildren();
                } catch (KeeperException | InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 创建一个节点
     *
     * @throws KeeperException
     * @throws InterruptedException
     */
    public void create() throws KeeperException, InterruptedException {
        String node = zkClient.create("/client", "create-test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        System.out.println("Created " + node);
    }

    /**
     * 获取子节点
     *
     * @throws KeeperException
     * @throws InterruptedException
     */
    public void getChildren() throws KeeperException, InterruptedException {
        // 参数watch设置为true,表示使用zkClient创建时的Watcher,并监听当前节点的子节点的增删变化
        List<String> children = zkClient.getChildren("/", true);
        for (String child : children) {
            System.out.println("children " + child);
        }
    }

    /**
     * 判断节点是否存在
     *
     * @throws KeeperException
     * @throws InterruptedException
     */
    public void exist() throws KeeperException, InterruptedException {
        Stat stat = zkClient.exists("/client", false);
        System.out.println(stat == null ? "not exist" : "exist");
        stat = zkClient.exists("/client1", false);
        System.out.println(stat == null ? "not exist" : "exist");
    }

    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
        ZkClient zkClient = new ZkClient();
        zkClient.init();
        zkClient.create();
        // getChildren()方法不用手动调用,zookeeper客户端初始化时,会调用监听器的process方法,可在该方法中调用getChildren()方法
        // zkClient.getChildren();
        zkClient.exist();

        // 保持运行状态,来观察监听器的动作
        Thread.sleep(Long.MAX_VALUE);

        // 可在命令行增删节点 / 的子节点,来触发监听器动作
    }
}

java中的zookeeper节点类型

properties 复制代码
持久无序号节点:CreateMode.PERSISTENT
持久有序号节点:CreateMode.PERSISTENT_SEQUENTIAL
临时无序号节点:CreateMode.EPHEMERAL
临时有序号节点:CreateMode.EPHEMERAL_SEQUENTIAL
相关推荐
onkel in blog3 小时前
【Docker】Docker Compose方式搭建分布式协调服务(Zookeeper)集群
分布式·docker·zookeeper
dddaidai1233 天前
分布式ID和分布式锁
redis·分布式·mysql·zookeeper·etcd
code在飞6 天前
windows 部署 Kafka3.x KRaft 模式 不依赖 ZooKeeper
windows·分布式·zookeeper·kafka
不会飞的鲨鱼6 天前
Windows系统下使用Kafka和Zookeeper,Python运行kafka(二)
windows·zookeeper·kafka
搞不懂语言的程序员7 天前
Kafka Controller的作用是什么?故障时如何恢复? (管理分区和副本状态;通过ZooKeeper选举新Controller)
分布式·zookeeper·kafka
giser@20117 天前
Zookeeper单机版安装部署
分布式·zookeeper·安装教程·单机部署
giser@20118 天前
ZooKeeper工作机制与应用场景
分布式·zookeeper·云原生
搞不懂语言的程序员9 天前
Kafka的核心组件有哪些?简要说明其作用。 (Producer、Consumer、Broker、Topic、Partition、ZooKeeper)
分布式·zookeeper·kafka
程序员buddha10 天前
SpringBoot+Dubbo+Zookeeper实现分布式系统步骤
分布式·zookeeper·dubbo·springboot
佳腾_18 天前
【分布式系统中的“瑞士军刀”_ Zookeeper】二、Zookeeper 核心功能深度剖析与技术实现细节
分布式·zookeeper·云原生·集群管理·命名服务