maven依赖如下 监控节点状态实现回调
<dependency>
<groupId>org.apache.curator</groupId><artifactId>curator-recipes</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.5.0</version>
</dependency>
public class CuratorTest
{
private static AtomicInteger count = new AtomicInteger(0);
public static void main(String args[])
{
CuratorFramework zkClient = CuratorFrameworkFactory.builder()
.connectString("127.0.0.1:2181")
.retryPolicy(new RetryNTimes(Integer.MAX_VALUE, 1000)).build();
try
{
zkClient.start();
Stat stat = zkClient.checkExists().forPath("/curator");
if (stat != null)
{
zkClient.delete().deletingChildrenIfNeeded()
.forPath("/curator");
}
zkClient.create().forPath("/curator");
for (int i = 0; i < 10; i++)
{
addWatcher(zkClient, "/curator/" + i);
}
System.in.read();
} catch (Exception e)
{
zkClient.close();
} finally
{
zkClient.close();
}
}
public static void addWatcher(final CuratorFramework zkClient,
final String path)
{
try
{
Stat stat = zkClient.checkExists().forPath(path);
if (stat == null)
{
zkClient.create().forPath(path);
}
zkClient.getChildren().usingWatcher(new CuratorWatcher()
{
public void process(WatchedEvent event) throws Exception
{
count.getAndIncrement();
System.out
.println("Change========================" + count);
}
}).forPath(path);
} catch (Exception e)
{
e.printStackTrace();
}
}
}
public class CuratorAdd
{
public static void main(String args[])
{
CuratorFramework zkClient = CuratorFrameworkFactory.builder()
.connectString("127.0.0.1:2181")
.retryPolicy(new RetryNTimes(Integer.MAX_VALUE, 1000)).build();
zkClient.start();
try
{
Stat stat = zkClient.checkExists().forPath("/curator");
if (stat != null)
{
zkClient.delete().deletingChildrenIfNeeded().forPath("/curator");
}
zkClient.create().forPath("/curator");
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
CuratorAdd add = new CuratorAdd();
for(int i=0 ;i<10; i++)
{
Thread thread = new Thread(add.new RunnTest(i));
thread.start();
}
}
private class RunnTest implements Runnable
{
private int count ;
public RunnTest(int i)
{
count = i ;
}
@Override
public void run()
{
// TODO Auto-generated method stub
CuratorFramework zkClient = CuratorFrameworkFactory.builder()
.connectString("127.0.0.1:2181")
.retryPolicy(new RetryNTimes(Integer.MAX_VALUE, 1000)).build();
zkClient.start();
try
{
Stat stat = zkClient.checkExists().forPath("/curator/" + count);
if (stat != null)
{
zkClient.delete().forPath("/curator/" + count);
}
//lock.release();
} catch (Exception e)
{
}
}
}
}
private boolean ZkLeaderElecte(CuratorFramework zkClient,String zkNodeName)
{
try
{
logger.debug("method ZkLeaderElecte start,正在选举");
Stat stat = zkClient.checkExists().forPath(zkNodeName);
if (null == stat)
{
zkClient.create().forPath(zkNodeName);
}
LeaderLatch example = new LeaderLatch(zkClient, zkNodeName);
example.start();
example.await();
if (example.hasLeadership())
{
logger.debug("method ZkLeaderElecte start,选举完成");
example.close();
return true;
}
example.close();
}
catch (Exception e)
{
logger.error(ExceptionUtils.getFullStackTrace(e));
}
logger.debug("method ZkLeaderElecte start,选举完成");
return false;
}