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最新超详细版教程通俗易懂(基础版)
- 拉取镜像
docker pull zookeeper
2. 创建目录来进行ZooKeeper目录文件的挂载
mkdir zookeeper
ls
3. docker启动容器
设置端口映射、目录挂载、开机自启等命令设置
docker run -d -e TZ="Asia/Shanghai" -p 2181: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
- 查看进程是否正常启动
docker exec -it zookeeper /bin/bash
出现如上页面即表示zookeeper
启动成功
2.3 本地连接linux zookeeper
- 新建SpringBoot项目
- 导入pom依赖:
<!--zookeeper连接包--> <dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.4.6</version> </dependency>
- 创建测试类:
package org.example;import org.apache.zookeeper.*;import java.util.List; import java.util.concurrent.CountDownLatch; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.Watcher.Event.KeeperState; import org.apache.zookeeper.ZooDefs.Ids; import org.apache.zookeeper.ZooKeeper; import org.apache.zookeeper.data.Stat;public class BaseZooKeeper implements Watcher {private static ZooKeeper zooKeeper;// 超时时间private static final int SESSION_TIME_OUT = 1000;private CountDownLatch countDownLatch = new CountDownLatch(1);@Overridepublic void process(WatchedEvent watchedEvent) {if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {System.out.println("Watch received event");countDownLatch.countDown();}}/**连接zookeeper* @param host* @throws Exception*/public void connectZookeeper(String host) throws Exception{zooKeeper = new ZooKeeper(host, SESSION_TIME_OUT, this);countDownLatch.await();System.out.println("zookeeper connection success");}/*** 创建节点* @param path* @param data* @throws Exception*/public String createNode(String path,String data) throws Exception{return this.zooKeeper.create(path, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);}/*** 获取路径下所有子节点* @param path* @return* @throws KeeperException* @throws InterruptedException*/public List<String> getChildren(String path) throws KeeperException, InterruptedException{List<String> children = zooKeeper.getChildren(path, false);return children;}/*** 获取节点上面的数据* @param path 路径* @return* @throws KeeperException* @throws InterruptedException*/public String getData(String path) throws KeeperException, InterruptedException{byte[] data = zooKeeper.getData(path, false, null);if (data == null) {return "";}return new String(data);}/*** 设置节点信息* @param path 路径* @param data 数据* @return* @throws KeeperException* @throws InterruptedException*/public Stat setData(String path,String data) throws KeeperException, InterruptedException{Stat stat = zooKeeper.setData(path, data.getBytes(), -1);return stat;}/*** 删除节点* @param path* @throws InterruptedException* @throws KeeperException*/public void deleteNode(String path) throws InterruptedException, KeeperException{zooKeeper.delete(path, -1);}/*** 获取创建时间* @param path* @return* @throws KeeperException* @throws InterruptedException*/public String getCTime(String path) throws KeeperException, InterruptedException{Stat stat = zooKeeper.exists(path, false);return String.valueOf(stat.getCtime());}/*** 获取某个路径下孩子的数量* @param path* @return* @throws KeeperException* @throws InterruptedException*/public Integer getChildrenNum(String path) throws KeeperException, InterruptedException{int childenNum = zooKeeper.getChildren(path, false).size();return childenNum;}/*** 关闭连接* @throws InterruptedException*/public void closeConnection() throws InterruptedException{if (zooKeeper != null) {zooKeeper.close();}}public static void main(String[] args) throws Exception {BaseZooKeeper zookeeper = new BaseZooKeeper();zookeeper.connectZookeeper("139.196.74.203:2181"); //改端口List<String> children = zookeeper.getChildren("/");System.out.println(children);} }
- 测试结果:
出现如上页面即表示连接成功
项目仓库代码:https://github.com/liuhuanhuan963019/ZooKeeper.git