Bootstrap

Zookeeper学习心得

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

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

一、Zookeeper安装

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

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

二、 zookeeper的数据模型

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

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

Apache ZooKeeper的使用:

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

依赖

		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.4.6</version>
		</dependency>

代码示例:

/**
 * 配置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协议

;