zookeeper使用場景,不是很難了解,感受zk監聽節點變化,這個功能比較厲害。zk存儲的節點組織結構有點像unix文件系統html
1.安裝zkjava
運行環境apache
centos 7 java 8 zookeeper
1.1 下載zkcentos
https://zookeeper.apache.org/releases.htmlide
下載解壓到/opt/soft,複製conf/zoo_sample.cfg到zoo.cfg測試
安裝完成,就是一個解壓,拷貝配置文件的過程。固然這是單機版的安裝,實際生產環境是不建議單檯安裝zk。存在單點故障問題,建議集羣部署zk,zk使用的是ZAB協議,基於PAXOS的,建議部署3臺以上的奇數。方便選出zk集羣leader。this
1.2 啓動zkspa
bin/zkServer.sh start
經過bin/zkCli.sh鏈接zk檢查是否啓動成功,或者jps查看進程是否存在,ps 等命令線程
2. java客戶端鏈接zkunix
經過java客戶端鏈接zk demo
TestClient.java
import org.apache.zookeeper.*; import org.apache.zookeeper.data.Stat; import java.io.IOException; import java.util.List; import java.util.concurrent.CountDownLatch; /** * Created by gxf on 2016/12/17. */ public class TestClient extends Thread implements Watcher{ public ZooKeeper zooKeeper; private static final int SESSION_TIME_OUT = 2000; private CountDownLatch countDownLatch = new CountDownLatch(1); public void connectToZookeeper(String host) throws IOException, InterruptedException { zooKeeper = new ZooKeeper(host, SESSION_TIME_OUT, this); countDownLatch.await(); System.out.println("zookeeper connect ok.e"); } public byte[] getNodeValue(String path) throws KeeperException, InterruptedException { return this.zooKeeper.getData(path, true, null); } public void closeZookeeperConnection() throws InterruptedException { zooKeeper.close(); } public String createNode(String path, byte data[]) throws KeeperException, InterruptedException { return this.zooKeeper.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } public List<String> getChildren(String path) throws KeeperException, InterruptedException { return this.zooKeeper.getChildren(path, false); } public Stat setDada(String path, byte[] data, int version) throws KeeperException, InterruptedException { return this.zooKeeper.setData(path, data, version); } public void deleteNode(String path, int version) throws KeeperException, InterruptedException { this.zooKeeper.delete(path, version); } @Override public void process(WatchedEvent watchedEvent) { System.out.println("監聽到事件 : " + watchedEvent.getState()); System.out.println("事件類型: " + watchedEvent.getType()); try { this.getNodeValue("/zk_test"); } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } if(watchedEvent.getState() == Event.KeeperState.SyncConnected){ System.out.println("watcher received event."); countDownLatch.countDown(); }//if } public void run(){ while(true){ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String args[]) throws IOException, InterruptedException, KeeperException { TestClient testClient = new TestClient(); String host = "192.168.211.129:2181"; testClient.connectToZookeeper(host); testClient.start(); byte[] data = testClient.getNodeValue("/zk_test"); String dataOfString = new String(data); System.out.println("dataOfString = " + dataOfString); // //// testClient.createNode("/zk_book", "books".getBytes()); // byte []data1 = testClient.getNodeValue("/zk_book"); // String data1OfString = new String(data1); // System.out.println("data1OfString = " + data1OfString); // testClient.closeZookeeperConnection(); } }
這裏起了一個線程,主要是測試watch節點的變化用的。