• 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
    
    • 1

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

    %ZOOKEEPER_HOME%\bin
    
    • 1

    在这里插入图片描述

    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
      
      • 1

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

    mkdir zookeeper
    ls
    
    • 1
    • 2

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

    docker run -d -e TZ="Asia/Shanghai" -p 2181:2181 -v /mydata/zookeeper:/data --name zookeeper --restart always zookeeper
    
    • 1

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

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

    在这里插入图片描述
    出现如上页面即表示zookeeper启动成功

    2.3 本地连接linux zookeeper

    1. 新建SpringBoot项目
    2. 导入pom依赖:
      
      <dependency>
          <groupId>org.apache.zookeepergroupId>
          <artifactId>zookeeperartifactId>
          <version>3.4.6version>
      dependency>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
    3. 创建测试类:
      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);
      
          @Override
          public 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);
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 48
      • 49
      • 50
      • 51
      • 52
      • 53
      • 54
      • 55
      • 56
      • 57
      • 58
      • 59
      • 60
      • 61
      • 62
      • 63
      • 64
      • 65
      • 66
      • 67
      • 68
      • 69
      • 70
      • 71
      • 72
      • 73
      • 74
      • 75
      • 76
      • 77
      • 78
      • 79
      • 80
      • 81
      • 82
      • 83
      • 84
      • 85
      • 86
      • 87
      • 88
      • 89
      • 90
      • 91
      • 92
      • 93
      • 94
      • 95
      • 96
      • 97
      • 98
      • 99
      • 100
      • 101
      • 102
      • 103
      • 104
      • 105
      • 106
      • 107
      • 108
      • 109
      • 110
      • 111
      • 112
      • 113
      • 114
      • 115
      • 116
      • 117
      • 118
      • 119
      • 120
      • 121
      • 122
      • 123
      • 124
      • 125
      • 126
      • 127
      • 128
      • 129
      • 130
      • 131
      • 132
      • 133
      • 134
      • 135
      • 136
      • 137
      • 138
      • 139
      • 140
    4. 测试结果:
      在这里插入图片描述
      ​ 出现如上页面即表示连接成功
      ​ 项目仓库代码:https://github.com/liuhuanhuan963019/ZooKeeper.git
  • 相关阅读:
    【云原生之k8s】kubernetes原理
    宝塔面板操作日志是存放在哪里的? 如何删除部分日志记录?
    【汇编语言02】第2章 寄存器——理论知识
    联想G50笔记本直接使用F键功能(F1~F12)需要在BIOS设置关闭热键功能可以这样操作!
    xml编写补间动画 PopupWindow实现出现退出的动画
    CADD课程学习(2)-- 靶点晶体结构信息
    Spring Boot 生成二维码
    Nginx面试常问题&工作原理揭秘
    2023秋招—大数据开发面经—多益网络
    《嵌入式 – GD32开发实战指南》第20章 GD32的存储结构
  • 原文地址:https://blog.csdn.net/qq_38140292/article/details/132601546