Zookeeper从设计模式角度来理解,是一个基于观察者模式设计的分布式服务管理框架,它负责存储和管理大家都关心的数据,然后接受观察者的注册,一旦这些数据的状态发生了变化,Zookeeper就负责通知已经在Zookeeper上注册的那些观察者做出相应的反应.
1.2、Zookeeper特点及理解 1.3、Zookeeper 数据结构详解 1.4、Zookeeper应用场景- 面试题:4个zookeeper的应用场景,你知道几个?
- Zookeeper及其应用场景
- ZooKeeper 的应用场景
# 集群启动:hadoop102、hadoop103、hadoop104 [atguigu@hadoop102 zookeeper-3.5.7]$ zkServer.sh start # 查看状态:hadoop102、hadoop103、hadoop104 [atguigu@hadoop102 zookeeper-3.5.7]$ bin/zkServer.sh status # 启动客户端 [atguigu@hadoop103 zookeeper-3.5.7]$ bin/zkCli.sh # 查看命令 [zk: localhost:2181(CONNECTED) 2] help ZooKeeper -server host:port cmd args addauth scheme auth close config [-c] [-w] [-s] connect host:port create [-s] [-e] [-c] [-t ttl] path [data] [acl] delete [-v version] path deleteall path delquota [-n|-b] path get [-s] [-w] path getAcl [-s] path history listquota path ls [-s] [-w] [-R] path ls2 path [watch] printwatches on|off quit reconfig [-s] [-v version] [[-file path] | [-members serverID=host:port1:port2;port3[,...]*]] | [-add serverId=host:port1:port2;port3[,...]]* [-remove serverId[,...]*] redo cmdno removewatches path [-c|-d|-a] [-l] rmr path set [-s] [-v version] path data setAcl [-s] [-v version] [-R] path acl setquota -n|-b val path stat [-w] path sync path Command not found: Command not found help2.2、API应用
添加pom文件
junit junit RELEASE org.apache.logging.log4j log4j-core 2.8.2 org.apache.zookeeper zookeeper 3.5.7
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
JAVA实例程序
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.List;
public class TestZkClient {
private ZooKeeper zk;
@Before
public void initZkClient() throws IOException {
// 连接ZK集群的地址
String connectionStr = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
// 超时时间
int sessionTimeOut = 10000;
zk = new ZooKeeper(connectionStr, sessionTimeOut, new Watcher() {
public void process(WatchedEvent event) {
// System.out.println("监听到有变化");
}
});
}
@After
public void closeZk() throws InterruptedException {
zk.close();
}
@Test
public void ls() throws IOException, KeeperException, InterruptedException {
//用客户端对象做各种操作
List children = zk.getChildren("/", false);
System.out.println(children);
}
JAVA运行输出
2021-12-30 22:45:25,769 INFO [org.apache.zookeeper.ClientCnxn] - Opening socket connection to server hadoop103/192.168.2.35:2181. Will not attempt to authenticate using SASL (unknown error) 2021-12-30 22:45:25,772 INFO [org.apache.zookeeper.ClientCnxn] - Socket connection established, initiating session, client: /192.168.2.9:56683, server: hadoop103/192.168.2.35:2181 2021-12-30 22:45:25,810 INFO [org.apache.zookeeper.ClientCnxn] - Session establishment complete on server hadoop103/192.168.2.35:2181, sessionid = 0x300071438870000, negotiated timeout = 10000 [zookeeper, sanguo] 2021-12-30 22:45:25,948 INFO [org.apache.zookeeper.ZooKeeper] - Session: 0x300071438870000 closed 2021-12-30 22:45:25,948 INFO [org.apache.zookeeper.ClientCnxn] - EventThread shut down for session: 0x300071438870000



