1、zookeeper实战2Zookeeper第二周一、Watcher的原理以及API实践(API监听一次就会失效)基于zookeeper的API,使用一个线程监测zookeeper集群节点的变化情况,具体代码如下:import java.io.IOException;import java.util.List;import org.apache.log4j.Logger;import org.apache.zookeeper.KeeperException;import org.apache.zookeeper.WatchedEvent;import org.apache.zookeeper.W
2、atcher;import org.apache.zookeeper.Watcher.Event.EventType;/* * * author hadoop * */public class ZKNodeWatcher extends AbstractZooKeeper implements Runnable private static List nodeList;/ 所要监控的结点的子结点列表 String monitorNode; private static Logger Log = Logger.getLogger(ZKNodeWatcher.class); public ZKNo
3、deWatcher(String monitorNode) super(); this.monitorNode = monitorNode; try this.connect(master); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); catch (InterruptedException e) / TODO Auto-generated catch block e.printStackTrace(); public String getMonitorNode() return mo
4、nitorNode; public void setMonitorNode(String monitorNode) this.monitorNode = monitorNode; public void run() synchronized (this) try monitorNodes(getWatcher(); catch (InterruptedException e) / TODO Auto-generated catch block Log.error(e); public Watcher getWatcher() return new Watcher() Override publ
5、ic void process(WatchedEvent event) / 结点数据改变之前的结点列表 List nodeListBefore = nodeList; / 主结点的数据发生改变时 if (event.getType() = EventType.NodeDataChanged) try Log.info(节点 数据发生变化: + event.getPath()+-数据变为+new String(getData(event.getPath(); catch (Exception e) / TODO Auto-generated catch block Log.error(e); /
6、 节点被删除是触发 if (event.getType() = EventType.NodeDeleted) Log.info(节点被删除: + event.getPath(); / 节点新创建时触发 if (event.getType() = EventType.NodeCreated) Log.info(新节点创建: + event.getPath(); / 获取更新后的nodelist try nodeList = zooKeeper.getChildren(event.getPath(), false); catch (Exception e) Log.error(e); Log.in
7、fo(event.getPath() + has no child); List nodeListNow = nodeList; / 增加结点 if (nodeListBefore.size() 0) for (String nodeName : nodeList) try zooKeeper.exists(getMonitorNode() + / + nodeName, wc); catch (Exception e) Log.error(e); /* * * 获取节点数据 * */ public byte getData(String path) throws KeeperExceptio
8、n,InterruptedException return this.zooKeeper.getData(path, false, null); public static void main(String args) new Thread(new ZKNodeWatcher(/node).start(); import java.io.IOException;import java.util.concurrent.CountDownLatch;import org.apache.log4j.Logger;import org.apache.zookeeper.WatchedEvent;imp
9、ort org.apache.zookeeper.Watcher;import org.apache.zookeeper.Watcher.Event.KeeperState;import org.apache.zookeeper.ZooKeeper;/* * * author hadoop * */public class AbstractZooKeeper implements Watcher protected Logger logger = Logger.getLogger(AbstractZooKeeper.class); /缓存时间 private static final int
10、SESSION_TIME = 2000; protected ZooKeeper zooKeeper; protected CountDownLatch countDownLatch=new CountDownLatch(1); /连接zk集群 public void connect(String hosts) throws IOException, InterruptedException zooKeeper = new ZooKeeper(hosts,SESSION_TIME,this); countDownLatch.await(); /zk处理 Override public void
11、 process(WatchedEvent event) if(event.getState()=KeeperState.SyncConnected) countDownLatch.countDown(); System.out.println(已经触发了 + event.getType() + 事件!); /关闭集群 public void close() throws InterruptedException zooKeeper.close(); 运行之后,客户端一直在监测zookeeper集群的节点情况在另外客户端进行新建节点操作API均能监测到节点变化信息2、zookeeper的ACL
12、实践 World模式 Auth模式 Digest模式 IP模式 Super模式由于使用super模式其用户名以及密码需要经过sha1和base64转码,利用以下的Java代码实现加密转码。import java.io.IOException;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import org.apache.zookeeper.KeeperException;/* * * author hadoop * */public class SuperAcl public
13、static void main(String args) throws IOException, InterruptedException, KeeperException String passwd = super:admin123; System.out.println(generateDigest(passwd); static public String generateDigest(String idPassword) String parts = idPassword.split(:, 2); byte digest = null; try digest = MessageDig
14、est.getInstance(SHA1).digest(idPassword.getBytes(); catch (NoSuchAlgorithmException e) e.printStackTrace(); return parts0 + : + base64Encode(digest); static final private String base64Encode(byte b) StringBuilder sb = new StringBuilder(); for (int i = 0; i b.length;) int pad = 0; int v = (bi+ & 0xff
15、) 16; if (i b.length) v |= (bi+ & 0xff) 8; else pad+; if (i 18); sb.append(encode(v 12); if (pad 6); else sb.append(=); if (pad 1) sb.append(encode(v); else sb.append(=); return sb.toString(); static final private char encode(int i) i &= 0x3f; if (i 26) return (char) (A + i); if (i 52) return (char) (a + i - 26); if (i 62) return (char) (0 + i - 52); return i = 62 ? + : /; 修改Zookeeper的启动脚本zkServer.sh,在start附件(100行左右)加入以下配置:-Dzookeeper.DigestAuthenticationProvider.superDigest=super:/o7XEyClGiOcKnJmPVFVYU8AQdw=重新启动Zookeeper服务:zkServer.sh restart使用super:admin123连接Zookeeper服务器就可以对里面的节点任意妄为了。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1