监听子节点
/**
* 订阅节点的信息改变(创建节点,删除节点,添加子节点)
* @author zhoulf
*
*/
public class SubscribeChildChanges {
private static class ZKChildListener implements IZkChildListener {
/**
* handleChildChange: 用来处理服务器端发送过来的通知 parentPath:对应的父节点的路径
* currentChilds:子节点的相对路径
*/
public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {
System.out.println(parentPath);
System.out.println(currentChilds.toString());
//create: /testUserNode/n1
// [n1]
//create: /testUserNode/n2
// [n1, n2]
//delete: /testUserNode/n1
// [n2]
// delete /testUserNode/n2 无数据:
// []
// delete /testUserNode 节点删除:空字符串
//
//set /testUserNode/n1 "adfa" 会没有反应,因为这个是DataChange监听的
}
}
public static void main(String[] args) throws InterruptedException {
// zk集群的地址
// String ZKServers = "192.168.30.164:2181,192.168.30.165:2181,192.168.30.166:2181";
String ZKServers = "localhost:2181";
ZkClient zkClient = new ZkClient(ZKServers, 10000, 10000, new SerializableSerializer());
System.out.println("conneted ok!");
/**
* "/testUserNode" 监听的节点,可以是现在存在的 也可以是不存在的
*/
zkClient.subscribeChildChanges("/testUserNode", new ZKChildListener());
Thread.sleep(Integer.MAX_VALUE);
}
}
监听节点
/**
* 订阅节点的数据内容的变化 (监听子节点变化,不好监听节点的删除。监听节点可以监听到节点的删除)
* @author zhoulf
*
*/
public class SubscribeDataChanges {
private static class ZKDataListener implements IZkDataListener {
public void handleDataChange(String dataPath, Object data) throws Exception {
System.out.println("changed\t"+dataPath + ":" + data.toString());
// create /testUserNode:com.zclient.t01_base.User@5f6db53a
// update /testUserNode:com.zclient.t01_base.User@1b9523c8
}
public void handleDataDeleted(String dataPath) throws Exception {
System.out.println("delete!\t"+dataPath);
//delete /testUserNode
}
}
public static void main(String[] args) throws InterruptedException {
// zk集群的地址
// String ZKServers = "192.168.30.164:2181,192.168.30.165:2181,192.168.30.166:2181";
String ZKServers = "localhost:2181";
ZkClient zkClient = new ZkClient(ZKServers, 10000, 10000, new SerializableSerializer());
System.out.println("conneted ok!");
zkClient.subscribeDataChanges("/testUserNode", new ZKDataListener());
Thread.sleep(Integer.MAX_VALUE);
// created path:/testUserNode
}
}
增删改
public class Base {
static ZkClient zkClient;
public static void main(String[] args) {
connect();
// zkClient.create("/lock2", null, CreateMode.PERSISTENT);
// System.out.println(create_EphemeralSequential("/lock2/Mutex", 1));
// System.out.println(create_EphemeralSequential("/lock2/Mutex", 2));
// System.out.println(create_EphemeralSequential("/lock2/Mutex", 1));
create("/testUserNode");
// write("/testUserNode");
// read();
// exists();
// delete("/testUserNode",true);
}
private static void connect() {
if (zkClient != null) {
return;
}
// zk集群的地址
// String ZKServers =
// "192.168.30.164:2181,192.168.30.165:2181,192.168.30.166:2181";
String ZKServers = "localhost:2181";
/**
* 创建会话 new SerializableSerializer() 创建序列化器接口,用来序列化和反序列化
*/
zkClient = new ZkClient(ZKServers, 10000, 10000, new SerializableSerializer());
System.out.println("conneted ok!");
}
public static void create(String p) {
connect();
User user = new User();
user.setId(1);
user.setName("testUser");
/**
* "/testUserNode" :节点的地址 user:数据的对象 CreateMode.PERSISTENT:创建的节点类型
*/
String path = zkClient.create(p, user, CreateMode.PERSISTENT);
// 输出创建节点的路径
System.out.println("created path:" + path);
}
public static String create_EphemeralSequential(String path, Object data) {
connect();
return zkClient.createEphemeralSequential(path, data);
}
public static void read() {
connect();
Stat stat = new Stat();
// 获取 节点中的对象
User user = zkClient.readData("/testUserNode", stat);
System.out.println(user.getName());// testUser
System.out.println(stat);// 557,557,1494071031735,1494071031735,0,0,0,0,189,0,557
}
public static void write(String path) {
connect();
User user = new User();
user.setId(2);
user.setName("testUser");
zkClient.writeData(path, user);
// zkClient.writeData(path, data, 1);
// Stat s = zkClient.writeDataReturnStat(path, data, 0);
// System.out.println(s);
}
public static void set(String path) {
connect();
Stat stat = new Stat();
// 获取 节点中的对象
User user = zkClient.readData(path, stat);
System.out.println(user.getName());
System.out.println(stat);
}
public static void exists() {
connect();
boolean e = zkClient.exists("/testUserNode");
// 返回 true表示节点存在 ,false表示不存在
System.out.println(e);
}
public static void delete(String path, boolean recurisive) {
connect();
if (recurisive) {
// 删除含有子节点的节点
System.out.println(zkClient.deleteRecursive(path));
} else {
// 删除单独一个节点,返回true表示成功
System.out.println(zkClient.delete(path));
}
}
}
分布式锁
public interface DistributedLock {
/*
* 获取锁,如果没有得到就等待
*/
public void acquire() throws Exception;
/*
* 获取锁,直到超时
*/
public boolean acquire(long time, TimeUnit unit) throws Exception;
/*
* 释放锁
*/
public void release() throws Exception;
}
BaseDistributedLock
public class BaseDistributedLock {
protected final ZkClient client;
protected final String lockSolutionPath;
// zookeeper中locker节点的路径
protected final String basePath;
protected final String lockName;
private static final Integer MAX_RETRY_COUNT = 10;
public BaseDistributedLock(ZkClient client, String path, String lockName) {
this.client = client;
this.basePath = path;
if (!client.exists(path)) {
client.create(path, null, CreateMode.PERSISTENT);
}
this.lockSolutionPath = path.concat("/").concat(lockName);
this.lockName = lockName;
}
private void deleteOurPath(String ourPath) throws Exception {
client.delete(ourPath);
}
protected String createLockNode(ZkClient client, String path) throws Exception {
return client.createEphemeralSequential(path, null);
}
private boolean waitToLock(long startMillis, Long millisToWait, String ourPath) throws Exception {
boolean haveGotLock = false;
boolean doDelete = false;
try {
while (!haveGotLock) {
// 获取lock节点下的所有节点
List<String> children = getSortedChildren();
String sequenceNodeName = ourPath.substring(basePath.length() + 1);
// 获取当前节点的在所有节点列表中的位置
int ourIndex = children.indexOf(sequenceNodeName);
// 节点位置小于0,说明没有找到节点
if (ourIndex < 0) {
throw new ZkNoNodeException("节点没有找到: " + sequenceNodeName);
}
// 节点位置大于0说明还有其他节点在当前的节点前面,就需要等待其他的节点都释放
boolean isGetTheLock = ourIndex == 0;
String pathToWatch = isGetTheLock ? null : children.get(ourIndex - 1);
if (isGetTheLock) {
haveGotLock = true;
} else {
/**
* 获取当前节点的次小的节点,并监听节点的变化
*/
String previousSequencePath = basePath.concat("/").concat(pathToWatch);
final CountDownLatch latch = new CountDownLatch(1);
final IZkDataListener previousListener = new IZkDataListener() {
public void handleDataDeleted(String dataPath) throws Exception {
latch.countDown();
}
public void handleDataChange(String dataPath, Object data) throws Exception {
// ignore
}
};
try {
// 如果节点不存在会出现异常
client.subscribeDataChanges(previousSequencePath, previousListener);
if (millisToWait != null) {
millisToWait -= (System.currentTimeMillis() - startMillis);
startMillis = System.currentTimeMillis();
if (millisToWait <= 0) {
doDelete = true; // timed out - delete our node
break;
}
latch.await(millisToWait, TimeUnit.MICROSECONDS);
} else {
latch.await();
}
} catch (ZkNoNodeException e) {
// ignore
} finally {
client.subscribeDataChanges(previousSequencePath, previousListener);
}
}
}
} catch (Exception e) {
// 发生异常需要删除节点
doDelete = true;
throw e;
} finally {
// 如果需要删除节点
if (doDelete) {
deleteOurPath(ourPath);
}
}
return haveGotLock;
}
private String getLockNodeNumber(String str, String lockName) {
int index = str.lastIndexOf(lockName);
if (index >= 0) {
index += lockName.length();
return index <= str.length() ? str.substring(index) : "";
}
return str;
}
private List<String> getSortedChildren() throws Exception {
try {
List<String> children = client.getChildren(basePath);
Collections.sort(children, new Comparator<String>() {
public int compare(String lhs, String rhs) {
return getLockNodeNumber(lhs, lockName).compareTo(getLockNodeNumber(rhs, lockName));
}
});
return children;
} catch (ZkNoNodeException e) {
client.createPersistent(basePath, true);
return getSortedChildren();
}
}
protected void releaseLock(String lockPath) throws Exception {
deleteOurPath(lockPath);
}
/**
* 尝试获取锁
*
* @param time
* @param unit
* @return
* @throws Exception
*/
protected String attemptLock(long time, TimeUnit unit) throws Exception {
final long startMillis = System.currentTimeMillis();
final Long millisToWait = (unit != null) ? unit.toMillis(time) : null;
String ourPath = null;
boolean hasTheLock = false;
boolean isDone = false;
int retryCount = 0;
// 网络闪断需要重试一试
while (!isDone) {
isDone = true;
try {
ourPath = createLockNode(client, lockSolutionPath);
hasTheLock = waitToLock(startMillis, millisToWait, ourPath);
} catch (ZkNoNodeException e) {
if (retryCount++ < MAX_RETRY_COUNT) {
isDone = false;
} else {
throw e;
}
}
}
if (hasTheLock) {
return ourPath;
}
return null;
}
}
SimpleDistributedLock
public class SimpleDistributedLock extends BaseDistributedLock implements DistributedLock {
private String curLockName = null;
public SimpleDistributedLock(ZkClient client, String path, String lockName) {
super(client, path, lockName);
}
@Override
public void acquire() throws Exception {
curLockName = attemptLock(0l, null);
}
@Override
public boolean acquire(long time, TimeUnit unit) throws Exception {
if ((curLockName = attemptLock(time, unit)) == null) {
return false;
}
return true;
}
@Override
public void release() throws Exception {
System.out.println("release:" + curLockName);
releaseLock(curLockName);
}
}