zookeeper在ubuntu上的安装配置及java操作

我们两清 提交于 2019-12-02 08:53:55

一、ubuntu上安装,配置,操作zookeeper

1.准备安装文件zookeeper-3.4.5.tar

2.上传安装文件到ubuntu,打开xshell,使用快捷键Ctrl+Alt+f打开文件上传窗口,将文件拖拽到ubuntu指定目录下

3.解压tar -zxvf zookeeper-3.4.5.tar.gz

4.cd都zookeeper-3.4.5/conf目录下,将zoo_sample.cfg复制一份,文件名为zoo.cfg

5.打开vi,编辑zoo.cfg文件,添加,保存

单个zookeeper配置:只需在上图加入红框中的内容即可

集群配置:要是多个虚拟机或者机器上都安装了zookeeper,则每个zookeeper配置文件都加入相同的配置即可,

如下:server.1=ubuntu1.2888:3888

          server.2=ubuntu2.2888:3888

          server.3=ubuntu3.2888:3888

bogon和ubuntu1,ubuntu2,ubuntu3是你虚拟机上ubuntu的主机名,其余固定配置,其中2888是zookeeper与其它机器上的zookeeper通信的端口,3888是zookeeper集群选举master的通信端口

6.启动,关闭zookeeper

cd到zookeeper的安装目录下执行

1.bin/zkServer.sh start 启动zookeeper

2.bin/zkServer.sh stop 停止zookeeper

3.bin/zkServer.sh status 查询zookeeper的状态,上图中Mode代表是主是从

7.使用zookeeper客户端

cd到zookeeper的安装目录下执行

1.bin/zkCli.sh

2.执行help命令,查看帮助

二、java代码连接zookeeper,增删改查

1.环境:在springboot的pom.xml添加zookeeper的maven依赖

2.zookeeper的增删改查java代码如下

package zookeeper;

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

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

/**
 * Created by Administrator on 2018/8/15.
 */
public class ZookeeperController {

    ZooKeeper zk = null;
    public static void main(String[] args) throws Exception {
        ZookeeperController zookeeperController = new ZookeeperController();
        ZooKeeper zk = zookeeperController.getZookeeper();
        //zookeeperController.creat(zk);
        zookeeperController.get(zk);
        //zookeeperController.remove(zk);
        zk.close();
    }

    //建立zookeeper连接
    public ZooKeeper getZookeeper() throws Exception {
            zk = new ZooKeeper("192.168.237.133:2181", 3000, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println(watchedEvent.getType()+"-------------"+watchedEvent.getPath());
                //验证zookeeper监听 2.持续监听
               /* try {
                    zk.getChildren("/",true);
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }*/
            }
        });
        return zk;
    }

    //创建节点
    public void creat(ZooKeeper zk) throws Exception {
        if(zk.exists("/zoo", false) == null)
        {
            zk.create("/zoo", "Zookeeper".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }
    }

    //查询子节点及子节点数据
    public void get(ZooKeeper zk) throws Exception {
        List<String> dataList = zk.getChildren("/",true);
        for (String data : dataList) {
            System.out.println("data:"+data);
        }
        //获取节点数据
        System.out.println(".................."+new String(zk.getData("/zoo", false, null)));
        //验证zookeeper监听:1.使线程一直启动
        //Thread.sleep(Long.MAX_VALUE);
    }

    //删除子节点
    public void remove(ZooKeeper zk) throws Exception {
        //参数2指定要删除的版本,-1表示删除所有版本
        zk.delete("/test",-1);
    }

    //关闭zookeeper
    public void close(ZooKeeper zk) throws Exception {
        zk.close();
    }

}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!