https://archive.apache.org/dist/zookeeper/zookeeper-3.4.10/
通过软件FX将安装包上传到linux系统的/export/software/目录下
执行命令tar -zxvf zookeeper-3.4.10.tar.gz -C /export/servers/
将安装包解压到指定文件目录/export/servers
(1)修改Zookeeper解压目录下的conf目录,复制其下zoo_sample.cfg文件并重命名为zoo_cfg,命令如下:
cp zoo_sample.cfg zoo.cfg
(2).修改配置文件zoo.cfg,设置dataDir目录,配置服务器编号与主机映射关系,设置与主机连接的心跳端口和选举端口 ,内容如下:
# The number of milliseconds of each tick
#设置通信心跳数
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
#设置初始通信时限
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
#设置数据文件目录+数据持久化路径/
dataDir=/export/data/zookeeper/zkdata
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
#配置zookeeper集群的服务器编号以及对应的主机名、通讯端口号(心跳端口号)和选举端口号
server.1=yangjian00:2888:3888
server.2=yangjian01:2888:3888
server.3=yangjian02:2888:3888
server.4=yangjian03:2888:3888
PS: server.1=yangjian00:2888:3888中,1代表服务器编号,yangjian00表示服务器IP地址,2888表示通讯端口号,3888表示leader选举端口号
(1).首先据文件zoo.cfg中设置的dataDir目录,创建zkdata文件夹
mkdir -p /export/data/zookeeper/zkdata
(2).在zkdata文件夹下创建myid文件,该文件内容为服务器编号
cd /export/data/zookeeper/zkdata
echo 1>myid
(1) 执行命令vi /etc/profile对profile文件进行修改,添加Zookeeper的环境变量
export ZK_HOME=/export/servers/zookeeper-3.4.10
export PATH=$PATH:$JAVA_HOME/bin:$HADOOP_HOME/bin:$HADOOP_HOME/sbin:$ZK_HOME/bin
(1)首先将Zookeeper安装目录分发到yangjian01 yangjian02 yangjian03服务器上
scp -r /export/servers/zookeeper-3.4.10/ yangjian01:/export/servers/
scp -r /export/servers/zookeeper-3.4.10/ yangjian02:/export/servers/
scp -r /export/servers/zookeeper-3.4.10/ yangjian03:/export/servers/
(2)将myid文件分发到yangjian01 yangjian02 yangjian03服务器,并修改myid文件依次对服务器号进行设置
scp -r /export/data/zookeeper/ yangjian01:/export/data
scp -r /export/data/zookeeper/ yangjian02:/export/data
scp -r /export/data/zookeeper/ yangjian03:/export/data
(3)将profile文件也分发到yangjian01 yangjian02 yangjian03服务器
scp /etc/profile yangjian01:/etc/profile
scp /etc/profile yangjian02:/etc/profile
scp /etc/profile yangjian03:/etc/profile
分别在yangjian01/02/03服务器下刷新profile文件,使环境变量生效
source /etc/profile
依次在yangjian00/01/02/03服务器上启动zookeeper服务
zkServer.sh start
zkServer.sh status
zkServer.sh stop
zkCli.sh -server localhost:2181
#查看当前Zookeeper中所含的内容
ls /
#查看当前节点数据
ls -s /
4.创建节点:
#创建序列化永久节点:
create -s /testnode test
#创建临时节点
create -e /testnode-temp testtemp
#创建永久节点:
create /testnode-p testp
ls path [watch]
get path [watch]
ls -s path [watch]
ls -s /
get -s /testnode-temp
set /testnode-temp 123
get -s /testnode-temp
PS:data表示修改的新内容,version表示数据版本。从而得知,testtemp
变为123、
get /testnode-temp watch
set /testnode-temp testwatch
#他会回调Watch得到触发结果
#普通删除的命令
delete path [version]
#递归删除的命令
rmr path [version]
所有代码如下:
4.0.0
org.example
Zookeeper
1.0-SNAPSHOT
org.apache.zookeeper
zookeeper
3.4.10
8
8
UTF-8
依赖如下
代码如下
log4j.rootLogger=INFO, stdout, logfile
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/inverted index.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
import org.apache.zookeeper.*;
public class Zookpeeper {
public static void main(String[] args) throws Exception{
//创建zookeeper客户端
ZooKeeper zk=new ZooKeeper("yangjian00:2181,yangjian01:2181,yangjian02:2181", 30000, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
System.out.println("事件类型为:"+ watchedEvent.getType());
System.out.println("事件发生的路径:"+ watchedEvent.getPath());
System.out.println("通知状态为:"+ watchedEvent.getState());
}
});
//创建目录节点
zk.create("/testRootPath","testRootData".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
//创建子节点目录
zk.create("/testRootPath/testChildPathOne","testChildDataOne".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
//获取目录节点数据
System.out.println("testRootData 节点数据为:"+new String(zk.getData("/testRootPath",false,null)));
//获取子目录节点数据
System.out.println(zk.getChildren("/testRootPath",true));
//修改子目录节点数据,使得监听触发
zk.setData("/testRootPath/testChildPathOne","modifyChildDataOne".getBytes(),-1);
//判断目录节点是否存在
System.out.println("目录节点状态:["+zk.exists("/testRootPath",true)+"]");
//删除子目录节点
zk.delete("/testRootPath/testChildPathOne",-1);
//删除目录节点
zk.delete("/testRootPath",-1);
zk.close();
}
}