Zookeeper的安装与HelloWorld

来源:互联网 发布:java读取文件为字符串 编辑:程序博客网 时间:2024/05/18 02:18

(一) 安装

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

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

tar -zxvf zookeeper-3.4.5.tar

1.2 修改配置文件

这里写图片描述

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

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

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

mkdir -p /apps/dat/zookeeper/

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

vi myid

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

1.3 启动

zkServer.sh start

1.4 查看zookeeper的状态

zkServer.sh status

1.5 停止

zkServer.sh stop

(二) 客户端操作

2.1 启动客户端

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

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

[zk: localhost:2181(CONNECTED) 0]

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

ls /

这里写图片描述

2.2 创建一个新的znode

create /

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

2.3 get获取节点信息

get /test

2.4 set 设置节点信息

set /test  world

这里写图片描述

2.5 删除

删除某个节点

delete /test

或者用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>

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();        }    }}

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

0 0
原创粉丝点击