安装zookeeper单机模式
下载地址:Apache ZooKeeper™版本
入门文档:ZooKeeper入门指南
参考资料:
Zookeeper入门看这篇就够了
-
先决条件:
ZooKeeper在Java 1.7或更高版本(JDK 7或更高版本,FreeBSD支持需要openjdk7)中运行。它作为ZooKeeper服务器的整体运行。建议三个最小的ZooKeeper服务器是一个整体的最小大小,我们也建议它们在单独的计算机上运行。 -
下载并解压
由于生产环境一般都是linux,且linux命令和windows有所区别,我们就在你windows中使用。
检查是否安装java环境并进入相应目录
[root@izm5e11cqeaucml4d3vumbz ~]# java -version
openjdk version "1.8.0_232"
OpenJDK Runtime Environment (build 1.8.0_232-b09)
OpenJDK 64-Bit Server VM (build 25.232-b09, mixed mode)
[root@izm5e11cqeaucml4d3vumbz apache-zookeeper-3.6.0-bin]# cd /opt/apache-zookeeper-3.6.0-bin
[root@izm5e11cqeaucml4d3vumbz apache-zookeeper-3.6.0-bin]#
- 设置配置文件并启动
[root@izm5e11cqeaucml4d3vumbz apache-zookeeper-3.6.0-bin]# cp conf/zoo_sample.cfg conf/zoo.cfg
[root@izm5e11cqeaucml4d3vumbz apache-zookeeper-3.6.0-bin]# bin/zkServer.sh start
-bash: bin/zkServer.sh: Permission denied
出现一个小问题,拒绝执行。我们对目录授个权,再次执行
[root@izm5e11cqeaucml4d3vumbz apache-zookeeper-3.6.0-bin]# chmod 755 -R /opt/apache-zookeeper-3.6.0-bin
[root@izm5e11cqeaucml4d3vumbz apache-zookeeper-3.6.0-bin]# bin/zkServer.sh start
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/apache-zookeeper-3.6.0-bin/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@izm5e11cqeaucml4d3vumbz apache-zookeeper-3.6.0-bin]#
- 验证
[root@izm5e11cqeaucml4d3vumbz apache-zookeeper-3.6.0-bin]# bin/zkCli.sh
/usr/bin/java
Connecting to localhost:2181
....
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0]
使用客户端命令操作zookeeper
- 查看
[zk: localhost:2181(CONNECTED) 1] ls
ls [-s] [-w] [-R] path
[zk: localhost:2181(CONNECTED) 5] ls /
[zookeeper]
- 创建
[zk: localhost:2181(CONNECTED) 6] create
create [-s] [-e] [-c] [-t ttl] path [data] [acl]
[zk: localhost:2181(CONNECTED) 7] create /zkPro myData
Created /zkPro
[zk: localhost:2181(CONNECTED) 8] ls /
[zkPro, zookeeper]
- 获取数据
[zk: localhost:2181(CONNECTED) 9] get
get [-s] [-w] path
[zk: localhost:2181(CONNECTED) 11] get /zkPro
myData
- 数据设置
[zk: localhost:2181(CONNECTED) 12] set
set [-s] [-v version] path data
[zk: localhost:2181(CONNECTED) 13] set /zkPro myData123
[zk: localhost:2181(CONNECTED) 14] get /zkPro
myData123
- 删除节点
[zk: localhost:2181(CONNECTED) 15] delete
delete [-v version] path
[zk: localhost:2181(CONNECTED) 16] delete /zkPro
[zk: localhost:2181(CONNECTED) 17] ls /
分析下配置文件
# 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=/tmp/zookeeper
# 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
## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true
参考资料:
https://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_configuration
zookeeper配置文件zoo.cfg详细讲解
Zookeeper配置文件
java连接操作
快速创建个精简的spring mvc,参考:https://www.jianshu.com/p/e878756fb75c
- 创建zookeeper文件夹
- 使用git获取代码https://code.aliyun.com/liyi1314/quicklyCreateSpringMvc.git
- 打开pom.xml修改 ,并导入maven
<artifactId>mini.Spring1</artifactId>
为
<artifactId>mini.Spring1.zookeeper</artifactId>
引入pom.xml
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.6.0</version>
</dependency>
写入ZooKeeperProSync.java
public class ZooKeeperProSync implements Watcher {
private static CountDownLatch connectedSemaphore = new CountDownLatch(1);
private static ZooKeeper zk = null;
private static Stat stat = new Stat();
public static void main(String[] args) throws Exception {
//zookeeper配置数据存放路径
String path = "/username";
//连接zookeeper并且注册一个默认的监听器
zk = new ZooKeeper("xxxx:2181", 1000, new ZooKeeperProSync());
//等待zk连接成功的通知
connectedSemaphore.await();
//获取path目录节点的配置数据,并注册默认的监听器
System.out.println(new String(zk.getData(path, true, stat)));
Thread.sleep(Integer.MAX_VALUE);
}
public void process(WatchedEvent event) {
if (Watcher.Event.KeeperState.SyncConnected == event.getState()) { //zk连接成功通知事件
if (Watcher.Event.EventType.None == event.getType() && null == event.getPath()) {
connectedSemaphore.countDown();
} else if (event.getType() == Watcher.Event.EventType.NodeDataChanged) { //zk目录节点数据变化通知事件
try {
System.out.println("配置已修改,新值为:" + new String(zk.getData(event.getPath(), true, stat)));
} catch (Exception e) {
}
}
}
}
}
Zookeeper集群模式安装
进入目录设置conf文件
配置文件
[root@izm5e11cqeaucml4d3vumbz apache-zookeeper-3.6.0-bin]# cd /opt/apache-zookeeper-3.6.0-bin
[root@izm5e11cqeaucml4d3vumbz apache-zookeeper-3.6.0-bin]# cp conf/zoo.cfg conf/zoo1.cfg
[root@izm5e11cqeaucml4d3vumbz apache-zookeeper-3.6.0-bin]# vim conf/zoo1.cfg
修改配置,配置服务器1
# vim conf/zoo-1.cfg
dataDir=/tmp/zookeeper-1
clientPort=2181
server.1=127.0.0.1:2888:3888
server.2=127.0.0.1:2889:3889
server.3=127.0.0.1:2890:3890
配置服务器2、3
[root@izm5e11cqeaucml4d3vumbz apache-zookeeper-3.6.0-bin]# cp conf/zoo1.cfg conf/zoo2.cfg
[root@izm5e11cqeaucml4d3vumbz apache-zookeeper-3.6.0-bin]# cp conf/zoo1.cfg conf/zoo3.cfg
修改配置2、3
dataDir=/tmp/zookeeper-2
clientPort=2182
# vim conf/zoo-2.cfg
dataDir=/tmp/zookeeper-3
clientPort=2183
标识Server ID
# cd /tmp/zookeeper-1
# vim myid
1
# cd /tmp/zookeeper-2
# vim myid
2
# cd /tmp/zookeeper-3
# vim myid
3
启动三个zookeeper实例
# bin/zkServer.sh start conf/zoo-1.cfg
# bin/zkServer.sh start conf/zoo-2.cfg
# bin/zkServer.sh start conf/zoo-3.cfg
检测集群状态
[root@izm5e11cqeaucml4d3vumbz apache-zookeeper-3.6.0-bin]# bin/zkServer.sh status conf/zoo2.cfg
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: conf/zoo2.cfg
Client port found: 2182. Client address: localhost.
Mode: leader
[root@izm5e11cqeaucml4d3vumbz apache-zookeeper-3.6.0-bin]# bin/zkServer.sh status conf/zoo1.cfg
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: conf/zoo1.cfg
Client port found: 2181. Client address: localhost.
Mode: follower
[root@izm5e11cqeaucml4d3vumbz apache-zookeeper-3.6.0-bin]# bin/zkServer.sh status conf/zoo3.cfg
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: conf/zoo3.cfg
Client port found: 2183. Client address: localhost.
Mode: follower
参考资料:
Zookeeper入门看这篇就够了
网友评论