ZooKeeper学习之zookeeper的ACL(AUTH)

1.如果学想学习Zookeeper的watcher Api请点击这里:Watcher事件类型和ZK状态

2.Zookeeper的ACL(AUTH)介绍:

zookeeper的ACL(AUTH)
ACL(Access Control List),Zookeeper作为一个分布式协调框架,其内部存储的都是一些关于分布式
系统运行时状态的元数据,尤其是设计到一些分布式锁,Master选举和协调等应用场景。我们需要有
效地保障Zookeeper中的数据安全,Zookeeper提供了三种模式。权限模式,授权对象,权限。
权限模式:Scheme,开发人员最多使用的如下四种权限模式:
	IP:ip模式通过ip地址粒度进行权限控制模式,例如配置了:192.168.110.135即表示权限控
           制都是针对这个ip地址的,同时也支持按网段分配,比如:192.168.110.*
	Digest:digest是最常用的权限控制模式,也更符合我们对权限控制的认识,其类似于
               "username:password"形式的权限标识进行权限配置。ZK会对形成的权限标识先后进
                行两次编码处理,粉笔是SHA-1加密算法和Base64编码。
        World:World是一直最开放的权限控制模式。这种模式可以看做为特殊的Digest,他仅仅是
               一个标识而已。
        Super:超级用户模式,在超级用户模式下可以对ZK任意进行操作。

权限对象:值得是权限赋予的用户或者是一个指定的实体,例如ip地址或机器等。在不同的模式下,
授权对象是不同的。这种模式和权限对象一一对应。

权限:权限就是指那些通过权限检测后可以被允许执行的操作,在ZK中,对数据的操作权限分为以下
五大类:create,delete,read,write,admin

3.实例代码:

package com.adai.zookeeper.three.auth;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.ACL;
/**
 * 工具类,为了测试zookeeper权限(ACL),封装了zookeeper一些api
 * @author adai
 * @since 20170913 01:28
 *
 */
public class ZookeeperUtil {
	//连接地址
	public static final String CONNECT_ADDR = "192.168.110.135,192.168.110.136,192.168.110.137";
	//会话连接超时时间
	public static final int SESSION_TIMEOUT = 2000;
	//测试数据跟路径
	public static final String ROOT_PATH = "/testAuth";
	public static final String CHILDREN = "/testAuth/children";
	public static final String CHILDRENAUTH = "/testAuth/children2";
	//认证方式
	public static final String AUTH_TYPE = "digest";
	//正确的认证方法
	public static final String CORRECTAUTH = "adai";
	//错误的认证方法
	public static final String BADAUTH = "adai1";
	//计数器
	public static final AtomicInteger SEQ = new AtomicInteger();
	
	public static final String LOG_PRE_OF_MAIN = "【main】:";
	
	
	/**
	 * 得到指定节点的数据内容
	 * @param zoo zookeeper对象
	 * @param path 节点的全路径
	 * @param String zookeeperCliName 用来区别是哪一个zookeeper类调用的Util方法
	 */
	public static void getData(ZooKeeper zoo , String path , String zookeeperCliName){
		try {
			String data = new String(zoo.getData(path, false, null));
			System.out.println(zookeeperCliName+"得到节点对应的数据为:"+path+":"+data);
		} catch (Exception e) {
			System.out.println(zookeeperCliName+"获取数据失败:"+path+"---失败原因:"+e.getMessage());
		}
	}
	
	/**
	 * 更新指定节点的内容
	 * @param zoo zookeeper对象
	 * @param path 节点的全路径
	 * @param data 更新的数据
	 * @param zookeeperCliName 用来区别是哪一个zookeeper类调用的Util方法
	 */
	public static void setData(ZooKeeper zoo , String path , String data , String zookeeperCliName){
		try {
			zoo.setData(path, data.getBytes(), -1); // -1忽略所有版本检查
			System.out.println(zookeeperCliName+":节点更新成功  "+path+":"+data);
		} catch (Exception e) {
			System.out.println(zookeeperCliName+":节点更新失败   "+path+":"+data+"---失败原因:"+e.getMessage());
		} 
	}
	
	/**
	 * 删除节点
	 * @param zoo ZooKeeper对象
	 * @param path 节点路径
	 * @param , String zookeeperCliName 用来区别是哪一个zookeeper类调用的Util方法
	 */
	public static void deleteNode(ZooKeeper zoo , String path , String zookeeperCliName){
		try {
			zoo.delete(path, -1);// -1忽略所有版本号
			System.out.println(zookeeperCliName+":删除成功:"+path);
		} catch (Exception e)  {
			System.out.println(zookeeperCliName+":删除节点失败:"+path+"--失败原因:"+e.getMessage());
		} 
	}
	
	/**
	 * 
	 * @param zoo   ZooKeeper对象
	 * @param path  节点路径
	 * @param zookeeperCliName  用来区别是哪一个zookeeper类调用的Util方法
	 */
	public static void exist(ZooKeeper zoo , String path , String zookeeperCliName){
		try {
			zoo.exists(path, false);
			System.out.println(zookeeperCliName+":判断节点是否存在成功:"+path);
		} catch (Exception e) {
			System.out.println(zookeeperCliName+":判断节点是否存在失败:"+path+"---失败原因:"+e.getMessage());
		} 
	}

	/**
	 * 创建一个具有认证权限的节点
	 * @param zoo ZooKeeper对象
	 * @param path 节点路径
	 * @param data 节点数据
	 * @param acls 权限
	 * @param createMode 节点存储数据的模式
	 * @param zookeeperCliName 用来区别是哪一个zookeeper类调用的Util方法
	 */
	public static void createAuthNode(ZooKeeper zoo , String path, String data, 
			List<ACL> acls, CreateMode createMode , String zookeeperCliName) {
		try {
			zoo.create(path, data.getBytes(), acls, createMode);
			System.out.println(zookeeperCliName+":创建了一个具有相应权限的节点成功:"+path+":"+data);
		} catch (Exception e) {
			System.out.println(zookeeperCliName+":创建了一个具有相应权限的节点失败:"+path+":"+data+"---失败原因:"+e.getMessage());
		} 
	}
	
	/**
	 * 创建一个普通的节点(不具有权限约束的节点)
	 * @param zoo  ZooKeeper对象
	 * @param path 节点路径
	 * @param data 节点数据
	 * @param zookeeperCliName 用来区别是哪一个zookeeper类调用的Util方法
	 */
	public static void createNodePath(ZooKeeper zoo , String path , String data , String zookeeperCliName ){
		try {
			zoo.create(path, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); 
			System.out.println(zookeeperCliName+":创建了一个新的节点成功:"+path+":"+data);
		} catch (Exception e) {
			System.out.println(zookeeperCliName+":创建了一个新的节点失败:"+path+":"+data+"---失败原因:"+e.getMessage());
		} 
	}
	
	/**
	 * 释放资源
	 * @param zoo ZooKeeper对象
	 */
	public static void close(ZooKeeper zoo){
		if(zoo != null ){
			try {
				zoo.close();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
package com.adai.zookeeper.three.auth;
import java.util.concurrent.CountDownLatch;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
/**
 * 
 * @author adai 
 * @since 20170913 0132
 */
/**
 * ZookeeperWatcher类,如果使用了watcher事件会触发该类的process方法。
 * 第一次连接zookeeper服务器的时候也会触发
 * @author Administrator
 *
 */
public class ZookeeperWatcher implements Watcher{
	//用来阻塞线程,等待客户端成功连接zookeeper服务器。
	private   CountDownLatch correctAuthZooKeeper = null;
	//用来打印日记时,目的是为了知道是哪一个创建的zookeeper
	private String log_process = "";
	
	public 	ZookeeperWatcher(CountDownLatch correctAuthZooKeeper , String zookeeperCliName){
		this.correctAuthZooKeeper = correctAuthZooKeeper;
		log_process = zookeeperCliName;
	}
	
	@Override
	public void process(WatchedEvent event) {
		if(event == null ) return;
		log_process = "【"+log_process+" watche-"+ZookeeperUtil.SEQ.addAndGet(1)+"】";
		String path = event.getPath(); // 受影响的节点路径
		EventType enventType = event.getType(); // 事件类型(一共有四种)
		KeeperState stat = event.getState() ; // ZK状态(一共有四种)
		
		if(KeeperState.SyncConnected == stat){
			if(EventType.None == enventType){
				System.out.println(log_process+":连接zookeeper服务器成功");
				correctAuthZooKeeper.countDown();
			}
		}else if(KeeperState.Disconnected == stat){
			System.out.println(log_process+":连接zookeeper服务器失败");
		}else if(KeeperState.Expired == stat){
			System.out.println(log_process+":连接zookeeper服务器会话已经过期");
		}else if(KeeperState.AuthFailed == stat){
			System.out.println(log_process+":连接zookeeper服务器认证失败");
		}
		System.out.println("------------------------------------");
	}
}

package com.adai.zookeeper.three.auth;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Stat;

/**
 * 
 * @author adai
 * @since 20170912 10:47
 * 拥有某种权限的zookeeper的客户端(权限类型为:digest 所对应的具体权限:adai【类型用户名密码验证等】)
 */
public class AuthZookeeper {
	private CountDownLatch countDownLatch = new CountDownLatch(1);
	private ZooKeeper zoo = null ;
	public AuthZookeeper(){
		try {
			zoo = new ZooKeeper(ZookeeperUtil.CONNECT_ADDR,
								ZookeeperUtil.SESSION_TIMEOUT,
								new ZookeeperWatcher(countDownLatch,"AuthZookeeper"));
			//授权  参数1:权限类型    参数2:对应的正确权限(CorrectAuthZookeeper创建的节点权限与其一致)
			zoo.addAuthInfo(ZookeeperUtil.AUTH_TYPE, ZookeeperUtil.CORRECTAUTH.getBytes());
			countDownLatch.await();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 释放连接
	 */
	public void close(){
		if(zoo != null ){
			try {
				zoo.close();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	public ZooKeeper getCorrectAuthZookeeper(){
		return zoo;
	}
	
	/**
	 * 创建一个具有认证权限的节点
	 * @param path
	 * @param data
	 */
	public void createAuthNode(String path , String data){
		try {
			Stat stat = zoo.exists(path, false);
			if (stat != null){
				System.out.println(path+"节点已经存在:");
				return ;
			}
		} catch (Exception e) {
		} 
		List<ACL> acls = new ArrayList<ACL>(1);
		for (ACL ids_acl : Ids.CREATOR_ALL_ACL) { //遍历出所有权限类型
			acls.add(ids_acl);
		}
		ZookeeperUtil.createAuthNode(zoo , path, data, acls, CreateMode.PERSISTENT ,"AuthZookeeper");
	}
}
package com.adai.zookeeper.three.auth;
import java.util.concurrent.CountDownLatch;
import org.apache.zookeeper.ZooKeeper;

/**
 * 拥有某种权限的zookeeper客户端,该权限与AuthZookeeper类的权限一致
 * 多以AuthZookeeper类创建的节点,该客户端可以对该节点进行相应的操作(如:节点数据的获取:修改数据,删除数据等)
 * @author adai
 * @since 20170912 11:03
 *
 */
public class CorrectAuthZookeeper {
	private CountDownLatch countDownLatch = new CountDownLatch(1);
	private ZooKeeper zoo = null ;
	
	public CorrectAuthZookeeper(){
		try {
			zoo = new ZooKeeper(ZookeeperUtil.CONNECT_ADDR,
								ZookeeperUtil.SESSION_TIMEOUT,
								new ZookeeperWatcher(countDownLatch,"CorrectAuthZookeeper"));
			//授权  参数1:权限类型    参数2:对应的正确权限(AuthZookeeper创建的节点权限一致)
			zoo.addAuthInfo(ZookeeperUtil.AUTH_TYPE, ZookeeperUtil.CORRECTAUTH.getBytes());
			countDownLatch.await();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 释放连接
	 */
	public void close(){
		if(zoo != null ){
			try {
				zoo.close();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public ZooKeeper getCorrectAuthZookeeper(){
		return zoo;
	}
}
package com.adai.zookeeper.three.auth;
import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.ZooKeeper;
/**
 * 拥有某种权限的zookeeper客户端,该权限与AuthZookeeper类的权限不一致
 * 所以AuthZookeeper类创建的节点,该客户端不可以对该节点进行相应的操作(如:节点数据的获取:修改数据,删除数据等)
 * 但可以进行创建新的节点
 * @author adai
 * @since 20170912 11:02
 * 
 */
public class BadAuthZookeeper {
	private CountDownLatch countDownLatch = new CountDownLatch(1);
	private ZooKeeper zoo = null ;
	
	public BadAuthZookeeper(){
		try {
			zoo = new ZooKeeper(ZookeeperUtil.CONNECT_ADDR,
								ZookeeperUtil.SESSION_TIMEOUT,
								new ZookeeperWatcher(countDownLatch,"BadAuthZookeeper"));
			//授权  参数1:权限类型    参数2:对应的正确权限(AuthZookeeper创建的节点权限不一致)
			zoo.addAuthInfo(ZookeeperUtil.AUTH_TYPE, ZookeeperUtil.BADAUTH.getBytes());
			countDownLatch.await();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 释放连接
	 */
	public void close(){
		if(zoo != null ){
			try {
				zoo.close();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public ZooKeeper getCorrectAuthZookeeper(){
		return zoo;
	}
}
package com.adai.zookeeper.three.auth;
import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.ZooKeeper;

/**
 * 
 * @author adai
 * @since 20170912 11:00
 * 没有任何权限的zookeeper客户端,不能操作AuthZookeeper客户端创建的节点(如:节点数据的获取:修改数据,删除数据等)
 */
public class NotAuthZookeeper {
	private CountDownLatch countDownLatch = new CountDownLatch(1);
	private ZooKeeper zoo = null ;
	
	public NotAuthZookeeper(){
		try {
			zoo = new ZooKeeper(ZookeeperUtil.CONNECT_ADDR,
								ZookeeperUtil.SESSION_TIMEOUT,
								new ZookeeperWatcher(countDownLatch,"NotAuthZookeeper"));
			countDownLatch.await();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void close(){
		if(zoo != null ){
			try {
				zoo.close();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public ZooKeeper getCorrectAuthZookeeper(){
		return zoo;
	}
}
4.测试总结:
package com.adai.zookeeper.three.auth;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

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

/**
 * 
 * @author adai
 * @since 20170912 10:46
 * 测试zookeeper权限类
 */
public class TestMain {
	private static ExecutorService executorService = null;
	private static ZooKeeper correctAuthZookeeper = null ; 
	private static ZooKeeper badAuthZookeeper = null ; 
	private static ZooKeeper notAuthZookeeper = null ; 
	private static AuthZookeeper authZookeeper = null ;
	private static CountDownLatch countDownLatch = new CountDownLatch(4);
	public static void main(String[] args) throws InterruptedException {
//		executorService =  Executors.newFixedThreadPool(4); 
		executorService =  new ThreadPoolExecutor(
				4, //  corePoolSize       核心线程数 (核心线程数不能大于最大线程数)
				4, //  maximumPoolSize    最大线程数 当使用无界队列时,该参数没有任何意义,无界队列执行的一次为核心线程的任务数
				2000, //  keepAliveTime 空闲时间
				TimeUnit.MILLISECONDS, // 时间单位(分)  
				new LinkedBlockingQueue<Runnable>() // workQueue  使用何种队列机制
				);
		connect();
		countDownLatch.await(); // 等待所有zookeeper客户端连接服务器成功
		//该客户端创建的节点具有权限,其他客户端要访问该节点,必须要与该节点的权限一致才能访问
		authZookeeper.createAuthNode(ZookeeperUtil.ROOT_PATH, " adai");
		
//		exist();
//		setDate();
//		getDate();
//		createNodePath();
//		setNodeData();
//		getNodeDate();
//		delete();
//		createNodePathOrAllAuth();
		deleteNodeAllAuth();
		deleteRoot();
		close();
	}
	
	/**
	 * 所有客户端异步连接zookeeper服务器
	 */
	public static void connect(){
		executorService.execute(new Runnable() {
					@Override
					public void run() {
						authZookeeper = new AuthZookeeper();
						countDownLatch.countDown();
					}
				});
		
		executorService.execute(new Runnable() {
			@Override
			public void run() {
				correctAuthZookeeper = new CorrectAuthZookeeper().getCorrectAuthZookeeper();
				countDownLatch.countDown();
			}
		});
		
		executorService.execute(new Runnable() {
			@Override
			public void run() {
				badAuthZookeeper = new BadAuthZookeeper().getCorrectAuthZookeeper();
				countDownLatch.countDown();
			}
		});
		
		executorService.execute(new Runnable() {
			@Override
			public void run() {
				notAuthZookeeper = new NotAuthZookeeper().getCorrectAuthZookeeper();
				countDownLatch.countDown();
			}
		});
	}
	
	/**
	 *correctAuthZookeeper得到节点对应的数据为:/testAuth: adai
	 *badAuthZookeeper获取数据失败:/testAuth---失败原因:KeeperErrorCode = NoAuth for /testAuth
	 *notAuthZookeeper获取数据失败:/testAuth---失败原因:KeeperErrorCode = NoAuth for /testAuth
	 */
	public static void getDate(){
		ZookeeperUtil.getData(correctAuthZookeeper, ZookeeperUtil.ROOT_PATH, "correctAuthZookeeper");
		ZookeeperUtil.getData(badAuthZookeeper, ZookeeperUtil.ROOT_PATH, "badAuthZookeeper");
		ZookeeperUtil.getData(notAuthZookeeper, ZookeeperUtil.ROOT_PATH, "notAuthZookeeper");
	}
	
	/**
	 * correctAuthZookeeper:节点更新成功  /testAuth:auth correctAuthZookeeper
	 *badAuthZookeeper:节点更新失败   /testAuth:auth badAuthZookeeper---失败原因:KeeperErrorCode = NoAuth for /testAuth
	 *notAuthZookeeper:节点更新失败   /testAuth:auth notAuthZookeeper---失败原因:KeeperErrorCode = NoAuth for /testAuth
	 */
	public static void setDate(){
		ZookeeperUtil.setData(correctAuthZookeeper,ZookeeperUtil.ROOT_PATH,
				"auth correctAuthZookeeper","correctAuthZookeeper");
		ZookeeperUtil.setData(badAuthZookeeper,ZookeeperUtil.ROOT_PATH,
				"auth badAuthZookeeper","badAuthZookeeper");
		ZookeeperUtil.setData(notAuthZookeeper,ZookeeperUtil.ROOT_PATH,
				"auth notAuthZookeeper","notAuthZookeeper");
	}
	
	/**
	 *AuthZookeeper:创建了一个具有相应权限的节点成功:/testAuth: adai
	 *correctAuthZookeeper:判断节点是否存在成功:/testAuth
	 *badAuthZookeeper:判断节点是否存在成功:/testAuth
	 *notAuthZookeeper:判断节点是否存在成功:/testAuth
	 */
	public static void exist(){
		ZookeeperUtil.exist(correctAuthZookeeper, ZookeeperUtil.ROOT_PATH, "correctAuthZookeeper");
		ZookeeperUtil.exist(badAuthZookeeper, ZookeeperUtil.ROOT_PATH, "badAuthZookeeper");
		ZookeeperUtil.exist(notAuthZookeeper, ZookeeperUtil.ROOT_PATH, "notAuthZookeeper");
	}
	
	/**
	 *correctAuthZookeeper:创建了一个新的节点成功:/testAuth/children: adai children correctAuthZookeeper
	 *badAuthZookeeper:创建了一个新的节点失败:/testAuth/children: 
	 *	adai children badAuthZookeeper---失败原因:KeeperErrorCode = NoAuth for /testAuth/children
	 *notAuthZookeeper:创建了一个新的节点失败:/testAuth/children: 
	 *	adai children notAuthZookeeper---失败原因:KeeperErrorCode = NoAuth for /testAuth/children
	 */
	public static void createNodePath(){
		ZookeeperUtil.createNodePath(correctAuthZookeeper, ZookeeperUtil.CHILDREN, 
				" adai children correctAuthZookeeper", "correctAuthZookeeper");
		ZookeeperUtil.createNodePath(badAuthZookeeper, ZookeeperUtil.CHILDREN+"2", 
				" adai children badAuthZookeeper", "badAuthZookeeper");
		ZookeeperUtil.createNodePath(notAuthZookeeper, ZookeeperUtil.CHILDREN+"3", 
				" adai children notAuthZookeeper", "notAuthZookeeper");
	}
	
	/**
	 *correctAuthZookeeper:节点更新成功  /testAuth/children:set nodeData correctAuthZookeeper
	 *badAuthZookeeper:节点更新成功  /testAuth/children:set nodeData badAuthZookeeper
	 *notAuthZookeeper:节点更新成功  /testAuth/children:set nodeData notAuthZookeeper
	 *疑问?为什么子节点会更新成功(请比较两个创建自己的方法有什么不同)
	 */
	public static void setNodeData(){
		ZookeeperUtil.setData(correctAuthZookeeper, ZookeeperUtil.CHILDREN, 
				"set nodeData correctAuthZookeeper", "correctAuthZookeeper");
		ZookeeperUtil.setData(badAuthZookeeper, ZookeeperUtil.CHILDREN, 
				"set nodeData badAuthZookeeper", "badAuthZookeeper");
		ZookeeperUtil.setData(notAuthZookeeper, ZookeeperUtil.CHILDREN, 
				"set nodeData notAuthZookeeper", "notAuthZookeeper");
	}
	
	/**
	点击打开链接 *correctAuthZookeeper得到节点对应的数据为:/testAuth/children:set nodeData notAuthZookeeper
	 *badAuthZookeeper得到节点对应的数据为:/testAuth/children:set nodeData notAuthZookeeper
	 *notAuthZookeeper得到节点对应的数据为:/testAuth/children:set nodeData notAuthZookeeper
	 *答上疑问,因为correctAuthZookeeper创建子节点没有将所有权限作为参数传入,而用的是Ids.OPEN_ACL_UNSAFE传入。
	 */
	public static void getNodeDate(){
		ZookeeperUtil.getData(correctAuthZookeeper, ZookeeperUtil.CHILDREN, "correctAuthZookeeper");
		ZookeeperUtil.getData(badAuthZookeeper, ZookeeperUtil.CHILDREN, "badAuthZookeeper");
		ZookeeperUtil.getData(notAuthZookeeper, ZookeeperUtil.CHILDREN, "notAuthZookeeper");
	}
	
	/**
	 * badAuthZookeeper:删除节点失败:/testAuth/children--失败原因:KeeperErrorCode = NoAuth for /testAuth/children
	 * notAuthZookeeper:删除节点失败:/testAuth/children--失败原因:KeeperErrorCode = NoAuth for /testAuth/children
	 * correctAuthZookeeper:删除成功:/testAuth/children
	 */
	public static void delete(){
		ZookeeperUtil.deleteNode(badAuthZookeeper, ZookeeperUtil.CHILDREN,"badAuthZookeeper");
		ZookeeperUtil.deleteNode(notAuthZookeeper, ZookeeperUtil.CHILDREN,"notAuthZookeeper");
		ZookeeperUtil.deleteNode(correctAuthZookeeper, ZookeeperUtil.CHILDREN,"correctAuthZookeeper");
	}

	public static void close(){
		ZookeeperUtil.close(correctAuthZookeeper);
		ZookeeperUtil.close(badAuthZookeeper);
		ZookeeperUtil.close(notAuthZookeeper);
		ZookeeperUtil.close(authZookeeper.getCorrectAuthZookeeper());
		executorService.shutdown(); // 等待任务执行完后释放资源
		System.out.println("释放资源完毕");
	}
	
	/**
	 * 解决上文疑问
	 * CorrectAuthZookeeper:创建了一个具有相应权限的节点成功:/testAuth/children2: adai all atuh create correctAuthZookeeper
	 * correctAuthZookeeper:节点更新成功  /testAuth/children2:set nodeData to AllAuth correctAuthZookeeper
	 * badAuthZookeeper:节点更新失败   /testAuth/children2:set nodeData2 to AllAuth badAuthZookeeper
	 * 		---失败原因:KeeperErrorCode = NoAuth for /testAuth/children2
	 * notAuthZookeeper:节点更新失败   /testAuth/children2:set nodeData2 to AllAuth notAuthZookeeper
	 * 		---失败原因:KeeperErrorCode = NoAuth for /testAuth/children2
	 */
	public static void createNodePathOrAllAuth(){
		try {
			Stat stat = correctAuthZookeeper.exists(ZookeeperUtil.CHILDRENAUTH, false);
			if (stat != null){
				System.out.println(ZookeeperUtil.CHILDRENAUTH+"节点已经存在:");
				return ;
			}
		} catch (Exception e) {
		} 
		List<ACL> acls = new ArrayList<ACL>(1);
		for (ACL ids_acl : Ids.CREATOR_ALL_ACL) { //遍历出所有权限类型
			acls.add(ids_acl);
		}
		ZookeeperUtil.createAuthNode(correctAuthZookeeper, ZookeeperUtil.CHILDRENAUTH, 
				" adai all atuh create correctAuthZookeeper", acls, CreateMode.PERSISTENT, "CorrectAuthZookeeper");
		ZookeeperUtil.setData(correctAuthZookeeper, ZookeeperUtil.CHILDRENAUTH, 
				"set nodeData to AllAuth correctAuthZookeeper", "correctAuthZookeeper");
		ZookeeperUtil.setData(badAuthZookeeper, ZookeeperUtil.CHILDRENAUTH, 
				"set nodeData2 to AllAuth badAuthZookeeper", "badAuthZookeeper");
		ZookeeperUtil.setData(notAuthZookeeper, ZookeeperUtil.CHILDRENAUTH, 
				"set nodeData2 to AllAuth notAuthZookeeper", "notAuthZookeeper");
	}
	
	public static void deleteNodeAllAuth(){
		ZookeeperUtil.deleteNode(badAuthZookeeper, ZookeeperUtil.CHILDRENAUTH,"badAuthZookeeper");
		ZookeeperUtil.deleteNode(notAuthZookeeper, ZookeeperUtil.CHILDRENAUTH,"notAuthZookeeper");
		ZookeeperUtil.deleteNode(correctAuthZookeeper, ZookeeperUtil.CHILDRENAUTH,"correctAuthZookeeper");
	}
	
	public static void deleteRoot(){
		ZookeeperUtil.deleteNode(badAuthZookeeper, ZookeeperUtil.ROOT_PATH,"badAuthZookeeper");
		ZookeeperUtil.deleteNode(notAuthZookeeper, ZookeeperUtil.ROOT_PATH,"notAuthZookeeper");
		ZookeeperUtil.deleteNode(correctAuthZookeeper, ZookeeperUtil.ROOT_PATH,"correctAuthZookeeper");
	}
}

如果以上代码有问题,请您告知我一下:qq:1721995310,也可以评论区里告知一声,在此说声感谢。更多关于zookeeper的学习,请点击这里: 点击打开链接




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值