java客户端调用zookeeper

1、引入pom依赖

<dependency>
	<groupId>com.101tec</groupId>
	<artifactId>zkclient</artifactId>
	<version>0.10</version>
</dependency>
<dependency>
	<groupId>org.apache.zookeeper</groupId>
	<artifactId>zookeeper</artifactId>
	<version>3.4.9</version>
</dependency>

2、调用

/**
 * 1、startZK通过java程序,新建链接zk,类似jdbc的connection,open.session
 * 2、createZNode新建一个znode节点/atguigu并设置为hello0228	等同于create /atguigu 
      hello1221 Ids.OPEN_ACL_UNSAFE
 * 3、getZnode获得当前节点/atguigu的最新值	get /atguigu
 * 4、stopZK关闭链接
 */
public class HelloZK {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(HelloZK.class);
	private static final String CONNECTSTRING = "192.168.154.162:2181";
	private static final String PATH = "/atguigu"; //Node节点
	private static final int SESSION_TIMEOUT = 20 * 1000; //sesseion超时时间

	/**
	 * startZK通过java程序,新建链接zk,
	 */
	public ZooKeeper startZK() throws IOException {
		return new ZooKeeper(CONNECTSTRING, SESSION_TIMEOUT, new Watcher() {

			@Override
			public void process(WatchedEvent arg0) {
				// TODO Auto-generated method stub

			}
		});
	}

	/**
	 * createZNode新建一个znode节点
	 */
	public void createZNode(ZooKeeper zk, String path, String data) throws KeeperException, InterruptedException {
		zk.create(path, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
	}

	/**
	 * getZnode获得当前节点
	 */
	public String getZnode(ZooKeeper zk, String path) throws KeeperException, InterruptedException {
		String result = "";
		byte[] data = zk.getData(path, false, new Stat());
		result = new String(data);
		return result;
	}

	/**
	 * stopZK关闭链接
	 */
	public void stopZK(ZooKeeper zk) throws InterruptedException {
		if (zk != null) zk.close();
	}

	public static void main(String[] args) throws Exception {

		HelloZK helloZK = new HelloZK();
		ZooKeeper zk = helloZK.startZK();
		if (zk.exists(PATH, false) == null) {
			helloZK.createZNode(zk, PATH, "hello0228");
			String znode = helloZK.getZnode(zk, PATH);

			logger.info("---------- znode =" + znode);
		} else {
			logger.info("----------this znode is ok");
		}
		helloZK.stopZK(zk);

	}
}

3、zookeeper通知机制(Watcher:观察者模式)

/**
 * 
 * @Description:
 *1	初始化ZK的多个操作
 * 		1.1	建立ZK的链接
 * 		1.2	创建/atguigu节点并赋值
 * 		1.3	获得该节点的值
 * 
 * 2	watchmore
 * 		2.1	获得值之后设置一个观察者watcher,如果/atguigu该节点的值发生了变化,要求通知Client端,一次性通知
 * 
 * 3	watchMore
 * 		3.1	获得值之后设置一个观察者watcher,如果/atguigu该节点的值发生了变化,要求通知Client端,继续观察
 * 		3.2	又再次获得新的值的同时再新设置一个观察者,继续观察并获得值
 * 		3.3	又再次获得新的值的同时再新设置一个观察者,继续观察并获得值.。。。。。重复上述过程
 * @author xialei
 */
public class WatchMore {
	private static final Logger logger = Logger.getLogger(WatchMore.class);
	//实例常量
	private static final String CONNECTION_STRING = "192.168.154.162:2181";
	private static final String PATH = "/atguigu";
	private static final int SESSION_TIMEOUT = 20 * 1000;
	//实例变量
	private  ZooKeeper zk = null;
	private  String oldValue = null;
	private  String newValue = null;
	
	public ZooKeeper getZk() {
		return zk;
	}

	public void setZk(ZooKeeper zk) {
		this.zk = zk;
	}

	public String getOldValue() {
		return oldValue;
	}

	public void setOldValue(String oldValue) {
		this.oldValue = oldValue;
	}

	public String getNewValue() {
		return newValue;
	}

	public void setNewValue(String newValue) {
		this.newValue = newValue;
	}

	/**
	 * 
	 * 
	* @Title: startZK
	* @Description: 获得ZK的session连接对象实例
	* @param @return
	* @param @throws IOException    参数
	* @return ZooKeeper    返回类型
	* @throws
	 */
	public ZooKeeper startZK() throws IOException {
		return new ZooKeeper(CONNECTION_STRING, SESSION_TIMEOUT, new Watcher() {
			@Override
			public void process(WatchedEvent event) {
			}
		});
	}
	
	/**
	 * 
	* @Title: createZnode
	* @Description: 再给定的路径下创建znode节点并赋值
	* @param @param zk
	* @param @param path
	* @param @param data
	* @param @throws KeeperException
	* @param @throws InterruptedException    参数
	* @return void    返回类型
	* @throws
	 */
	public void createZnode(String path,String data) throws KeeperException, InterruptedException {
		zk.create(path, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
	}	
	
	/**
	 * 
	* @Title: getZnode
	* @Description: 获取我们对应节点的值
	* @param @param zk
	* @param @param path
	* @param @return
	* @param @throws KeeperException
	* @param @throws InterruptedException    参数
	* @return String    返回类型
	* @throws
	 */
	public String getZnode(String path) throws KeeperException, InterruptedException {
		String result = "";
		byte[]  byteArray = zk.getData(path, new Watcher() {
			@Override
			public void process(WatchedEvent event) {
				try {
					triggerValue(path);
				} catch (KeeperException | InterruptedException e) {
					e.printStackTrace();
				}
			}
		}, new Stat());
		result = new String(byteArray);
		oldValue = result;
		return result;
	}	
	
	public boolean triggerValue(String path) throws KeeperException, InterruptedException {
		String result = "";
		byte[] byteArray = zk.getData(path, new Watcher() {
			@Override
			public void process(WatchedEvent event) {
				try {
					triggerValue(path);
				} catch (KeeperException | InterruptedException e) {
					e.printStackTrace();
				}
			}
		}, new Stat());
		result = new String(byteArray);
		newValue = result;
		
		if(oldValue.equals(newValue)) {
			logger.info("-----------no changes---------0000 ");
			return false;
		}else {
			logger.info("-----------newValue: "+newValue+"\t oldValue: "+oldValue);
			oldValue = newValue;
			return true;
		}
	}	

	public static void main(String[] args) throws Exception {
		WatchMore watchMore = new WatchMore();
		watchMore.setZk(watchMore.startZK());
		
		if(watchMore.getZk().exists(PATH, false) == null) {
			watchMore.createZnode(PATH, "AAA");
			
			String result = watchMore.getZnode(PATH);//AAA
			
			if (logger.isInfoEnabled()) {
				logger.info("main(String[]) --------init String result=" + result);
			}			
		}else {
			logger.info("this node has already exists!!!!");			
		}
		
		Thread.sleep(Long.MAX_VALUE);		
	}
}

猜你喜欢

转载自blog.csdn.net/wxd_1024/article/details/83819824