Zookeeper的安装与HelloWorld

来源:互联网 发布:陕西干部网络培训网 编辑:程序博客网 时间:2024/04/30 19:36

(一) 安装

ps: 我的zookeeper集群有三台机器,分别为IP1、IP2、IP3

1.1将zookeeper的包分别拷贝到/apps/svr/目录下进行解压

tar -zxvf zookeeper-3.4.5.tar
  • 1
  • 1

1.2 修改配置文件

这里写图片描述

(1)配置server,如上图红色部分所示

(2)配置dataDir,图中设置为/apps/dat/zookeeper

如果所在目录不存在,创建目录

mkdir -p /apps/dat/zookeeper/
  • 1
  • 1

在该目录下创建myid文件,设置其内容

vi myid
  • 1
  • 1

如server.1对应IP1,那么在IP1机器的myid中填写1;其他依次类推!

1.3 启动

zkServer.sh start
  • 1
  • 1

1.4 查看zookeeper的状态

zkServer.sh status
  • 1
  • 1

1.5 停止

zkServer.sh stop
  • 1
  • 1

(二) 客户端操作

2.1 启动客户端

zkCli.sh -server IP1:2181IP2:2181IP3:2181
  • 1
  • 1

成功后,应该会进到提示符下,类似下面这样:

[zk: localhost:2181(CONNECTED) 0]

2.2 使用ls查看zookeeper包含的内容

ls /
  • 1
  • 1

这里写图片描述

2.2 创建一个新的znode

create /
  • 1
  • 1

这里写图片描述 
这个命令创建了一个新的 znode 节点“ test”以及与它关联的字符串:

2.3 get获取节点信息

get /test
  • 1
  • 1

2.4 set 设置节点信息

set /test  world
  • 1
  • 1

这里写图片描述

2.5 删除

删除某个节点

delete /test
  • 1
  • 1

或者用rmr 用来递归删除某个节点及其所有子节点

(三) hello world

我们不使用原生的zookeeper API,使用功能更加强大的zkclient.

3.1 pom.xml

<dependency>            <groupId>org.apache.zookeeper</groupId>            <artifactId>zookeeper</artifactId>            <version>3.4.5</version>            <exclusions>                <exclusion>                    <artifactId>slf4j-log4j12</artifactId>                    <groupId>org.slf4j</groupId>                </exclusion>                <exclusion>                    <artifactId>log4j</artifactId>                    <groupId>log4j</groupId>                </exclusion>                <exclusion>                    <artifactId>slf4j-api</artifactId>                    <groupId>org.slf4j</groupId>                </exclusion>            </exclusions>        </dependency>        <dependency>            <groupId>com.101tec</groupId>            <artifactId>zkclient</artifactId>            <version>0.6</version>        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

3.2 HelloWorld

public class ZookeeperTest {    public static void main(String[] args) {        ZkClient zkClient = new ZkClient("xx.xx.xx.xx:2181,xx.xx.xx.xx:2181,xx.xx.xx.xx:2181");        String node = "/kevin";        /**         * 查询节点是否存在         */        if(zkClient.exists(node)){            System.out.println("节点 "+node+" exist in the zookeeper..");         }        if(!zkClient.exists("/test")){            System.out.println("节点 test not exist in the zookeeper..");         }        /**         * 创建节点         */        if (!zkClient.exists(node+"/child1_of_kevin")) {        zkClient.createPersistent(node+"/child1_of_kevin");        }        if (!zkClient.exists(node+"/child2_of_kevin")) {        zkClient.createPersistent(node+"/child2_of_kevin");        }        if (!zkClient.exists(node+"/child3_of_kevin")) {        zkClient.createPersistent(node+"/child3_of_kevin","lucy");        }                    /**         * 写入数据         */        if(zkClient.exists(node+"/child1_of_kevin")){            zkClient.writeData(node+"/child1_of_kevin",new Person("Tom",6));        }        if(zkClient.exists(node+"/child2_of_kevin")){            zkClient.writeData(node+"/child2_of_kevin",new Person("Jack",4));        }        /**         * 读数据         */        if (zkClient.exists(node)) {            System.out.println(zkClient.getChildren(node));        }        if(zkClient.exists(node+"/child1_of_kevin")){            System.out.println(zkClient.readData(node+"/child1_of_kevin"));        }        if(zkClient.exists(node+"/child2_of_kevin")){            System.out.println(zkClient.readData(node+"/child2_of_kevin"));        }        if(zkClient.exists(node+"/child2_of_kevin")){            System.out.println(zkClient.readData(node+"/child3_of_kevin"));        }    }    static class Person implements Serializable{        /**         *          */        private static final long serialVersionUID = -3336167432621630183L;        private String name;        private int age;        public Person(String name,int age){            this.name=name;            this.age=age;        }        public String getName() {            return name;        }        public void setName(String name) {            this.name = name;        }        public int getAge() {            return age;        }        public void setAge(int age) {            this.age = age;        }        @Override        public String toString() {            StringBuilder builder =new StringBuilder();            builder.append("{\"name\":").append(this.name).append(",");            builder.append("\"age\":").append(this.age).append("}");            return builder.toString();        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101

结果如下: 
这里写图片描述

0 0