Spring Boot 集成 Zookeeper

本文档展示了如何在SpringBoot项目中使用Zookeeper进行节点的创建、读取、更新、删除等操作,并提供了详细的操作步骤和代码示例。同时,文章总结了Zookeeper的关键API及其参数含义,包括连接Zookeeper、获取子节点、创建和删除节点、获取和修改节点数据以及判断节点是否存在等功能。
摘要由CSDN通过智能技术生成

提前准备:

Spring Boot项目、搭建完成的Zookeeper 虚拟机
该文章前面都有写过。

引入zookeeper 需要的依赖

直接上代码

package com.test.zookeeper;

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

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

public class DeomTest {
    
    private ZooKeeper zookeeper;

    
    @Before
    // 连接zookeeper
    public void init() throws IOException {
        Watcher watcher = new Watcher() {
            // 连接成功之后的回调
            @Override
            public void process(WatchedEvent event) {
                System.out.println(event.getType()+"--"+event.getPath());
            }
        };
        zookeeper = new ZooKeeper("192.168.xxx.xx:2181",5000,watcher);
    }
    
    // 获取子节点
    @Test
    public void getChlid(){
        /**
         * 传入2个参数
         * 1、指定获取那个节点
         * 2、是否使用监听器(watcher),true表示使用以上的监听功能
         */
        try {
            List<String> children = zookeeper.getChildren("/",true);
            for (String chi : children){
                System.out.println("chi=="+ chi);
            }
        }catch (KeeperException e){
            e.printStackTrace();
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }
    
    // 创建节点
    @Test
    public void createChild(){
        String s = null;
        try {
            /**
             * 传入4个参数
             * 1、创建的节点
             * 2、节点数据
             * 3、节点的权限 OPEN_ACL_UNSAFE表示内部应用权限
             * 4、节点类型 4种 持久化节点、带序列的持久化节点、临时节点、带序列的临时节点
             * 持久:PERSISTENT
             * 持久序列节点:PERSISTENT_SEQUENTIAL
             * 临时:EPHEMERAL
             * 临时序列节点:EPHEMERAL_SEQUENTIAL
             */
            s = zookeeper.create("/b","bb".getBytes(),ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
            System.out.println("s==" +s);
        }catch (KeeperException e){
            e.printStackTrace();
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }
    
    // 获取节点
    @Test
    public void getDate(){
        byte[] data = new byte[0];
        
        try {
            data = zookeeper.getData("/b",false,null);
            System.out.println("data=="+ new String(data));
        }catch (KeeperException e){
            e.printStackTrace();
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }
    
    // 删除节点
    @Test
    public void deleteNode(){
        try {
            zookeeper.delete("/b",-1);
            System.out.println("删除成功");
        }catch (KeeperException e){
            e.printStackTrace();
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }
    
    // 修改节点数据
    @Test
    public void updateNode(){
        try {
            zookeeper.setData("/a","aaaa".getBytes(),-1);
            System.out.println("修改完成");
            byte[] data = zookeeper.getData("/a",false,null);
            System.out.println("修改后的值:"+ new String(data));
        }catch (KeeperException e){
            e.printStackTrace();
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }
    
    // 判断节点是否存在
    @Test
    public void testCheckNode(){
        try {
            Stat stat = zookeeper.exists("/a",false);
            System.out.println("节点是否为空==="+ stat );
            if(null == stat){
                System.out.println("不存在");
            }else{
                System.out.println("存在");

            }
        }catch (KeeperException e){
            e.printStackTrace();
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }
}

 

以下是个人总结的一些zookeeper 使用总结

 

/**
     * 总结:
     * 1、:需要连接zookeeper 关键字:Watcher
     * 2:zookeeper自带的方法
     *  2.1:getChildren 获取子节点   getChildren(Stirng path, boolean watcher) 
     *  2.2:create 创建节点          create(Stirng path,byte[] data, List<ACL> acl, CreateMode createMode)
     *  2.3:getData 获取节点         getData(Stirng path, boolean watcher, Stat stat) 
     *  2.4:delete 删除节点          delete(Stirng path, int version);
     *  2.5:setData 修改节点         setData(Stirng path,byte[] data,int version)
     *  2.6:exists 判断节点是否存在   exists(Stirng path, boolean watcher)
     * 3:节点释义:
     *  3.1:path: 操作节点
     *  3.2:watcher:(true/false)是否通知客户端该节点变化
     *  3.3:stat    设置是否监控这个节点数据状态
     *  3.4:data    节点数据(需转换字节)
     *  3.5:acl     节点权限
     *  3.6:createMode  节点类型
     *   3.6.1:持久:PERSISTENT
     *   3.6.2:持久序列节点:PERSISTENT_SEQUENTIAL
     *   3.6.3:EPHEMERAL
     *   3.6.4:临时序列节点:EPHEMERAL_SEQUENTIAL
     *  3.7:version: 版本  -1 表示适应所有版本
     *  
     */

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值