首页 > 代码库 > zookeeper入门教程

zookeeper入门教程

zookeeper使用场景,不是很难了解,感觉zk监听节点变化,这个功能比较厉害。zk存储的节点组织结构有点像unix文件系统

1.安装zk

运行环境

centos 7

java 8

zookeeper

1.1 下载zk

https://zookeeper.apache.org/releases.html

下载解压到/opt/soft,复制conf/zoo_sample.cfg到zoo.cfg

安装完成,就是一个解压,拷贝配置文件的过程。当然这是单机版的安装,实际生产环境是不建议单台安装zk。存在单点故障问题,建议集群部署zk,zk使用的是ZAB协议,基于PAXOS的,建议部署3台以上的奇数。方便选出zk集群leader。

1.2 启动zk

bin/zkServer.sh start

通过bin/zkCli.sh连接zk检查是否启动成功,或者jps查看进程是否存在,ps 等命令

2. java客户端连接zk

通过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 = http://www.mamicode.com/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节点的变化用的。

 

zookeeper入门教程