zookeeper 单机环境

单机模式

Zookeeper单机安装,这种模式主要用在测试或demo的情况下。

下载地址:

https://www.apache.org/dyn/closer.cgi

安装

单机模式安装比较简单

wget http://apache.fayea.com/zookeeper/zookeeper-3.4.9/zookeeper-3.4.9.tar.gz

tar -zxvf zookeeper-3.4.9.tar.gz

vim ~/.bash_profile

export PATH=/usr/local/zookeeper/bin:$PATH

source ~/.bash_profile

修改配置

cd /usr/local/zookeeper/conf

cp /usr/local/zookeeper/conf/zoo_sample.cfg /usr/local/zookeeper/conf/zoo.cfg

[root@bogon conf]# vim zoo.cfg 
...

# 修改改目录
dataDir=/var/zookeeper
#末尾新增
server.1=127.0.0.1:2888:38888

配置示例:

# 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=/var/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

server.1=127.0.0.1:2888:38888

clientPort
服务的监听端口

dataDir
用于存放内存数据库快照的文件夹,同时用于集群的myid文件也存在这个文件夹里(注意:一个配置文件只能包含一个dataDir字样 )

dataLogDir:
log目录. 如果没有设置该参数, 将使用和dataDir相同的设置.

测试

启动服务

[root@bogon bin]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

2.输入jps命令,查看进程

[root@bogon ~]# jps
3857 Jps
3574 QuorumPeerMain

yum安装java,jps不存在情况处理

# 安装对应版本的openjdk-devel即可
yum install java-1.8.0-openjdk-devel

3、查看状态

[root@bogon ~]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: standalone

4、启动客户端脚本

 zkCli.sh -server 127.0.0.1:2181

5连接客户端

[root@bogon ~]# zkCli.sh -server 127.0.0.1:2181
Connecting to 127.0.0.1:2181
2017-06-04 02:33:57,876 [myid:] - INFO  [main:Environment@100] - Client environment:zookeeper.version=3.4.9-1757313, built on 08/23/2016 06:50 GMT
2017-06-04 02:33:57,881 [myid:] - INFO  [main:Environment@100] - Client environment:host.name=bogon
2017-06-04 02:33:57,881 [myid:] - INFO  [main:Environment@100] - Client environment:java.version=1.8.0_131
2017-06-04 02:33:57,885 [myid:] - INFO  [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
....
[zk: 127.0.0.1:2181(CONNECTED) 0] 

5 测试:

连接客户端输入命令:

# 查看根节点
[zk: 127.0.0.1:2181(CONNECTED) 9] ls /
[zookeeper]

# 创建节点node_1 
[zk: 127.0.0.1:2181(CONNECTED) 10] create /node_1 php     
Created /node_1

# 创建节点node_2 
[zk: 127.0.0.1:2181(CONNECTED) 11] create /node_2 java
Created /node_2

# 创建节点node_3 
[zk: 127.0.0.1:2181(CONNECTED) 12] create /node_3 c#  
Created /node_3

# 查看根节点
[zk: 127.0.0.1:2181(CONNECTED) 13] ls /
[zookeeper, node_1, node_2, node_3]

# 查看节点内容
[zk: 127.0.0.1:2181(CONNECTED) 14] get /node_1
php
cZxid = 0x1d
ctime = Mon Jun 05 21:42:20 CST 2017
mZxid = 0x1d
mtime = Mon Jun 05 21:42:20 CST 2017
pZxid = 0x1d
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0

# 设置节点
[zk: 127.0.0.1:2181(CONNECTED) 15] set /node_1 python
cZxid = 0x1d
ctime = Mon Jun 05 21:42:20 CST 2017
mZxid = 0x20
mtime = Mon Jun 05 21:43:05 CST 2017
pZxid = 0x1d
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0

# 查看节点内容
[zk: 127.0.0.1:2181(CONNECTED) 16] get /node_1       
python
cZxid = 0x1d
ctime = Mon Jun 05 21:42:20 CST 2017
mZxid = 0x20
mtime = Mon Jun 05 21:43:05 CST 2017
pZxid = 0x1d
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0

# 删除节点
[zk: 127.0.0.1:2181(CONNECTED) 17] delete /node_1

# 再次查看根节点
[zk: 127.0.0.1:2181(CONNECTED) 18] ls /              
[zookeeper, node_2, node_3]

6 停止服务

zkServer.sh stop

推荐阅读更多精彩内容