Chiriri's blog Chiriri's blog
首页
  • Java

    • JavaSE
    • JavaEE
    • 设计模式
  • Python

    • Python
    • Python模块
    • 机器学习
  • Golang

    • Golang
    • gRPC
  • 服务器

    • Linux
    • MySQL
    • NoSQL
    • Kubernetes
  • 项目

    • 传智健康
    • 畅购商城
  • Hadoop生态

    • Hadoop
    • Zookeeper
    • Hive
    • Flume
    • Kafka
    • Azkaban
    • Hbase
    • Scala
    • Spark
    • Flink
  • 大数据项目

    • 离线数仓
  • 青训营

    • 第四届青训营
  • HTML

    • HTML
    • JavaScript
  • Vue

    • Vue2
    • TypeScript
    • Vue3
    • Uni-APP
  • 数据结构与算法
  • C语言
  • 考研数据结构
  • 计算机组成原理
  • 计算机操作系统
  • Java基础

    • Java基础
    • Java集合
    • JUC
    • JVM
  • 框架

    • Spring
    • Dubbo
    • Spring Cloud
  • 数据库

    • MySQL
    • Redis
    • Elasticesearch
  • 消息队列

    • RabbitMQ
    • RocketMQ
  • 408

    • 计算机网络
    • 操作系统
    • 算法
  • 分类
  • 标签
  • 归档
  • 导航站
GitHub (opens new window)

Iekr

苦逼后端开发
首页
  • Java

    • JavaSE
    • JavaEE
    • 设计模式
  • Python

    • Python
    • Python模块
    • 机器学习
  • Golang

    • Golang
    • gRPC
  • 服务器

    • Linux
    • MySQL
    • NoSQL
    • Kubernetes
  • 项目

    • 传智健康
    • 畅购商城
  • Hadoop生态

    • Hadoop
    • Zookeeper
    • Hive
    • Flume
    • Kafka
    • Azkaban
    • Hbase
    • Scala
    • Spark
    • Flink
  • 大数据项目

    • 离线数仓
  • 青训营

    • 第四届青训营
  • HTML

    • HTML
    • JavaScript
  • Vue

    • Vue2
    • TypeScript
    • Vue3
    • Uni-APP
  • 数据结构与算法
  • C语言
  • 考研数据结构
  • 计算机组成原理
  • 计算机操作系统
  • Java基础

    • Java基础
    • Java集合
    • JUC
    • JVM
  • 框架

    • Spring
    • Dubbo
    • Spring Cloud
  • 数据库

    • MySQL
    • Redis
    • Elasticesearch
  • 消息队列

    • RabbitMQ
    • RocketMQ
  • 408

    • 计算机网络
    • 操作系统
    • 算法
  • 分类
  • 标签
  • 归档
  • 导航站
GitHub (opens new window)
  • Hadoop

  • Zookeeper

    • 概述
    • 集群搭建
    • 客户端命令行操作
    • 内部原理
    • Api
  • Hive

  • Flume

  • Kafka

  • Azkaban

  • Hbase

  • Scala

  • Spark

  • Flink

  • 离线数仓

  • 青训营

  • DolphinScheduler

  • Doris

  • 大数据
  • Zookeeper
Iekr
2021-10-18

Api

# Api

坐标

<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-core</artifactId>
			<version>2.8.2</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.5.7</version>
		</dependency>
</dependencies>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

log4j.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  
1
2
3
4
5
6
7
8

test

package com.atguigu.zkclient;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class ZkClient {
    private ZooKeeper zooKeeper = null;

    @Before
    public void before() throws IOException {
        String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
        int sessionTimeout = 2000;
        //创建zk对象
        zooKeeper = new ZooKeeper(connectString,  //连接地址
                sessionTimeout,  //超时时间
                new Watcher() {
                    //Zookeeper监听的回调函数
                    @Override
                    public void process(WatchedEvent watchedEvent) {
                        System.out.println("回调函数");
                    }
                }


        );
    }

    @After
    public void after() throws InterruptedException {
        zooKeeper.close();
    }

    //创建新节点
    @Test
    public void create() throws IOException, InterruptedException, KeeperException {

        zooKeeper.create(
                "/testApi", //节点名
                "haha".getBytes(), //值
                ZooDefs.Ids.OPEN_ACL_UNSAFE,  //访问控制列表 相对应权限
                CreateMode.PERSISTENT  //永久节点
        );
    }

    //查询子节点
    @Test
    public void ls() throws InterruptedException, KeeperException {
        List<String> children = zooKeeper.getChildren(
                "/",
                new Watcher() {
                    @Override
                    public void process(WatchedEvent watchedEvent) {
                        System.out.println("自定义回调函数");
                    }
                }
        );
        for (String child : children) {
            System.out.println(child);
        }

        Thread.sleep(Long.MAX_VALUE);
    }

    //查询节点的值
    @Test
    public void get() throws InterruptedException, KeeperException, IOException {
        Stat stat = new Stat();  //节点状态(Stat结构体)
        byte[] data = zooKeeper.getData("/testApi", false, stat);
        System.out.write(data);
        System.out.println();
        System.out.println(stat.getMzxid());
    }

    //查询一个节点的状态
    @Test
    public void stat() throws InterruptedException, KeeperException {
        Stat stat = zooKeeper.exists("/testApi", false);
        if (stat == null) {
            System.out.println("节点不存在");
        } else {
            System.out.println(stat);
        }
    }

    //修改节点内容
    @Test
    public void set() throws InterruptedException, KeeperException {
        String node = "/testApi";
        int version = 0;
        Stat stat = zooKeeper.exists(node, false);

        if (stat == null) {
            System.out.println("节点不存在");
        } else {
            version = stat.getVersion();
        }
        //乐观锁 修改前先比较版本号 不一致则报错
        zooKeeper.setData(node,
                "123".getBytes(StandardCharsets.UTF_8),
                version
        );
    }

    //删除节点
    @Test
    public void delete() throws InterruptedException, KeeperException {
        zooKeeper.delete("/testApi",1);
    }



}
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
编辑 (opens new window)
上次更新: 2023/12/06, 01:31:48
内部原理
介绍

← 内部原理 介绍→

最近更新
01
k8s
06-06
02
进程与线程
03-04
03
计算机操作系统概述
02-26
更多文章>
Theme by Vdoing | Copyright © 2022-2025 Iekr | Blog
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式