文档结构
- 1、概念简介
- 1.1、选举机制
- 2、环境部署
- 2.1、单机部署
- 2.2、集群部署
- 2.3、客户端指令
- 3、配置应用
1、概念简介
1.1、选举机制
2、环境部署
下载链接:https://zookeeper.apache.org/releases.html
下载版本:apache-zookeeper-3.7.1-bin.tar.gz
此处下载带 bin 的包,即经过编译的安装包;如果下载源码包,要重新编译打包后才可以使用;否则启动服务会报错
Error: Could not find or load main class org.apache.zookeeper.server.quorum.QuorumPeerMain
;
2.1、单机部署
1)下载解压
解压和安装路径:/software/Zookeeper
2)配置调整
zk 默认没有 zoo.cfg 文件,需要复制 conf 下的样例配置文件;
[gdsuser@hz-yyserv bin]$ pwd
/software/Zookeeper/bin
[gdsuser@hz-yyserv bin]$ cp ../conf/zoo_sample.cfg ../conf/zoo.cfg
[gdsuser@hz-yyserv bin]$
[gdsuser@hz-yyserv bin]$ ll ../conf
total 16
-rw-r--r-- 1 gdsuser gdsgrp 535 May 7 14:44 configuration.xsl
-rw-r--r-- 1 gdsuser gdsgrp 3435 May 7 14:44 log4j.properties
-rw-r--r-- 1 gdsuser gdsgrp 1148 Aug 18 14:28 zoo.cfg
-rw-r--r-- 1 gdsuser gdsgrp 1148 May 7 14:44 zoo_sample.cfg
[gdsuser@hz-yyserv bin]$
设置数据文件路径:为 ../dataDir
[gdsuser@hz-yyserv conf]$ pwd
/software/Zookeeper/conf
[gdsuser@hz-yyserv conf]$ vim zoo.cfg
# 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=../dataDir
# 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.
#
3)启动服务端
[gdsuser@hz-yyserv bin]$
[gdsuser@hz-yyserv bin]$ sh zkServer.sh start
[gdsuser@hz-yyserv bin]$ sh zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /software/Zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[gdsuser@hz-yyserv bin]$
- 状态查看
[gdsuser@hz-yyserv bin]$ sh zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /software/Zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: standalone
[gdsuser@hz-yyserv bin]$
- 停止服务
sh zkServer.sh stop
4)客户端启动
sh zkCli.sh
输出信息:
Connecting to localhost:2181
................
................
................
Welcome to ZooKeeper!
................
................
WATCHER::
WatchedEvent state:SyncConnected type: None path:null
[zk: localhost:2181(CONNECTED) 0]
2.2、集群部署
zookeeper 的三个端口作用
A)2181 : 对 client 端提供服务
B)2888 : 集群内机器通信使用
C)3888 : 选举 leader 使用
按照3个节点组成一套zookeeper集群,相比于单节点的部署方式,集群新增如下步骤:
1)在每个节点 zoo.cfg 文件配置末尾添加如下配置
server.1=node1_ip:2888:3888
server.2=node2_ip:2888:3888
server.3=node3_ip:2888:3888
2)配置 myid
在 zookeeper根目录新建 myid文件,按照节点编号分别配置,如 1 2 3;
3)服务端启动
说明:该步骤和单节点相同;
3)状态查看
区别于单节点的状态Mode: standalone,集群模式下模式的值分为 leader或 follswer;
2.3、客户端指令
- 节点访问
[gdsuser@hz-yyserv bin]$ sh zkCli.sh
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 1] ls /zookeeper
[config, quota]
[zk: localhost:2181(CONNECTED) 2] ls /zookeeper/config
[]
- 节点属性
[zk: localhost:2181(CONNECTED) 3] get -s -w /zookeeper/configcZxid = 0x0
ctime = Thu Jan 01 08:00:00 CST 1970
mZxid = 0x0
mtime = Thu Jan 01 08:00:00 CST 1970
pZxid = 0x0
cversion = 0
dataVersion = 0
aclVersion = -1
ephemeralOwner = 0x0
dataLength = 0
numChildren = 0
[zk: localhost:2181(CONNECTED) 4]
说明:'get path [watch]' has been deprecated. Please use 'get [-s] [-w] path' instead.
- 节点状态
[zk: localhost:2181(CONNECTED) 8] stat -w /zookeeper/config
cZxid = 0x0
ctime = Thu Jan 01 08:00:00 CST 1970
mZxid = 0x0
mtime = Thu Jan 01 08:00:00 CST 1970
pZxid = 0x0
cversion = 0
dataVersion = 0
aclVersion = -1
ephemeralOwner = 0x0
dataLength = 0
numChildren = 0
[zk: localhost:2181(CONNECTED) 9]
- 退出连接
[zk: localhost:2181(CONNECTED) 9] quit
3、配置应用
============================================== over =============================================