0    课程地址

https://coding.imooc.com/lesson/201.html#mid=12721

 

1    重点关注

1.1    本节内容

java api客户端删除节点,包含同步修改和异步修改,只做了异步,同步不通用(因为没有回调函数,不知道是否删除成功)也可以参照视频看下

 

1.2    java api 删除节点同步修改

zkServer.getZookeeper().delete("/test-delete-node", 2);

 

 

1.3    java api 删除节点异步调用(回调函数)

        String ctx = "{'delete':'success'}";
        zkServer.getZookeeper().delete("/test-delete-node", 0, new DeleteCallBack(), ctx);


package com.imooc.zk.demo;


import org.apache.zookeeper.AsyncCallback.VoidCallback;

public class DeleteCallBack implements VoidCallback {
    @Override
    public void processResult(int rc, String path, Object ctx) {
        System.out.println("删除节点" + path);
        System.out.println((String)ctx);
    }

}

 

 

 

2    课程内容

 

 

3    Coding

3.1    java api 删除节点异步调用Demo

  • 启动服务端
    进入到
cd /usr/local/zookeeper/bin

 
    重启zookeeper服务端
./zkServer.sh restart

 

  • 运行java类,删除节点
package com.imooc.zk.demo;

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

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Stat;

/**
 * 
 * @Title: ZKConnectDemo.java
 * @Description: zookeeper 操作demo演示
 */
public class ZKNodeOperator implements Watcher {

    private ZooKeeper zookeeper = null;
    
    public static final String zkServerPath = "172.26.139.4:2181";
    public static final Integer timeout = 5000;
    
    public ZKNodeOperator() {}
    
    public ZKNodeOperator(String connectString) {
        try {
            zookeeper = new ZooKeeper(connectString, timeout, new ZKNodeOperator());
        } catch (IOException e) {
            e.printStackTrace();
            if (zookeeper != null) {
                try {
                    zookeeper.close();
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
    
    /**
     * 
     * @Title: ZKOperatorDemo.java
     * @Description: 创建zk节点
     */
    public void createZKNode(String path, byte[] data, List<ACL> acls) {
        
        String result = "";
        try {
            /**
             * 同步或者异步创建节点,都不支持子节点的递归创建,异步有一个callback函数
             * 参数:
             * path:创建的路径
             * data:存储的数据的byte[]
             * acl:控制权限策略
             *             Ids.OPEN_ACL_UNSAFE --> world:anyone:cdrwa
             *             CREATOR_ALL_ACL --> auth:user:password:cdrwa
             * createMode:节点类型, 是一个枚举
             *             PERSISTENT:持久节点
             *             PERSISTENT_SEQUENTIAL:持久顺序节点
             *             EPHEMERAL:临时节点
             *             EPHEMERAL_SEQUENTIAL:临时顺序节点
             */
//            result = zookeeper.create(path, data, acls, CreateMode.EPHEMERAL);
            
            String ctx = "{'create':'success'}";
            zookeeper.create(path, data, acls, CreateMode.PERSISTENT, new CreateCallBack(), ctx);
            
            System.out.println("创建节点:\t" + result + "\t成功...");
            new Thread().sleep(2000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) throws Exception {
        ZKNodeOperator zkServer = new ZKNodeOperator(zkServerPath);
        
        // 创建zk节点
        //zkServer.createZKNode("/testnode", "testnode".getBytes(), Ids.OPEN_ACL_UNSAFE);
        
        
        /**
         * 参数:
         * path:节点路径
         * data:数据
         * version:数据状态 从服务端获取到的版本号dataversion
         */
        //Stat status  = zkServer.getZookeeper().setData("/testnode", "xyz".getBytes(),0);
        //System.out.println(status.getVersion());
        
        /**
         * 参数:
         * path:节点路径
         * version:数据状态
         */
        zkServer.createZKNode("/test-delete-node", "123".getBytes(), Ids.OPEN_ACL_UNSAFE);
//        zkServer.getZookeeper().delete("/test-delete-node", 2);
        
        String ctx = "{'delete':'success'}";
        zkServer.getZookeeper().delete("/test-delete-node", 0, new DeleteCallBack(), ctx);
        Thread.sleep(2000);
    }

    public ZooKeeper getZookeeper() {
        return zookeeper;
    }
    public void setZookeeper(ZooKeeper zookeeper) {
        this.zookeeper = zookeeper;
    }

    @Override
    public void process(WatchedEvent event) {
    }
}

 

  • 回调类:
package com.imooc.zk.demo;


import org.apache.zookeeper.AsyncCallback.VoidCallback;

public class DeleteCallBack implements VoidCallback {
    @Override
    public void processResult(int rc, String path, Object ctx) {
        System.out.println("删除节点" + path);
        System.out.println((String)ctx);
    }

}

 

  • 打印日志:
创建节点: /test-delete-node
{'create':'success'}
创建节点:        成功...
删除节点/test-delete-node
{'delete':'success'}

 

 

  •  linux 连接zk客户端,验证临时节点已删除(断点形式验证,新增时查看下节点,删除后查看下节点)

 

posted on 2024-03-31 08:56  菜鸟乙  阅读(13)  评论(0编辑  收藏  举报