部署环境

[root@node1 ~]# uname  -rm

2.6.32-504.el6.x86_64 x86_64

[root@node1 ~]# cat  /etc/redhat-release

CentOS release 6.6 (Final)

创建目录

[root@node1 ~]# mkdir  /{softs,app}

安装JDK

[root@node1 ~]# cd  /softs/

[root@node1 softs]# tar xf  jdk-8u77-linux-x64.tar.gz  -C  /app/

[root@node1 softs]# ln -s  /app/jdk1.8.0_77/ /app/jdk

[root@node1 softs]# vim  /etc/profile

export JAVA_HOME=/app/jdk

export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH

exportCLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/$jre/lib:$JAVA_HOME/lib/tools.jar

[root@node1 softs]# source  /etc/profile

[root@node1 softs]# java  -version

java version"1.8.0_77"

Java(TM) SE RuntimeEnvironment (build 1.8.0_77-b03)

Java HotSpot(TM) 64-BitServer VM (build 25.77-b03, mixed mode)

[root@node1 softs]# cd

部署zookeeper

安装

[root@node1 ~]# cd   /softs/

[root@node1 softs]# wget  http://mirrors.cnnic.cn/apache/zookeeper/zookeeper-3.4.8/zookeeper-3.4.8.tar.gz

[root@node1 softs]# tar xf  zookeeper-3.4.8.tar.gz  -C  /app/

[root@node1 softs]# ln -s  /app/zookeeper-3.4.8/  /app/zookeeper

[root@node1 softs]# cd

配置启动

[root@node1 ~]# cd  /app/zookeeper/conf/

[root@node1 conf]# cp zoo_sample.cfg zoo.cfg

[root@node1 conf]# grep  ^[a-z] zoo.cfg

tickTime=2000

dataDir=/data/zookeeper

clientPort=2181

[root@node1 ~]# mkdir -p  /data/zookeeper

[root@node1 ~]# vim  /etc/profile

PATH=/app/zookeeper/bin:$PATH

[root@node1 ~]# source  /etc/profile

[root@node1 ~]# zkServer.sh  start

ZooKeeper JMX enabled bydefault

Using config:/app/zookeeper/bin/../conf/zoo.cfg

Starting zookeeper ...STARTED

[root@node1 ~]# netstat-lntp|grep 2181

tcp        0     0 :::2181                    :::*                        LISTEN      1286/java

连接zookeeper

java连接

[root@node1 ~]# zkCli.sh -server  127.0.0.1:2181

Connecting to localhost:2181

c连接

[root@node1 ~]# cd/app/zookeeper/src/c

[root@node1 c]# ./configure

[root@node1 c]# make

[root@node1 c]# make install

[root@node1 c]# . cli_mt172.0.0.1:2821

[root@node1 c]#LD_LIBRARY_PATH=. cli_mt 127.0.0.1:2181

[root@node1 c]#LD_LIBRARY_PATH=. cli_st 127.0.0.1:2181

要在zookeeper的src/c目录下,但是客户端在新版的版本比较低。

操作zookeeper

一旦连接到zookeeper就可以执行一些命令,就像在文件系统一样

列出

[zk:127.0.0.1:2181(CONNECTED) 0] ls /

[zookeeper]

创建新的znode

执行create/zk_test my_data。这会创建一个新的znode,并且字符串my_data与该节点相关联

[zk:127.0.0.1:2181(CONNECTED) 1] create /zk_test my_data

Created /zk_test

[zk:127.0.0.1:2181(CONNECTED) 2] ls /

[zookeeper, zk_test]

验证

可以通过命令get  /zk_test来验证数据是和这个znode相关联

[zk:127.0.0.1:2181(CONNECTED) 3] get  /zk_test

my_data

cZxid = 0x14

ctime = Sat Jun 11 23:41:57CST 2016

mZxid = 0x14

mtime = Sat Jun 11 23:41:57CST 2016

pZxid = 0x14

cversion = 0

dataVersion = 0

aclVersion = 0

ephemeralOwner = 0x0

dataLength = 7

numChildren = 0

设置

通过set命令改变与zk_test相关联的数据

[zk:127.0.0.1:2181(CONNECTED) 4] set  /zk_test kevin

cZxid = 0x14

ctime = Sat Jun 11 23:41:57CST 2016

mZxid = 0x15

mtime = Sat Jun 11 23:49:22CST 2016

pZxid = 0x14

cversion = 0

dataVersion = 1

aclVersion = 0

ephemeralOwner = 0x0

dataLength = 5

numChildren = 0

[zk:127.0.0.1:2181(CONNECTED) 5] get  /zk_test

kevin

cZxid = 0x14

ctime = Sat Jun 11 23:41:57CST 2016

mZxid = 0x15

mtime = Sat Jun 11 23:49:22CST 2016

pZxid = 0x14

cversion = 0

删除

[zk:127.0.0.1:2181(CONNECTED) 6] delete /zk_test

[zk:127.0.0.1:2181(CONNECTED) 7] ls

[zk:127.0.0.1:2181(CONNECTED) 8] ls /

[zookeeper]

退出

[zk:127.0.0.1:2181(CONNECTED) 11] quit

Quitting...

停止服务

[root@node1 ~]# zkServer.sh stop