1.安装前准备
(1)安装Jdk
(2)在官网http://zookeeper.apache.org/下载zookeeper.
(3)解压到指定目录(解压zookeeper-3.4.10至D:\Dev\zookeeper-3.4.10)
(4)新建data及log目录。
2.配置修改
(1)将D:\Dev\zookeeper-3.4.10/conf这个路径下的zoo_sample.cfg修改为zoo.cfg;
(2)打开zoo.cfg文件,修改dataDir路径:
修改如下内容:
dataDir=D:\\Config\\zookeeper\\data dataLogDir=D:\\Config\\zookeeper\\log
3.操作Zookeeper
(1)启动Zookeeper
cmd命令下进入D:\Dev\zookeeper-3.4.10\bin目录下运行zkserver.cmd,或直接双击
(2)查看进程是否启动
进入cmd下进bin目录,输入:zkCli.cmd 127.0.0.1:2181
1.创建一个Maven工程
2.添加pom文件
junit
junit
RELEASE
org.apache.logging.log4j
log4j-core
2.8.2
org.apache.zookeeper
zookeeper
3.4.10
3.拷贝log4j.properties文件到项目根目录
需要在项目的src/main/resources目录下,新建一个文件,命名为“log4j.properties”,在文件中填入。
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
@Before
public void init() throws IOException{
//创建zkClient客户端
zkClient = new ZooKeeper(connectString, sessionTimeout , new Watcher() {
public void process(WatchedEvent event) {
logger.info("监听开始。。。。");
List children;
try {
children = zkClient.getChildren("/", true);
logger.info("------------------监听start-------------------");
for (String child : children) {
logger.info("监听---子节点:{}",child);
logger.info("监听---读取内容:{}",new String(zkClient.getData("/"+child,true,stat)));
logger.info("监听---读取Stat:czxid=" + stat.getCzxid() + ";mzxid=" + stat.getMzxid() + ";version=" + stat.getVersion());
}
logger.info("------------------监听end-------------------");
} catch (KeeperException e) {
logger.error(e.getMessage());
} catch (InterruptedException e) {
logger.error(e.getMessage());
}
}
});
}
@Test
public void createNode() throws KeeperException, InterruptedException{
String nodeCreate = zkClient.create(
"/path-zf", //参数1:要创建的节点的路径;
"data:{姓名:Tane,年龄:25}".getBytes(), //参数2:节点数据 ;
Ids.OPEN_ACL_UNSAFE, //参数3:节点权限 ;
CreateMode.PERSISTENT //参数4:节点的类型(ephemeral:短暂;Persistent:持久)
);
logger.info("createNode---nodeCreate:{}",nodeCreate);
}
@Test
public void getChildren() throws KeeperException, InterruptedException {
List childNodes = zkClient.getChildren("/", true);
logger.info("getChildren---childNodes:{}",childNodes);
for (String childNode : childNodes) {
logger.info("getChildren---子节点:{}",childNode);
//new String(zkClient.getData("/"+childNode,true,stat));
logger.info("getData---读取内容:{}",new String(zkClient.getData("/"+childNode,true,stat)));
//logger.info("getChildren--读取Stat:czxid=" + stat.getCzxid() + ";mzxid=" + stat.getMzxid() + ";version=" + stat.getVersion());
}
Thread.sleep(Long.MAX_VALUE);//线程等待,监控节点数据变化
}
@Test
public void watcher() throws Exception {
String childNode ="/path-zf";
zkClient.setData(childNode,(childNode+"监控节点数据变化").getBytes(),-1);
Thread.sleep(Long.MAX_VALUE);//线程等待,监控节点数据变化
}
@Test
public void exist() throws Exception {
Stat stat = zkClient.exists("/eclipse", false);
System.out.println(stat == null ? "not exist" : "exist");
}
遇到问题:
资源:https://download.csdn.net/download/tane_1018/11185397