ZooKeeper官网下载地址
建议下载稳定版本的
下载后进行解压后得到如下文件:
进入解压后的目录,将zoo_example.cfg
复制一份并重命名为zoo.cfg
,如图所示:
打开zoo.cfg
文件,找到dataDir
,修改数据存放路径,此路径为本地自定义路径。
新增dataLogDir
,添加zookeeper日志保存地址。
在此配置中也可进行端口号修改,默认使用的是2181
端口,但是一般使用的就是默认的配置文件,不需要进行更改。
新增系统环境变量:
ZOOKEEPER_HOME=D:\software\apache-zookeeper-3.8.2
然后在系统变量Path中新增如下命令参数:
%ZOOKEEPER_HOME%\bin
由于我们已经设置了环境变量,我们只需要在cmd输入zkserver
就能成功运行ZooKeeper,具体如下图所示:
ZooKeeper是一个分布式的协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:
现在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
参数说明:
docker exec -it zookeeper /bin/bash
出现如上页面即表示zookeeper
启动成功
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>3.4.6version>
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);
@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);
}
}