Java程序操作Zookeeper
1.创建java项目并导入相关jar包
主要jar包在主目录下
项目需要的相关依赖的jar包在zookeeper的解压文件的lib目录下就有
将这几个jar包导入项目即可
maven项目坐标
<dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> <type>pom</type> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency>
2.API简单使用
2.1配置Zookeeper对象
// zookeeper的服务器地址 private String connectString = "192.168.88.121:2181,192.168.88.122:2181,192.168.88.123:2181"; // 连接超时时间 private int sessionTimeout = 2000; private ZooKeeper zk = null; /** * 设置zookeeper对象 * @throws IOException */ @Before public void setZookeeper() throws IOException { zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() { /** * 事件触发的回调方法 */ @Override public void process(WatchedEvent event) { } }); System.out.println("---"+zk); }
常用API操作
/** * create 方法参数 * 第一个参数 路径 * 第二个参数 值 bytes * 第三个参数 对节点的访问控制 * 第四个参数 节点的类型 短暂 永久 序号 * @throws KeeperException * @throws InterruptedException */ @Test public void create() throws KeeperException, InterruptedException { String path = zk.create("/app3", "123".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); System.out.println(path); } /** * 判断节点是否存在 * @throws InterruptedException * @throws KeeperException */ @Test public void exist() throws KeeperException, InterruptedException{ // 设置为 true 会调用 zk中的监听器 Stat stat = zk.exists("/app1", true); if(stat!=null){ System.out.println("存在。。。。"+stat.getDataLength()); }else{ System.out.println("不存在。。。"); } } /** * 获取子节点 * @throws InterruptedException * @throws KeeperException */ @Test public void getChilren() throws KeeperException, InterruptedException{ List<String> list = zk.getChildren("/", true); for (String s : list) { System.out.println(s); } } /** * 获取节点的内容 * @throws InterruptedException * @throws KeeperException */ @Test public void getData() throws KeeperException, InterruptedException{ byte[] b = zk.getData("/app1", false, null ); System.out.println(new String(b)); } /** * 修改节点内容 * version -1 自动维护 * @throws InterruptedException * @throws KeeperException */ @Test public void setData() throws KeeperException, InterruptedException{ Stat s = zk.setData("/app1", "test".getBytes(), -1); } /** * 删除节点 * * @throws InterruptedException * @throws KeeperException */ @Test public void deleteNode() throws InterruptedException, KeeperException{ zk.delete("/app1", -1); }
3.监听器的使用
@Before public static void setUpBeforeClass() throws Exception { zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() { /** * 监听器 */ @Override public void process(WatchedEvent event) { System.out.println("事件触发..."); } }); } /** * 获取子节点 * getChildren(path,watch?)监听的事件是:节点下的子节点增减变化事件 * @throws InterruptedException * @throws KeeperException */ @Test public void getChilren() throws KeeperException, InterruptedException{ List<String> list = zk.getChildren("/", new Watcher() { @Override public void process(WatchedEvent event) { // TODO Auto-generated method stub System.out.println("监控节点下的子节点被改变..."+event.getPath()); } }); for (String s : list) { System.out.println(s); } Thread.sleep(Long.MAX_VALUE); } /** * 获取节点的内容 * getData(path,watch?)监听的事件是:节点数据变化事件 * @throws InterruptedException * @throws KeeperException */ @Test public void getData() throws KeeperException, InterruptedException{ byte[] b = zk.getData("/app1", new Watcher() { @Override public void process(WatchedEvent event) { System.out.println("--数据改变了--"); } }, null ); System.out.println(new String(b)); Thread.sleep(Long.MAX_VALUE); }
~zookeeper的javaAPI操作还是比较简单的,本文就介绍到这里