1. 安装条件

Linux下安装Zookeeper,必须先在Linux中安装好jdk。安装步骤参见:Linux安装Jdk

2. 下载并安装Zookeeper

在官网下载Zookeeper的压缩包,在Linux下解压tar -zxvf apache-zookeeper-3.5.10-bin.tar.gz

3. 编辑配置文件

  1. 进入Zookeeper的config目录

cd apache-zookeeper-3.5.10-bin/config

  1. 将zoo_sample.cfg复制并重命名为zoo.cfg(必须是这个名字)

cp zoo_sample.cfg zoo.cfg

  1. 编辑zoo.cfg文件,添加快照存放目录和日志存放目录(自己创建固定存放目录) ```shell vim zoo.cfg

dataDir=/root/data/zookeeper/apache-zookeeper-3.5.10-bin/snapshot dataLogDir=/root/data/zookeeper/apache-zookeeper-3.5.10-bin/snapshot/log

  1. <a name="SpM3v"></a>
  2. #### 4. 配置环境变量
  3. 将Zookeeper的相关信息配置到/etc/profile
  4. ```shell
  5. #zookeeper conf
  6. export ZOOKEEPER_INSTALL=/root/data/zookeeper/apache-zookeeper-3.5.10-bin
  7. export PATH=${PATH}:$ZOOKEEPER_INSTALL/bin

5. 测试启动

进入到Zookeeper安装路径下的bin目录,启动Zookeeper服务和客户端

  1. 启动Zookeeper服务 ./zkServer.sh start
  2. 启动Zookeeper客户端 ./zkCli.sh
  3. 启动成功效果如下: ```shell Connecting to localhost:2181 ……. ……. Welcome to ZooKeeper! …… 2022-06-15 20:12:14,807 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1415] - Session establishment complete on server localhost/0:0:0:0:0:0:0:1:2181, sessionid = 0x1000201d2dc0005, negotiated timeout = 30000

WATCHER:: WatchedEvent state:SyncConnected type:None path:null

  1. <a name="t8I5E"></a>
  2. #### 6. Zookeeper使用
  3. 1. ls 可以查看子节点
  4. ```shell
  5. [zk: localhost:2181(CONNECTED) 1] ls /
  6. [services, zookeeper]
  1. get 获得子节点信息 ```shell [zk: localhost:2181(CONNECTED) 3] get /services/cloud-provider-payment/32a071d8-3a40-4f7f-ba00-03a0e23449e8 {“name”:”cloud-provider-payment”,”id”:”32a071d8-3a40-4f7f-ba00-03a0e23449e8”,”address”:”DESKTOP-FDCTOPU.mshome.net”,”port”:8004,”sslPort”:null,”payload”:{“@class”:”org.springframework.cloud.zookeeper.discovery.ZookeeperInstance”,”id”:”application-1”,”name”:”cloud-provider-payment”,”metadata”:{}},”registrationTimeUTC”:1655293925420,”serviceType”:”DYNAMIC”,”uriSpec”:{“parts”:[{“value”:”scheme”,”variable”:true},{“value”:”://“,”variable”:false},{“value”:”address”,”variable”:true},{“value”:”:”,”variable”:false},{“value”:”port”,”variable”:true}]}}

```