Zookeeper实现分布式锁
2015-06-25 22:38
288 查看
package com.billstudy.zookeeper; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.Watcher.Event.EventType; import org.apache.zookeeper.ZooDefs.Ids; import org.apache.zookeeper.ZooKeeper; import org.apache.zookeeper.data.Stat; /** * 注册服务,并且自动监听可用服务列表 * @author Bill * @since V1.0 2015年6月24日 - 上午10:03:24 */ public class AppServer { private ZooKeeper zk = null; // 树前缀 private static final String zkParentPrefix = "/appserver"; private static final String zkChildPrefix = "/app"; // 维护可用列表 private final ArrayList<String> availableServerList = new ArrayList<String>(); public void connectZk(String address){ try { zk = new ZooKeeper("hadoop-server05:2181,hadoop-server06:2181,hadoop-server07:2181", 5000, new Watcher() { @Override public void process(WatchedEvent event) { System.out.println(event.toString()); if (event.getType() == EventType.NodeChildrenChanged && event.getPath().startsWith(zkParentPrefix) ) { flushServerList(); } } }); // 如果根节点没有,则先创建 if (zk.exists(zkParentPrefix, true) == null) { zk.create(zkParentPrefix, "AppServer root dir ".getBytes("UTF-8"), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); System.out.println("znode :" + zkParentPrefix + "is not exists , create successful !"); } // 根据当前address创建临时连续子节点,这样多个不同的app child节点不会重复。 zk会自己维护序列 String childPath = zk.create(zkParentPrefix + zkChildPrefix, address.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); System.out.println("create " + childPath + " successful, address is :" + address); flushServerList(); } catch (Exception e) { e.printStackTrace(); } } /** * 收到子节点更新后,刷新当前可用服务列表 * @author Bill * @since V1.0 2015年6月24日 - 上午10:08:19 */ protected void flushServerList() { availableServerList.clear(); try { List<String> children = zk.getChildren(zkParentPrefix, true); for (String child : children) { byte[] data = zk.getData(zkParentPrefix + "/" + child, true,new Stat()); availableServerList.add(new String(data,"UTF-8")); } System.out.println("current available server list:" + availableServerList); } catch (Exception e) { e.printStackTrace(); } } /** * 此处可以用来处理业务逻辑,目前让主线程挂起 * @author Bill * @since V1.0 2015年6月24日 - 上午10:24:00 */ public void handle(){ try { // System.out.println("handle ..."); TimeUnit.HOURS.sleep(Long.MAX_VALUE); } catch (InterruptedException e) { e.printStackTrace(); } } public static void main(String[] args) { if (args.length != 1) { System.err.println("The program first argument must be address !"); System.exit(1); } AppServer appServer = new AppServer(); appServer.connectZk(args[0]); appServer.handle(); } }
相关文章推荐
- 在ubuntu上搭建android开发环境(1)——安装ubuntu
- devexpress实现单元格合并以及依据条件合并单元格
- Zookeeper实现分布式锁 分类: hadoop Java 2015-06-25 22:38 68人阅读 评论(0) 收藏
- ploymer
- plsql编程学习笔记-2
- 打印杨辉三角
- Zookeeper实现服务上下线监控服务列表
- 当数组元素是应用类型的时候,关于内存分配的理解误区
- Zookeeper实现服务上下线监控服务列表 分类: hadoop Java 2015-06-25 22:37 71人阅读 评论(0) 收藏
- LeetCode(2)
- 黑马程序员-IOS学习笔记(五)类
- CSS基础
- 程序编译是碰到的g++ 对c++11支持问题
- [leetcode] 93.Restore IP Addresses
- hdu - 1068 Girls and Boys (二分图最大独立集+拆点)
- 民族英雄墓地被盗挖 墓碑倒在墓前
- 使用 git 将两个不同 branch 以差分包形式生成补丁
- 6.2.3 散射光(2)OpenGL ES 2.0
- Python20150624
- ie8浏览器不支持placeholder属性