0


Windows和Linux环境中安装Zookeeper具体操作

1.Windows环境中安装Zookeeper

1.1 下载Zookeeper安装包

ZooKeeper官网下载地址
建议下载稳定版本的
在这里插入图片描述
下载后进行解压后得到如下文件:在这里插入图片描述

1.2 修改本地配置文件

进入解压后的目录,将

zoo_example.cfg

复制一份并重命名为

zoo.cfg

,如图所示:

打开

zoo.cfg

文件,找到

dataDir

,修改数据存放路径,此路径为本地自定义路径。

新增

dataLogDir

,添加zookeeper日志保存地址。在这里插入图片描述
在此配置中也可进行端口号修改,默认使用的是

2181

端口,但是一般使用的就是默认的配置文件,不需要进行更改。

1.3 环境变量配置

新增系统环境变量:在这里插入图片描述

ZOOKEEPER_HOME=D:\software\apache-zookeeper-3.8.2

然后在系统变量Path中新增如下命令参数:

%ZOOKEEPER_HOME%\bin

在这里插入图片描述

1.4 运行ZooKeeper

由于我们已经设置了环境变量,我们只需要在cmd输入

zkserver

就能成功运行ZooKeeper,具体如下图所示:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.Linux/Max环境中安装Zookeeper

2.1 ZooKeeper介绍

​ ZooKeeper是一个分布式的协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:

  • 配置维护
  • 域名服务
  • 分布式同步
  • 组服务等。 ​ 在大型企业级项目开发中,服务的数量十分庞大。此时,如果想要添加一个服务的话,就需要对文件进行重新覆盖,对整个容器进行重启。这样做的一个弊端就是涉及的组件相当大,维护什么困难。 ​ 那么需要一个能够动态注册服务和获取服务信息的组件来统一管理服务,这就是我们常说的服务配置中心。而zookeeper不仅能够对consumer和provider进行管理,并且还内置了负载均衡、主动通知等功能,能够帮助我们很好地解决分布式相关的问题。

2.2 ZooKeeper安装

现在linux主要采用Docker进行环境安装,方便又快捷,Docker的安装和使用请参考作者的这篇博客。
Docker最新超详细版教程通俗易懂(基础版)

  1. 拉取镜像docker pull zookeeper

在这里插入图片描述
2. 创建目录来进行ZooKeeper目录文件的挂载

mkdir zookeeper
ls

在这里插入图片描述
3. docker启动容器
设置端口映射、目录挂载、开机自启等命令设置

docker run -d-eTZ="Asia/Shanghai"-p2181:2181 -v /mydata/zookeeper:/data --name zookeeper --restart always zookeeper

在这里插入图片描述
参数说明:

  • -e TZ=“Asia/Shanghai” :指定时区为上海
  • -d :后台运行
  • -p 2181:2181 : 端口映射,本地2181端口映射到容器内部的2181端口
  • -name : 设置容器的名称
  • -v :指定挂载的目录
  • -restart always :始终重新启动zookeeper
  1. 查看进程是否正常启动docker exec -it zookeeper /bin/bash

在这里插入图片描述
出现如上页面即表示

zookeeper

启动成功

2.3 本地连接linux zookeeper

  1. 新建SpringBoot项目
  2. 导入pom依赖:<!--zookeeper连接包--><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.4.6</version></dependency>
  3. 创建测试类:packageorg.example;importorg.apache.zookeeper.*;importjava.util.List;importjava.util.concurrent.CountDownLatch;importorg.apache.zookeeper.CreateMode;importorg.apache.zookeeper.KeeperException;importorg.apache.zookeeper.WatchedEvent;importorg.apache.zookeeper.Watcher;importorg.apache.zookeeper.Watcher.Event.KeeperState;importorg.apache.zookeeper.ZooDefs.Ids;importorg.apache.zookeeper.ZooKeeper;importorg.apache.zookeeper.data.Stat;publicclassBaseZooKeeperimplementsWatcher{privatestaticZooKeeper zooKeeper;// 超时时间privatestaticfinalintSESSION_TIME_OUT=1000;privateCountDownLatch countDownLatch =newCountDownLatch(1);@Overridepublicvoidprocess(WatchedEvent watchedEvent){if(watchedEvent.getState()==Event.KeeperState.SyncConnected){System.out.println("Watch received event"); countDownLatch.countDown();}}/**连接zookeeper * @param host * @throws Exception */publicvoidconnectZookeeper(String host)throwsException{ zooKeeper =newZooKeeper(host,SESSION_TIME_OUT,this); countDownLatch.await();System.out.println("zookeeper connection success");}/** * 创建节点 * @param path * @param data * @throws Exception */publicStringcreateNode(String path,String data)throwsException{returnthis.zooKeeper.create(path, data.getBytes(),ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);}/** * 获取路径下所有子节点 * @param path * @return * @throws KeeperException * @throws InterruptedException */publicList<String>getChildren(String path)throwsKeeperException,InterruptedException{List<String> children = zooKeeper.getChildren(path,false);return children;}/** * 获取节点上面的数据 * @param path 路径 * @return * @throws KeeperException * @throws InterruptedException */publicStringgetData(String path)throwsKeeperException,InterruptedException{byte[] data = zooKeeper.getData(path,false,null);if(data ==null){return"";}returnnewString(data);}/** * 设置节点信息 * @param path 路径 * @param data 数据 * @return * @throws KeeperException * @throws InterruptedException */publicStatsetData(String path,String data)throwsKeeperException,InterruptedException{Stat stat = zooKeeper.setData(path, data.getBytes(),-1);return stat;}/** * 删除节点 * @param path * @throws InterruptedException * @throws KeeperException */publicvoiddeleteNode(String path)throwsInterruptedException,KeeperException{ zooKeeper.delete(path,-1);}/** * 获取创建时间 * @param path * @return * @throws KeeperException * @throws InterruptedException */publicStringgetCTime(String path)throwsKeeperException,InterruptedException{Stat stat = zooKeeper.exists(path,false);returnString.valueOf(stat.getCtime());}/** * 获取某个路径下孩子的数量 * @param path * @return * @throws KeeperException * @throws InterruptedException */publicIntegergetChildrenNum(String path)throwsKeeperException,InterruptedException{int childenNum = zooKeeper.getChildren(path,false).size();return childenNum;}/** * 关闭连接 * @throws InterruptedException */publicvoidcloseConnection()throwsInterruptedException{if(zooKeeper !=null){ zooKeeper.close();}}publicstaticvoidmain(String[] args)throwsException{BaseZooKeeper zookeeper =newBaseZooKeeper(); zookeeper.connectZookeeper("139.196.74.203:2181");//改端口List<String> children = zookeeper.getChildren("/");System.out.println(children);}}
  4. 测试结果:在这里插入图片描述 ​ 出现如上页面即表示连接成功 ​ 项目仓库代码:https://github.com/liuhuanhuan963019/ZooKeeper.git
标签: zookeeper

本文转载自: https://blog.csdn.net/qq_38140292/article/details/132601546
版权归原作者 指尖听戏 所有, 如有侵权,请联系我们删除。

“Windows和Linux环境中安装Zookeeper具体操作”的评论:

还没有评论