4.3.1 idea环境搭建
1.创建一个Maven工程
2.添加pom文件
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.8.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.10</version> </dependency> </dependencies> |
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
4.3.2 创建ZooKeeper客户端
package com.dukun.study;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringZkStudyApplicationTests {
private static String connectString = "127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183";
private static int sessionTimeout = 2000;
private ZooKeeper zkClient = null;
/**
* 创建zk连接客户端
* @throws Exception
*/
@Test
public void init() throws Exception{
//创建zk连接客户端
zkClient=new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
}
});
}
}
4.3.3 创建子节点
/**
* 创建节点
* @throws Exception
*/
@Test
public void create() throws Exception {
// 参数1:要创建的节点的路径; 参数2:节点数据 ; 参数3:节点权限 ;参数4:节点的类型
String nodeCreated = zkClient.create("/dukun","ceshishiyong".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println("创建节点:"+nodeCreated);
}
4.3.4 获取节点中数据
// 获取节点中 数据
@Test
public void getNodeDat() throws Exception {
//要先获取节点相关信息 不加监听 然后在获取数据
Stat stat= zkClient.exists("/dukun",false);
byte[] data =zkClient.getData("/dukun",false,stat);
String dataSt= new String(data);
System.out.println(data);
}
4.3.5 获取子节点并监听节点变化
通过监听获取节点中的变化 process()
new Watcher() { @Override public void process(WatchedEvent watchedEvent) { } }
/**
* 创建zk连接客户端
* @throws Exception
*/
@Before
public void init() throws Exception{
//创建zk连接客户端
zkClient=new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
// 收到事件通知后的回调函数(用户的业务逻辑)
System.out.println(watchedEvent.getType() + "--" + watchedEvent.getPath());
// 再次启动监听
try {
zkClient.getChildren("/", true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
// 获取子节点
@Test
public void getChildren() throws Exception {
//获取节点信息并加监听
List<String> children = zkClient.getChildren("/", true);
for (String child : children) {
System.out.println(child);
}
// 延时阻塞
Thread.sleep(Long.MAX_VALUE);
}