Zookeeper的安装与HelloWorld

(一) 安装

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值