tar -xf apache-zookeeper-3.5.7-bin.tar.gz -C /opt/module/
sudo vim /etc/profile.d/my_env.sh
export ZOOKEEPER_HOME=/opt/module/zookeeper-3.4.7
export PATH=$PATH:$ZOOKEEPER_HOME/bin
source /etc/profile
修改持久化数据存储目录。
cp $ZOOKEEPER_HOME/conf/zoo_sample.cfg $ZOOKEEPER_HOME/conf/zoo.cfg
vim zoo.cfg
将目录修改为自己要存放zookeeper数据目录(请与博主保持一致)
在配置文件末尾添加
server.32=hadoop32:2888:3888
server.33=hadoop33:2888:3888
server.34=hadoop34:2888:3888
32,33,34代表myid,集群模式下配置一个文件myid,这个文件在dataDir目录下,这个文件里面有一个数据就是server.后的值,Zookeeper启动时读取此文件,拿到里面的数据与zoo.cfg里面的配置信息比较从而判断到底是哪个server。
等号后面是服务器ip,我们加了映射,不懂的请看博主大数据第一篇博客;2888服务器之间交换信息的端口号(是follower与leader的通信端口);3888是执行选举时通信的端口。
创建目录,并创建子目录myid
[yuaf@hadoop32 conf]$ cd ..
[yuaf@hadoop32 zookeeper-3.4.7]$ mkdir zkData
[yuaf@hadoop32 zookeeper-3.4.7]$ cd zkData/
[yuaf@hadoop32 zkData]$ touch myid
[yuaf@hadoop32 zkData]$ chmod +x myid
[yuaf@hadoop32 zkData]$ vim myid
xsync /opt/module/zookeeper-3.4.7/
然后修改myid的值与之设置的服务器编号一致
修改hadoop33 zookeeper 的 myid
修改hadoop34 zookeeper 的 myid
sudo xsync /etc/profile.d/my_env.sh
按以下代码执行。
cd $ZOOKEEPER_HOME/bin
touch zk.sh
chmod +x zk.sh
vim zk.sh
#!/bin/bash
if (( $#==0 )); then
echo No Args!;
exit;
fi
for host in hadoop32 hadoop33 hadoop34
do
echo "------------------------------ZooKeeper in $host----------------------------"
ssh $host "zkServer.sh $1" 2> /dev/null
done
测试一下:
在根目录下输入:
zk.sh start
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-coreartifactId>
<version>2.8.2version>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>3.5.7version>
dependency>
dependencies>
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
package com.yuaf.zookeepert;
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 ZKApiTest {
//2181zookeeper的客户端端口号
private static String connectString =
"hadoop102:2181,hadoop103:2181,hadoop104:2181";
//会话超时时间
private static int sessionTimeout = 2000;
private ZooKeeper zkClient = null;
@Before
public void before() throws IOException {
zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent event) {
// 收到事件通知后的回调函数(用户的业务逻辑)
System.out.println(event.getType() + "--" + event.getPath());
// 再次启动监听
try {
zkClient.getChildren("/", true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
@After
public void after() throws InterruptedException {
zkClient.close();
}
//创建子节点
@Test
public void create() throws KeeperException, InterruptedException {
zkClient.create("/abddc",
"song".getBytes(),
ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
}
//获取子节点并监听节点变化
@Test
public void getChildren() throws KeeperException, InterruptedException {
List<String> childen = zkClient.getChildren("/", true);
childen.forEach(System.out::println);
Thread.sleep(Long.MAX_VALUE);
}
//判断节点是否存在
@Test
public void exist() throws KeeperException, InterruptedException {
Stat stat = zkClient.exists("/ooo", true);
System.out.println(stat == null ? "not exist" : "exist");
}
}