前置知识:Zookeeper学习笔记(1)—— 基础知识-CSDN博客
Zookeeper集群搭建部分
前提:保证zookeeper集群处于启动状态
环境搭建
依赖配置
<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><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.5.7</version></dependency></dependencies>
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
参数具体含义:
log4j.rootLogger=INFO, stdout:设置根记录器的级别为INFO,并指定将日志输出到控制台(stdout)
log4j.appender.stdout=org.apache.log4j.ConsoleAppender:创建一个名为stdout的日志输出器,使用org.apache.log4j包中的ConsoleAppender类
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout:为stdout输出器设置布局,使用org.apache.log4j包中的PatternLayout类。
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n:定义了日志消息的格式,其中:
- %d:表示日期和时间;
- %p:表示日志级别;
- [%c]:表示日志来源的类名;
- %m:表示实际的日志消息;
- %n:表示换行符。
log4j.appender.logfile=org.apache.log4j.FileAppender:创建一个名为logfile的日志输出器,使用org.apache.log4j包中的FileAppender类。
log4j.appender.logfile.File=target/spring.log:指定logfile输出器将日志写入到名为"target/spring.log"的文件中。
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout:为logfile输出器设置布局,使用org.apache.log4j包中的PatternLayout类。
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n:定义了日志消息的格式,与stdout输出器相同。
打包插件配置
<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-shade-plugin</artifactId><version>3.2.4</version><executions><execution><phase>package</phase><goals><goal>shade</goal></goals></execution></executions></plugin></plugins>
</build>
客户端操作
整体使用junit进行测试
package com.why.zk;import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.Before;
import org.junit.Test;import java.io.IOException;
import java.util.List;public class zkClient {private static String connetString = "hadoop102:2181,hadoop103:2181,hadoop104:2181"; //客户端连接ipprivate static int sessionTimeout = 2000; //超时时间private ZooKeeper zkClient = null; //客户端对象@Beforepublic void init() throws IOException {zkClient = new ZooKeeper(connetString, sessionTimeout, new Watcher() {@Overridepublic void process(WatchedEvent watchedEvent) {//收到事件通知后的回调函数System.out.println("事件类型:" + watchedEvent.getType());System.out.println("事件路径:" + watchedEvent.getPath());//再次启动对子节点的监听// try {// List<String> children = zkClient.getChildren("/", true);// for(String child : children)// {// System.out.println(child);// }// } catch (InterruptedException | KeeperException e) {// e.printStackTrace();// }}});}//创建节点@Testpublic void create(){try {String nodeCreated = zkClient.create("/bigdata/test5","test5".getBytes(),ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);System.out.println(nodeCreated);} catch (InterruptedException | KeeperException e) {e.printStackTrace();}}//获取子节点并监听节点变化@Testpublic void getChildren() throws InterruptedException, KeeperException {List<String> children = zkClient.getChildren("/", true);for(String child : children){System.out.println(child);}// 延时阻塞(此时在shell中创建子节点,可以在控制台监听到子节点的变化Thread.sleep(Long.MAX_VALUE);}//判断节点是否存在@Testpublic void exist() throws InterruptedException, KeeperException {Stat stat = zkClient.exists("/bigdata", false);System.out.println(stat == null ? "not exist" : "exist");}}