zookeeper Curator客户端
来源:互联网 发布:淘宝首页装修教程视频 编辑:程序博客网 时间:2024/05/15 12:03
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;
}
- ZooKeeper客户端curator应用
- zookeeper Curator客户端
- zookeeper客户端curator使用手记
- zookeeper客户端curator
- ZooKeeper客户端curator组件介绍
- Zookeeper客户端curator常用法
- ZooKeeper的客户端库Curator
- Zookeeper客户端Curator使用详解
- zookeeper开源客户端curator
- zookeeper客户端操作框架curator
- Zookeeper开源客户端框架Curator简介
- Zookeeper开源客户端框架Curator简介
- Zookeeper开源客户端框架Curator简介
- Zookeeper开源客户端框架Curator简介
- Zookeeper开源客户端框架Curator简介
- Zookeeper开源客户端框架Curator简介
- Zookeeper开源客户端框架Curator简介
- ZooKeeper客户端curator介绍及使用
- 经纬度互换和计算距离
- shell实现mysql高可用
- Usaco 生气的奶牛 - (dp&滑窗)|(二分&贪心check)
- response.getWriter().write 中文乱码问题
- X86汇编快速入门
- zookeeper Curator客户端
- 关联规则浅谈以及Apriori算法matlab实现
- Tomcat的常见的组件与部署
- SQLServer数据库通用访问类
- aliyun服务器 在 centos下 挂载新硬盘
- Android4.4之后发送或接收短信后自动删除问题
- 258. Add Digits
- 操作系统基础
- Java动态加载jar文件