Zookeeper学习心得

本人学zookeeper时按照此文路线学的

Zookeeper学习大纲 - 似懂非懂视为不懂 - 博客园

一、Zookeeper安装

ZooKeeper 入门教程 - Java陈序员 - 博客园

Docker安装Zookeeper教程(超详细)_docker 安装zk-CSDN博客

二、 zookeeper的数据模型

ZooKeeper 入门教程 - Java陈序员 - 博客园

三、使用zookeeper的一些API实现增删改查

Apache ZooKeeper的使用:

原链接找不到是哪个来着了,附上我学习用的代码

依赖

XML 复制代码
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.4.6</version>
		</dependency>

代码示例:

java 复制代码
/**
 * 配置zookeeper
 * yml配置:
 *  zookeeper:
 *      server: 192.168.1.198:2181
 *      timeout: 3000
 */
@Configuration
public class ZookeeperConfig {
    @Value("${zookeeper.server}")
    private String server;

    @Value("${zookeeper.timeout}")
    private Integer timeout;

    @Bean
    public ZooKeeper zkClient() throws IOException {
        return new ZooKeeper(server, timeout, watchedEvent -> {});
    }
}



/**
 * 增删改查
 */
@RestController
@RequestMapping("/api")
public class ZookController {

    @Autowired
    ZooKeeper zkClient;

    @GetMapping("/zookeeper")
    public String getData() throws KeeperException, InterruptedException {
        String path = "/zookeeper";
        boolean watch = true;
        byte[] data = zkClient.getData(path, watch, null);
        return new String(data);
    }

    @GetMapping("/addNode/{nodename}/{data}")
    public String addNode(@PathVariable("nodename")String nodename, @PathVariable("data") String data1){
        // 创建节点的路径
        String path = "/"+nodename;
        // 节点数据
        String data =data1;
        // 权限控制
        List<ACL> aclList = ZooDefs.Ids.OPEN_ACL_UNSAFE;
        // 创建节点的类型
        CreateMode createMode = CreateMode.PERSISTENT;

        String result = null;
        try {
            result = zkClient.create(path, data.getBytes(), aclList, createMode);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    @GetMapping("/getData/{nodename}")
    public String getData(@PathVariable("nodename") String nodename){
        //数据的描述信息,包括版本号,ACL权限,子节点信息等等
        Stat stat = new Stat();
        //返回结果是byte[]数据,getData()方法底层会把描述信息复制到stat对象中
        byte[] bytes;
        String path="/"+nodename;
        try {
            bytes = zkClient.getData(path, false, stat);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        //打印结果
        System.out.println("ZNode的数据data:" + new String(bytes));//Hello World
        System.out.println("获取到dataVersion版本号:" + stat.getVersion());//默认数据版本号是0
        return new String(bytes);
    }
    @GetMapping("/setData/{nodename}/{data}")
    public String setData(@PathVariable("nodename")String nodename, @PathVariable("data") String data1) throws InterruptedException, KeeperException {
        String path = "/"+nodename;
        zkClient.exists(path, new MyWatcher());
        String data = data1;
        // 这里必须先拿到版本号才能更新
        int version =1;

        Stat stat = null;
        try {
            stat = zkClient.setData(path, data.getBytes(), version);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return stat.toString();
    }
    @GetMapping("/deleteNode/{nodename}")
    public String deleteNode(@PathVariable("nodename")String nodename){
        String path = "/"+nodename;
        int version = 0;
        try {
            zkClient.delete(path, version);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "OK!";
    }
}

Curator客户端的使用

ZooKeeper 实战(三) SpringBoot整合Curator-开发使用篇-CSDN博客

四、使用Curator实现分布式锁、分布式id、watch事件监听

ZooKeeper 实战(四) Curator Watch事件监听_curatorwatcher-CSDN博客

ZooKeeper 实战(五) Curator实现分布式锁-CSDN博客

ZooKeeper 实战(六) - 分布式ID实现方案_分布式id生成方案-CSDN博客

五、学习zookeeper集群

六、学习zookeeper的机制和原理

CAP理论

ZAB协议

相关推荐
Nefu_lyh16 小时前
【Spark】05Spark 数据读取与保存:文件格式、文件系统与结构化数据
大数据·分布式·spark
楚山之石16 小时前
【Lancet】BRAID 随机试验:乳腺致密女性补充成像,abbreviated MRI 与 CEM 检出更多早期癌(首轮中期结果)
人工智能·科技·学习·mri·医学影像
Sunflower_ac21 小时前
强化学习学习笔记:Lab 0–Lab 2
笔记·学习
云边有个稻草人21 小时前
集中式与分布式怎么选?谈谈金仓数据库的架构取舍
分布式·国产数据库·架构选型·金仓数据库·集中式数据库·企业级数据库·核心业务系统
东风微鸣1 天前
QuePaxa VS Raft:广域网共识,为什么「慢但不停」胜过「快但一抖就死」
分布式·云原生·raft
Z21-X1 天前
云原生运维学习笔记:Kubernetes 自动扩缩容与资源管控实战
运维·笔记·学习·云原生·kubernetes
wdf80881 天前
面向大规模科学数据可视化的ParaView分布式架构优化与UltraLAB高性能计算平台适配研究
分布式·信息可视化·架构·paraview
崖边看雾1 天前
Kafka 手册
分布式·面试·kafka
可涵不会debug1 天前
ChatServer 模块学习笔记:HTTP 接入层、多线程与流式响应
笔记·学习·http
console.log('npc')2 天前
Python第9课 类学习笔记,前端js语法对照
前端·python·学习