java zookeeper 基本操作指令

package zookeeper;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.data.Stat;
import java.util.List;
import java.util.concurrent.CountDownLatch;
	
	/**
	 *
	 */
	public class CreateZNode {
	
	    //超时时间
	    private static final int SESSION_TIMEOUT = 500000;
	    private  CountDownLatch connectedSignal = new CountDownLatch(1);
	    public static WatchTest watch = new WatchTest();

	    public static void main(String[] args) {
	        try {
	            Watcher wh = new Watcher() {
	            	public void process(WatchedEvent event) {
	            		String path = event.getPath();
	            		EventType type = event.getType();
	            		System.out.println("----------int---"+type.getIntValue());
	            		System.out.println("----------path---"+path);
	            		System.out.println("----------type---"+type);
	        			// 判断事件的类型是否是删除了,如果删除就之间返回程序
	        			if (type == EventType.NodeDeleted) {
	        				System.out.println(path + "节点被删除了,程序需停止" + type);
	        				return;
	        			}
	        			if (type == EventType.NodeDataChanged) {
	        				System.out.println(path + "节点内容有改动" + type);
	        				return;
	        			}
	        			if (type == EventType.NodeCreated) {
	        				System.out.println(path + "新建节点" + type);
	        				return;
	        			}
	        			if (type == EventType.NodeChildrenChanged) {
	        				System.out.println(path + "子节点有变动" + type);
	        				return;
	        			}
	        			// 打印节点变动的信息
	        			System.out.println(path + "出事了" + type);
	            		
	            	}
	            };
            
	            ZooKeeper zk = new ZooKeeper("127.0.0.1:2181", SESSION_TIMEOUT, wh);
	            createNode(zk);
	            //修改
	            //updateNode(zk);
	            //获取zk列表
	            //listNode(zk);
	            //获取value
	           // getData(zk);
	            //删除 节点
	           //deleteNode(zk);
	           
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	    }

	    private static void updateNode(ZooKeeper zk) throws Exception {
			Stat update = zk.setData("/axz/b", "4545346".getBytes(), -1);
			System.out.println(update);
		}

		private static void deleteNode(ZooKeeper zk) throws Exception {
			// TODO Auto-generated method stub
			zk.delete("/zxz/c", -1);
		}

		private static void listNode(ZooKeeper zk) throws KeeperException, InterruptedException {
	    	List idList = zk.getChildren("/zxz/b", false);
            idList.forEach(s->{
            	System.out.println(s);
            });
		}

		private static void getData(ZooKeeper zk) throws Exception {
			byte[] bytes = zk.getData("/zxz/b", false, new Stat());
			System.out.println(new String(bytes));
		}

		public static void createNode(ZooKeeper zk) throws Exception {
	    	 String str1 = zk.create("/axz", "abc".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
	    }
}

linux链接客户端的指令
cd /zookeeper/bin
sh zKcli.sh
新建
create -s -e /test
-s:创建顺序节点
-e:创建临时节点
获取子节点列表
ls /test
获取节点信息指令
get /test
修改指令
set /test data
删除指令
delete /test

zookeeper权限控制
在实际应用中,我们搭建一个zookeeper集群,统一为若干应用服务,如kafka集群,dubbo等,为了避免一个应用的数据被其它的应该干扰,我们需要权限控制。
身份认证模式分为:world,auth,digest,ip
world:默认方式,相当于全世界都能访问
auth:代表已经认证通过的用户(cli中可以通过addauth digest user:pwd 来添加当前上下文中的授权用户)
digest:即用户名:密码这种方式认证,这也是业务系统中最常用的
ip:使用Ip地址认证

 ZooKeeper zk = new ZooKeeper("127.0.0.1:2181", SESSION_TIMEOUT, null);
zk.addAuthInfo("digest","foo:true".getBytes());
zk.create("/fff", "123".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
ZooKeeper zk1 = new ZooKeeper("127.0.0.1:2181", SESSION_TIMEOUT, null);
byte[] bytes = zk1.getData("/fff", false, new Stat());
System.out.println(new String(bytes));

此时zk1查询节点信息会报错 :zookeeperException$NoAuthException

我们应该为zk1增加权限

ZooKeeper zk1 = new ZooKeeper("127.0.0.1:2181", SESSION_TIMEOUT, null);
zk1.addAuthInfo("digest","foo:true".getBytes());
byte[] bytes = zk1.getData("/fff", false, new Stat());
System.out.println(new String(bytes));

你可能感兴趣的:(zookeeper,zookeeper,指令)