文章目录
jdk搭建
可参考笔者这篇文章
Linux下jdk的下载安装
zookeeper环境搭建
下载
Index of /dist/zookeeper/zookeeper-3.7.0
上传到Linux上
解压
tar -zxvf zookeeper-3.4.11.tar.gz
重命名并移动到usr/local位置下
mv zookeeper-3.7.0 zookeeper
mv zookeeper /usr/local/
配置环境变量
vim /etc/profile
添加如下内容
export JAVA_HOME=/usr/jdk8
export ZOOKEEPER_HOME=/usr/local/zookeeper
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$ZOOKEEPER_HOME/bin:$JAVA_HOME/bin
编译
source /etc/profile
测试
启动zk
./bin/zkServer.sh start
# jps -l #可查看zk是否以及正常启动
./bin/zkServer.sh status # 查看zk状态
退出zk
./bin/zkServer.sh stop
启动zk客户端
注意在进入客户端状态候可在命令行敲入quit
退出
./bin/zkCli.sh
操作节点
查看Zookeeper中包含的key
ls /
创建一个新的Znode 创建成功以后我们可以使用 ls/查看我们创建的内容
create /zkZsy hahahah
ls /
[zkZsy , zookeeper]
get命令获取创建Znode的内容
get /zkZsy
set 命令来对 zk 所关联的字符串进行设置
set /zkZsy zsy666
删除Znode
delete /zkMxn
zk配置详解
# The number of milliseconds of each tick
tickTime=2000 #心跳时间,zk服务器与客户端的心跳时间单位为毫秒
# The number of ticks that the initial
# synchronization phase can take
initLimit=10 # 初始化阶段 leader与follower的通信时限,在本配置中即10*tickTime
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5 #leader与follower的同步通信时间
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/tmp/zookeeper # 保存zk中的快照数据
# 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
java api操作zk快速上手
在maven项目添加如下依赖
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.6.3</version>
</dependency>
<!--junit单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
package com.example.zookeeper;
import lombok.extern.slf4j.Slf4j;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.CountDownLatch;
@Slf4j
public class ZookeeperTest {
// IP 和端口
private final static String ipAddress = "xxxx:2181";
public static void main(String[] args) {
ZookeeperTest test = new ZookeeperTest();
String key = "/zkZsy";
String value = "this is zhangshiyu";
//创建Znode
// test.add(key,value);
// 获取节点数据
// test.get(key);
//修改节点数据
test.modify(key,"zhangshiyu hahahaha");
//删除节点
// test.delete(key);
}
/**
* @return
* @Author
* @Description
* @Date 10:22 2021/9/29
* @Param
**/
public static ZooKeeper getConntection() {
ZooKeeper zooKeeper = null;
try {
final CountDownLatch countDownLatch = new CountDownLatch(1);
//watch机制(回调),监听是否连接成功
zooKeeper = new ZooKeeper(ipAddress, 5000, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
if (Event.KeeperState.SyncConnected == watchedEvent.getState()) {
//如果受收到了服务端的响应事件,连接成功
countDownLatch.countDown();
}
}
});
countDownLatch.await();
log.info("zookeeper状态:{}",zooKeeper.getState());//CONNECTED
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return zooKeeper;
}
/** @Author lyy
* @Description //TODO 关闭ZooKeeper连接
* @Date 14:57 2021/9/29
* @Param
* @return
**/
public static void closeConnection(ZooKeeper zooKeeper) {
try {
// zooKeeper.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/** @Author lyy
* @Description //TODO 添加节点
* @Date 13:36 2021/9/29
* @Param
* @return
**/
public void add(String key ,String value) {
ZooKeeper zooKeeper = ZookeeperTest.getConntection();
try {
//参数类型
//1.key
//2.value
//3.对应的ACL,当前节点的权限控制
//4.设置当前节点类型
zooKeeper.create(key, value.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
ZookeeperTest.closeConnection(zooKeeper);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/** @Author lyy
* @Description //TODO 获取节点信息
* @Date 14:57 2021/9/29
* @Param
* @return
**/
public void get(String key) {
ZooKeeper zooKeeper = ZookeeperTest.getConntection();
Stat stat = new Stat();
String data = null;
try {
byte[] bytes = zooKeeper.getData(key, null, stat);
data = new String(bytes, "gbk");
log.info("当前节点信息:{}",data);
ZookeeperTest.closeConnection(zooKeeper);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
/** @Author lyy
* @Description //TODO 修改节点信息
* @Date 14:57 2021/9/29
* @Param
* @return
**/
public void modify(String key,String newValue) {
ZooKeeper zooKeeper = ZookeeperTest.getConntection();
Stat stat = new Stat();
//version乐观锁概念,此处需要获取version信息,则需要先get拿到节点信息
try {
//获取节点(修改需要version信息)
zooKeeper.getData(key, null, stat);
//再修改
zooKeeper.setData(key, newValue.getBytes(), stat.getVersion());
ZookeeperTest.closeConnection(zooKeeper);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/** @Author lyy
* @Description //TODO 删除节点
* @Date 14:57 2021/9/29
* @Param
* @return
**/
public void delete(String key) {
ZooKeeper zooKeeper = ZookeeperTest.getConntection();
Stat stat = new Stat();
try {
//获取节点(删除需要version信息)
zooKeeper.getData(key, null, stat);
//删除节点
zooKeeper.delete(key, stat.getVersion());
log.info("节点删除成功");
ZookeeperTest.closeConnection(zooKeeper);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}