一.安装ZooKeeper
1./opt下新建zookeeper文件夹,将安装包上传到文件夹内
2.解压
进入安装包目录
cd /opt/ZooKeeper
解压:
tar -zxvf apache-zookeeper-3.5.7-bin.tar.gz
3.重命名为 zookeeper-3.5.7
mv apache-zookeeper-3.5.7-bin zookeeper-3.5.7
4.创建日志文件和数据文件
5.进入conf,将zoo_sample.cfg改名为zoo.cfg
6.配置zoo.cfg
主要是 dataDir = 位置
7.启动测试
首先进入以下目录(即解压的zookeeper-3.5.7下的bin目录):
/opt/zookeeper/zookeeper-3.5.7/bin
Linux命令启动:
./zkServer.sh start
查看:
jps
二.进入 zookeeper 命令行操作
1.进入命令行(在/opt/zookeeper/zookeeper-3.5.7/bin目录下,不用退出。)
./zkCli.sh
运行完成,回车,即可输入命令
2.显示所有操作:
help
3.查看当前节点中包含的内容:
ls /
4.查看当前节点数据:
ls2 /
4.创建普通节点app1 数据值为 helloapp1:
create /app1 helloapp1
5.获取当前节点数据值:
get /app1
6.创建短暂节点app-emphemeral 数据值 8888:
create -e /app-emphemeral 8888
7.创建带序号节点:
create /app2 "app2"
8.修改节点数据值(将app1的数据值修改为999)
set /app1 999
9.监听节点:
get /app1 watch
10.删除节点:
delete /app1
11.查看节点状态:
stat /app2
三.连接IDEA 使用代码操作
安装Maven 看这里——>这里不在赘述
1.新建项目,配置Maven:
直接选择Maven---->next
配置本工程的Maven
2.导入依赖
<dependencies>
<!-- zookeeper-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<!-- 测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- 日志-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
</dependencies>
3.在项目的 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
点击刷新一下依赖:
4.创建 包和java类
5.创建 创建zk连接
package com.gingtaishuji.zrt;
import org.apache.zookeeper.*;
import org.junit.Test;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
public class FirstTest {
@Test
public void test() throws Exception {
//1.创建zk连接,端口一般是2181,前面的ip是自己的,这里是主机名映射
ZooKeeper zooKeeper = new ZooKeeper("hy:2181", 200000, new Watcher() {
public void process(WatchedEvent watchedEvent) {
System.out.println("收到监听:type:" + watchedEvent.getType() + ",路径:"
+ watchedEvent.getPath() + ",状态:" + watchedEvent.getState());
}
});
}
}
6.创建 父节点
代码接上:
//2.创建父节点 zrt
zooKeeper.create("/yyy",
"nodeValue".getBytes(),
ZooDefs.Ids.READ_ACL_UNSAFE,
CreateMode.PERSISTENT
);
Linux查看:
ls /
7.创建 子节点(应该需要给节点设置权限,否则没权限操作)
代码接上:
//3.创建子节点 zrt
String sonPath = zooKeeper.create("/zrt/son",
"sonNodeValue".getBytes(),
ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT
);
8.获取父节点中的值
代码接上:
//4.获取节点中的值
byte[] value = zooKeeper.getData("/zrt",false,null);
//打印
System.out.println("子节点:"+new String(value)
);
9.获取子节点中的值
//5.获取子节点中的值
List<String> children = zooKeeper.getChildren("/zrt",false);
for (String child : children){
System.out.println("子节点:"+child);
}
10.判断节点是否存在
//6.判断节点是否存在
Stat exists = zooKeeper.exists("/zrt/son",false);
System.out.println(exists);
11.删除节点
//7.删除节点
zooKeeper.delete("/hhh",-1);