Zookeeper叢集Java操作(zookeeper3.4.5)

來源:互聯網
上載者:User

標籤:java操作zk叢集   zookeeper叢集java操作   

1.zk叢集java簡單操作1.0 包目錄結構lib取自zookpeeper官方壓縮包(基於zk3.4.5測試通過)

1.1 AbstractZooKeeper.java

package com.ccy.zk;import java.io.IOException;import java.util.concurrent.CountDownLatch;import org.apache.log4j.Logger;import org.apache.zookeeper.WatchedEvent;import org.apache.zookeeper.Watcher;import org.apache.zookeeper.Watcher.Event.KeeperState;import org.apache.zookeeper.ZooKeeper;/** *  * <p>  * Title: AbstractZooKeeper.java  * Package com.ccy.zk  * </p> * <p> * Description: TODO * <p> * @author Tom.Cai * @created 2015-4-13 下午9:48:50  * @version V1.0  * */public class AbstractZooKeeper implements Watcher {  protected Logger logger = Logger.getLogger(AbstractZooKeeper.class);       //緩衝時間       private static final int 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 process(WatchedEvent event) {          if(event.getState()==KeeperState.SyncConnected){              countDownLatch.countDown();          }      }           //關閉叢集    public void close() throws InterruptedException{             zooKeeper.close();         }    } 

1.2 ZooKeeperOperator.java(測試類別)

package com.ccy.zk;import java.util.List;import org.apache.log4j.Logger;import org.apache.zookeeper.CreateMode;import org.apache.zookeeper.KeeperException;import org.apache.zookeeper.ZooDefs.Ids;import org.apache.zookeeper.data.Stat;/** *  * <p>  * Title: ZooKeeperOperator.java  * Package com.ccy.zk  * </p> * <p> * Description: TODO * <p> * @author Tom.Cai * @created 2015-4-13 下午9:49:00  * @version V1.0  * */public class ZooKeeperOperator extends AbstractZooKeeper {private static Logger logger = Logger.getLogger(ZooKeeperOperator.class); /** *  *建立znode * */public void create(String path,byte[] data)throws KeeperException, InterruptedException{/** * 此處採用的是建立的是持久化節點:PERSISTENT表示不會因串連的斷裂而刪除節點 * EPHEMERAL 表示The znode will be deleted upon the client's disconnect. */ this.zooKeeper.create(path, data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);}/** *  *擷取節點資訊 * */public void getChild(String path) throws KeeperException, InterruptedException{   try{List<String> list=this.zooKeeper.getChildren(path, false);if(list.isEmpty()){logger.info(path+"中沒有節點");System.out.println(path+"中沒有節點");}else{for(String child:list){System.out.println("節點:"+child);}}}catch (KeeperException.NoNodeException e) {// TODO: handle exception throw e;   }}/** *  *擷取節點資料 * */public byte[] getData(String path) throws KeeperException, InterruptedException {           return  this.zooKeeper.getData(path, false,null);       }  /** * 修改節點資料 */public Stat setDate(String path,byte[] data,int version) throws KeeperException, InterruptedException{return this.zooKeeper.setData(path, data, version);}/** * 刪除節點 */public void deleteNode(final String path,int version) throws InterruptedException, KeeperException{ this.zooKeeper.delete(path, version);} public static void main(String[] args) { try {               ZooKeeperOperator zkoperator = new ZooKeeperOperator();               zkoperator.connect("192.168.80.100");                        //操作1:建立一個持久化節點            //zkoperator.create("/test1", "zk".getBytes());            //zkoperator.create("/test1/test11", "zk".getBytes());            //zkoperator.create("/test1/test12", "zk".getBytes());                        //操作2:取出原有的節點            //byte[] b = zkoperator.getData("/test1");            //System.out.println(new String(b));                        //操作3:設定節點資料            //zkoperator.setDate("/test1", "zk1".getBytes(), 1);                                    //操作4:刪除節點操作            zkoperator.deleteNode("/test1/test12", 0);                        //擷取子節點           // zkoperator.getChild("/test1");                                   zkoperator.close();                                   } catch (Exception e) {               e.printStackTrace();           }   }}

我的個人網站:http://www.caicongyang.com ;

我的CSDN部落格地址: http://blog.csdn.net/caicongyang ;




Zookeeper叢集Java操作(zookeeper3.4.5)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.