Zookeeper(1):入门

1.前置知识

其他账号切换到root下:

Zookeeper(1):入门_第1张图片

Zookeeper(1):入门_第2张图片 Zookeeper(1):入门_第3张图片Zookeeper(1):入门_第4张图片Zookeeper(1):入门_第5张图片Zookeeper(1):入门_第6张图片Zookeeper(1):入门_第7张图片

Zookeeper(1):入门_第8张图片 Zookeeper(1):入门_第9张图片

2.脚本 

为了方便管理局群,写了一个脚本:

在目录 下,有个zk.sh的脚本。

内容:

#!/bin/bash
case $1 in
"start"){
for i in hadoop100 hadoop101 hadoop102
do
 echo ---------- zookeeper $i 启动 ------------
ssh $i "/opt/module/zookeeper-3.5.7/bin/zkServer.sh start"
done
};;
"stop"){
for i in hadoop100 hadoop101 hadoop102
do
 echo ---------- zookeeper $i 停止 ------------
ssh $i "/opt/module/zookeeper-3.5.7/bin/zkServer.sh stop"
done
};;
"status"){
for i in hadoop100 hadoop101 hadoop102
do
 echo ---------- zookeeper $i 状态 ------------
ssh $i "/opt/module/zookeeper-3.5.7/bin/zkServer.sh status"
done
};;
esac

Zookeeper 集群启动脚本

zk.sh start

Zookeeper 集群停止脚本

 zk.sh stop 

Zookeeper 查看集群状态

zk.sh status

3.命令行语法

Zookeeper(1):入门_第10张图片

Zookeeper(1):入门_第11张图片

Zookeeper(1):入门_第12张图片 Zookeeper(1):入门_第13张图片

Zookeeper(1):入门_第14张图片 Zookeeper(1):入门_第15张图片

Zookeeper(1):入门_第16张图片

Zookeeper(1):入门_第17张图片 Zookeeper(1):入门_第18张图片

Zookeeper(1):入门_第19张图片 Zookeeper(1):入门_第20张图片Zookeeper(1):入门_第21张图片Zookeeper(1):入门_第22张图片

4.java代码操作 

package com.atguigu.zk;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class zkClient {
    ZooKeeper zkClient;
    @Before
    public void init() throws IOException {
        String connectString = "hadoop100:2181,hadoop101:2181,hadoop102:2181";
        int sessionTimeout = 200000000;
        zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                List children = null;
                try {
                    children = zkClient.getChildren("/", true);
                    for (String child : children) {
                        System.out.println(child);
                    }
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }
        );
    }
    @Test
    public void create() throws InterruptedException, KeeperException {
        String nodeCreate = zkClient.create("/atguigu", "ss.avi".getBytes(StandardCharsets.UTF_8), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        System.out.println(nodeCreate);
    }
    @Test
    public void getChild() throws InterruptedException, KeeperException {
        List children = zkClient.getChildren("/", true);
        for (String child : children) {
            System.out.println(child);
        }
        Thread.sleep(Long.MAX_VALUE);
    }
    @Test
    public void exist() throws InterruptedException, KeeperException {
        Stat exists = zkClient.exists("/atguigu", false);
        System.out.println(exists==null?"no":"yes");
    }
}

Zookeeper(1):入门_第23张图片Zookeeper(1):入门_第24张图片

你可能感兴趣的:(zookeeper,linux,服务器)