zookeeper实践

Zookeeper实践

一、安装搭建:

在h1、h2、h3三台机器上安装搭建Zookeeper

在h1机器上执行以下操作:

1.下载解压zookeeper压缩包
2.cd /home/vagrant/zookeeper-3.4.10
    mkdir data
    mkdir logs
    cd data
    vi myid
        1
3.cd /home/vagrant/zookeeper-3.4.10/conf
    cp zoo_sample.cfg zoo.cfg
    vi zoo.cfg
        tickTime=2000
        initLimit=10
        syncLimit=5
        dataDir=/home/vagrant/zookeeper-3.4.10/data
        dataLogDir=/home/vagrant/zookeeper-3.4.10/logs
        clientPort=2181
        server.1=h1:2888:3888
        server.2=h2:2888:3888
        server.3=h3:2888:3888
4.
    scp -r zookeeper-3.4.10 root@h2:/home/vagrant
    scp -r zookeeper-3.4.10 root@h3:/home/vagrant

    vi /etc/profile
        export ZOOKEEPER_HOME=/home/vagrant/zookeeper-3.4.10
        export PATH=$JAVA_HOME/bin:$HIVE_HOME/bin:$SCALA_HOME/bin:$SPARK_HOME/bin:$HBASE_HOME/bin:$ZOOKEEPER_HOME/bin:$PATH
    
    source /etc/profile

将h2上的zookeeper的data目录下的myid改为2,设置zookeeper环境变量

将h3上的zookeeper的data目录下的myid改为3,设置zookeeper环境变量

二、启动操作zookeeper

在三台机器上分别启动zookeeper:zkServer.sh start
分别查看状态:zkServer.sh status
连接客户端:zkCli.sh -server h1:2181
image
1.查看当前zookeeper所包含的内容:ls /
2.创建一个新的Znode节点"syj",以及相关字符:create /syj 123456
3.查看:ls /
4.获取某节点对应的字符:get /syj
5.设置节点对应的字符:set /syj 7890
6.get /syj
7.删除节点:delete /syj
8.查看:ls /
image
分别关闭:zkServer.sh stop

三、java API操作zookeeper

package testZK;

import java.io.IOException;
import java.util.List;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.apache.zookeeper.ZooDefs.Ids;

public class ZkTest {
    public ZooKeeper zk = null;

    public static void main(String[] args) {
        
        ZkTest zkTest = new ZkTest();
        zkTest.createConnection("10.0.34.112:2181", 10000);
        
//      zkTest.createNode("/syj/cy", "1111");
//      zkTest.deleteNode("/syj/cy");
//      zkTest.setDate("/syj", "11");
        
//      String data = zkTest.getData("/syj");
//      System.out.println(data);
        
//      zkTest.getChildren("/");
        zkTest.isExist("/syj");
        
        zkTest.closeConnection();
        
    }
    
    // 建立连接
    public void createConnection(String connectString,int timeout) {
        try {
            zk = new ZooKeeper(connectString, timeout, null);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 关闭连接
    public void closeConnection() {
        try {
            this.zk.close();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    
    // 创建节点
    public void createNode(String path,String data) {
        try {
            this.zk.create(path, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    
    // 删除节点
    public void deleteNode(String path) {
        try {
            this.zk.delete(path, -1);
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    
    // 设置节点对应数据
    public void setDate(String path, String data) {
        try {
            this.zk.setData(path, data.getBytes(), -1);
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    
    // 获取某节点对应数据
    public String getData(String path) {
        String ret = null;
        try {
            ret = new String(this.zk.getData(path, false, null));
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return ret;
    }
    
    // 查询某节点的子节点
    public void getChildren(String path) {
        try {
            List<String> zkChildren = this.zk.getChildren(path, false);
            for (int i = 0; i < zkChildren.size(); i++) {
                System.out.println(zkChildren.get(i));
            }
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    
    // 判断某节点是否存在
    public void isExist(String path) {
        try {
            Stat x = this.zk.exists(path, false);
            if(x != null) {
                System.out.println("存在此节点");
//              System.out.println(x);
            }else {
                System.out.println("不存在此节点");
            }
            
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    
}

推荐阅读更多精彩内容