java操作zookeeper

Zookeeper的增删改查

package top.campsis.zk;

import org.I0Itec.zkclient.ZkClient;

/**
 * @ClassName: TestCURD
 * @Author: campsis-tk
 * @Date: 2020/7/27 21:10
 * @Description:
 **/
public class TestCURD {

	private static final String ZkServer = "你的zookeeper地址";

	private static ZkClient zkClient = null;

	static {
		zkClient = new ZkClient(ZkServer, 10000,10000);
	}

	public static void main(String[] args) {

		/**
		 * 创建一个节点
		 */
		String string = zkClient.create("/java", "hello,world", CreateMode.PERSISTENT);
		System.out.println(string);

		/**
		 * 获取一个节点
		 */
		Object data = zkClient.readData("/java");
		System.out.println(data);

		/**
		 * 重新设置一个节点的值
		 */
		zkClient.writeData("/java", "hello,java");

		/**
		 * 删除一个节点
		 */
		boolean delete = zkClient.delete("/java");
		System.out.println(delete);

		System.out.println(zkClient);
	}
}

Zookeeper的序列化

package top.campsis.zk;

import org.I0Itec.zkclient.exception.ZkMarshallingError;
import org.I0Itec.zkclient.serialize.ZkSerializer;

import java.io.UnsupportedEncodingException;

/**
 * @ClassName: CustomerSerializer
 * @Author: campsis-tk
 * @Date: 2020/7/27 21:35
 * @Description:
 **/
public class CustomerSerializer implements ZkSerializer {

	/** default utf 8 */
	private String charset = "UTF-8";


	public CustomerSerializer() {
		// TODO Auto-generated constructor stub
	}

	public CustomerSerializer(String charset) {
		this.charset = charset;
	}

	/**
	 * 序列化
	 */
	public byte[] serialize(Object data) throws ZkMarshallingError {
		try {
			byte[] bytes = String.valueOf(data).getBytes(charset);
			return bytes;
		} catch (UnsupportedEncodingException e) {
			throw new ZkMarshallingError("Wrong Charset:" + charset);
		}
	}

	/**
	 * 反序列化
	 */
	public Object deserialize(byte[] bytes) throws ZkMarshallingError {
		String result = null;
		try {
			result = new String(bytes, charset);
		} catch (UnsupportedEncodingException e) {
			throw new ZkMarshallingError("Wrong Charset:" + charset);
		}
		return result;
	}

}

Zookeeper监听器

package top.campsis.zk;

import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;

import java.io.IOException;

/**
 * @ClassName: TestCURD
 * @Author: campsis-tk
 * @Date: 2020/7/27 21:10
 * @Description:
 **/
public class TestListener {

	private static final String ZkServer = "你的zookeeper地址";

	private static ZkClient zkClient = null;

	static {

		zkClient = new ZkClient(ZkServer, 10000,10000,new CustomerSerializer("UTF-8"));
	}

	public static void main(String[] args) {

		/**
		 * 创建一个节点
		 */
		String string = zkClient.create("/java", "hello,world", CreateMode.PERSISTENT);
		System.out.println(string);

		/**
		 * 监听/java节点的数据变化
		 */
		zkClient.subscribeDataChanges("/java", new IZkDataListener() {
			public void handleDataChange(String s, Object o) throws Exception {
				System.err.println(s + " :handleDataChange: " + o);
			}

			public void handleDataDeleted(String s) throws Exception {
				System.err.println(s + " :handleDataDeleted");
			}
		});
		System.err.println("启动成功!");
		//不让程序结束
		try {
			System.in.read();
		} catch (IOException e) {
			e.printStackTrace();
		}

		System.out.println(zkClient);
	}
}

相关推荐