使用zookeeper实现静态数据中心化配置管理
使用zookeeper实现静态数据中心化配置管理
各个项目都会存在静态数据配置,这些数据平台变化很少,为提高性能一般采用缓存的方式缓存数据。如果采用分布式缓存,网络成本比较高不太适合
一般采用本地缓存,在单机环境下修改缓存数据方式比较简单,更新数据库的同时,也修改更新本地缓存,但在集群模式下可就没有这么简单,最简单直接
的方式就是一台台服务器等去修改,费时费力。或者通过jms消息同步的方式进行处理,具体不描述太多。这里介绍另外一种方式,即使用zookeeper实现
静态数据中心化配置管理。
先简单介绍一下zookeeper,ZooKeeper是近期比较热门的一个Paxos算法实现,也是Hadoop下的一个子项目,被认为是Google Chubby的开源版,
主要用来在分布式环境下提供分布式锁、配置管理、名字服务、群组服务。它具有很高的可用性、稳定性、可靠性。它在分布式应用中像一把瑞士军刀,
很多地方都用得着。
为了缓存数据,需要一个HashMap对象,但JDK中的HashMap是不支持分布式环境同步数据的。为此需要结合zookeeper重新实现一个HashMap,
取名叫做ZKHashMap。连接到zookeeper服务器的任何一个客户端修改了ZKHashMap。能够通过zookeeper服务器主动同步到其它连接到zookeeper服务器的客户端。
搜索发现已经有这样的实现工具包:menagerie(https://github.com/openUtility/menagerie)。官网描述为:Menagerie is an implementation of the
Java Concurrency Libraries based on the popular Apache ZooKeeper。主要功能有分布式集合、分布式队列、分布式锁的实现。有兴趣可以去学习一下源码
为了演示其效果,做个测试,代码如下,
public class TestZKHashMap {private static final String hostString = "localhost:2181";private static final String baseHashMapPath = "/test-maps";private static final String baseLockPath = "/test-locks";private static final String Init_Done = "/initData";private static final int timeout = 2000;private static ZooKeeper zk1, zk2; private static ZkSessionManager zkSessionManager1, zkSessionManager2; private static ZkHashMap<String,Person> testMap1, testMap2; private static Serializer<Entry<String, Person>> serializer = new JavaSerializer<String, Person>(); public static void main(String[] args) throws Exception { setup(); final CountDownLatch latch = new CountDownLatch(2);Thread thread1 = new Thread() {@Overridepublic void run() {initData(zk1, zkSessionManager1);String znode = "test1";Person person = new Person("melin"); testMap1.put(znode, person);latch.countDown();}};Thread thread2 = new Thread() {@Overridepublic void run() {initData(zk2, zkSessionManager2);String znode = "test2";Person person = new Person("melin"); testMap2.put(znode, person);latch.countDown();}};thread1.start();thread2.start();latch.await();Thread.sleep(100);if(testMap1.containsKey("test2")) {System.out.println("testMap1 包含 test2");}if(testMap2.containsKey("test1")) {System.out.println("testMap2 包含 test1");}tearDown();}private static ZooKeeper newZooKeeper() throws IOException { return new ZooKeeper(hostString, timeout,new Watcher() { @Override public void process(WatchedEvent event) { System.out.println("+++++++++"+event); } }); }public static void setup() throws Exception { zk1 = newZooKeeper(); zk1.create(baseHashMapPath,new byte[]{}, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); zkSessionManager1 = new BaseZkSessionManager(zk1); testMap1 = new ZkHashMap<String, Person>(baseHashMapPath, zkSessionManager1, serializer); zk2 = newZooKeeper(); zkSessionManager2 = new BaseZkSessionManager(zk2); testMap2 = new ZkHashMap<String, Person>(baseHashMapPath, zkSessionManager2, serializer); } public static void tearDown() throws Exception{ try{ ZkUtils.recursiveSafeDelete(zk1,baseHashMapPath,-1); ZkUtils.recursiveSafeDelete(zk1,baseLockPath,-1); ZkUtils.recursiveSafeDelete(zk1,Init_Done,-1); }catch(KeeperException ke){ //suppress because who cares what went wrong after our tests did their thing? }finally{ zk1.close(); zk2.close(); } } public static void initData(ZooKeeper zk, ZkSessionManager zkSessionManager) { final Lock lock = new ReentrantZkLock(baseLockPath, zkSessionManager); try { if (null == zk.exists(Init_Done, true)) { // if the init_done node not exists we try to init lock.lock(); if(zk.exists(Init_Done, true) != null) { System.out.println("已经初始化数据"+zk); return; } System.out.println("初始化数据"+zk); //创建初始化成功标识,注意这个标志是永久节点 zk.create(Init_Done, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); //工作完成,释放锁 lock.unlock(); } else {// if init_done exists we simply load data from gcih System.out.println("已经初始化数据"+zk); }} catch (Exception e) {e.printStackTrace();} }}