linux下zookeeper安装与测试

时间:2021-10-11 08:26:49

linux下zookeeper安装与测试

zookeeper 

一、linux下单机版zookeeper安装
(0)预备
安装java环境,并配置好java相关的环境变量$JAVA_HOME

(1)下载最新的稳定版并解压
从人人下载吧:
wget wget http://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.3.5/zookeeper-3.3.5.tar.gz

tar -zxvf zookeeper-3.4.3.tar.gz
可执行文件都在bin/目录下,配置都在conf/目录下

(2)生成配置
conf/目录下有个zoo_sample.cfg,是样板配置文件
复制一份成zoo.cfg
里面有两个比较重要的配置:
dataDir=/var/lib/zookeeper # 数据存放位置,可根据需要修改
clientPort=2181 # 服务监听端口,可根据需要修改

(3)启动服务
bin/zkServer.sh start
如果未启动成功,一个检查JAVA_HOME是否正确设置,二个看端口是否冲突

(4)启动客户端测试
启动命令:
bin/zkCli.sh -server 127.0.0.1:2181

显示根目录下、文件:
ls /

创建文件,并设置初始内容:
create /shenjian hello

获取文件内容:
get /shenjian

修改文件内容:
set /shenjian world

删除文件:
delete /shenjian

退出客户端:
quit

查看zookeeper的状态

 zk@PC0021:~/zookeeper-3.3.5/bin>zkServer.sh status
JMX enabled by default
Using config: /home/zk/zookeeper-3.3.5/bin/../conf/zoo.cfg
Mode: standalone

 

重启zookeeper 命令
zk@PC0021:~/zookeeper-3.3.5/bin>zkServer.sh  restart
JMX enabled by default
Using config: /home/zk/zookeeper-3.3.5/bin/../conf/zoo.cfg
JMX enabled by default
Using config: /home/zk/zookeeper-3.3.5/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
JMX enabled by default
Using config: /home/zk/zookeeper-3.3.5/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
zk@PC0021:~/zookeeper-3.3.5/bin>

 

 

 linux编写重启脚本:

 1.查找进程并删除进程,然后启动进程

C代码  linux下zookeeper安装与测试
  1. <span style="font-size: 16px;">ps -ef|grep 'zookeeper'|grep -v 'grep'|awk '{print $2}'|xargs -n1 kill -9  
  2.   
  3. cd /home/zk/zookeeper-3.3.5/bin  
  4. nohup ./zkServer.sh start &</span>  

 2.使用zookeeper server命令重启

Java代码  linux下zookeeper安装与测试
  1. cd /home/zk/zookeeper-3.3.5/bin  
  2. nohup ./zkServer.sh restart &  

 

 

 启动客户端

zk@PC0021:~/zookeeper-3.3.5/bin> zkCli.sh  -server 127.0.0.1:2181
Connecting to 127.0.0.1:2181
2013-05-29 16:12:29,245 - INFO  [main:Environment@97] - Clientenvironment:zookeeper.version=3.3.5-1301095, built on 03/15/2012 19:48 GMT
2013-05-29 16:12:29,246 - INFO  [main:Environment@97] - Clientenvironment:host.name=PC0021
2013-05-29 16:12:29,247 - INFO  [main:Environment@97] - Clientenvironment:java.version=1.6.0_30
2013-05-29 16:12:29,247 - INFO  [main:Environment@97] - Client

environment:java.vendor=Sun Microsystems Inc.
2013-05-29 16:12:29,247 - INFO  [main:Environment@97] - Clientenvironment:java.home=/usr/java/jdk1.6.0_30/jre
2013-05-29 16:12:29,247 - INFO  [main:Environment@97] - Client environment:java.class.path=/home/zk/zookeeper-3.3.5/bin/../build/classes:/home/zk/zookeeper-3.3.5/bin/../build/lib/*.jar:/home/zk/zookeeper-3.3.5/bin/../zookeeper-3.3.5.jar:/home/zk/zookeeper-3.3.5/bin/../lib/log4j-1.2.15.jar:/home/zk/zookeeper-3.3.5/bin/../lib/jline-0.9.94.jar:/home/zk/zookeeper-3.3.5/bin/../src/java/lib/*.jar:/home/zk/zookeeper-3.3.5/bin/../conf:/usr/java/jdk1.6.0_30/lib/dt.jar:/usr/java/jdk1.6.0_30/lib/tools.jar:
2013-05-29 16:12:29,248 - INFO  [main:Environment@97] - Client environment:java.library.path=/usr/java/jdk1.6.0_30/jre/lib/amd64/server:/usr/java/jdk1.6.0_30/jre/lib/amd64:/usr/java/jdk1.6.0_30/jre/../lib/amd64:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2013-05-29 16:12:29,248 - INFO  [main:Environment@97] - Client environment:java.io.tmpdir=/tmp
2013-05-29 16:12:29,248 - INFO  [main:Environment@97] - Client environment:java.compiler=<NA>
2013-05-29 16:12:29,248 - INFO  [main:Environment@97] - Client environment:os.name=Linux
2013-05-29 16:12:29,249 - INFO  [main:Environment@97] - Client environment:os.arch=amd64
2013-05-29 16:12:29,249 - INFO  [main:Environment@97] - Clientenvironment:os.version=2.6.32.12-0.7-default
2013-05-29 16:12:29,249 - INFO  [main:Environment@97] - Client environment:user.name=zk
2013-05-29 16:12:29,249 - INFO  [main:Environment@97] - Clientenvironment:user.home=/home/zk
2013-05-29 16:12:29,249 - INFO  [main:Environment@97] - Clientenvironment:user.dir=/home/zk/zookeeper-3.3.5/bin
2013-05-29 16:12:29,250 - INFO  [main:ZooKeeper@379] - Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=30000watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@2d92b996
Welcome to ZooKeeper!
2013-05-29 16:12:29,263 - INFO  [main-SendThread():ClientCnxn$SendThread@1058] - Opening socket connection to server /127.0.0.1:2181
2013-05-29 16:12:29,278 - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@947] - Socket connection established to localhost/127.0.0.1:2181, initiating session
JLine support is enabled
[zk: 127.0.0.1:2181(CONNECTING) 0] 2013-05-29 16:12:29,401 - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@736] - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x13eef5467810000, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
ls
ZooKeeper -server host:port cmd args
        connect host:port
        get path [watch]
        ls path [watch]
        set path data [version]
        delquota [-n|-b] path
        quit
        printwatches on|off
        create [-s] [-e] path data acl
        stat path [watch]
        close
        ls2 path [watch]
        history
        listquota path
        setAcl path acl
        getAcl path
        sync path
        redo cmdno
        addauth scheme auth
        delete path [version]
        setquota -n|-b val path
[zk: 127.0.0.1:2181(CONNECTED) 1] ls /
[zookeeper]
[zk: 127.0.0.1:2181(CONNECTED) 2] create /xiaobai hello
Created /xiaobai
[zk: 127.0.0.1:2181(CONNECTED) 3] get /xiaobai
hello
cZxid = 0x2
ctime = Wed May 29 16:13:14 CST 2013
mZxid = 0x2
mtime = Wed May 29 16:13:14 CST 2013
pZxid = 0x2
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 5
numChildren = 0
[zk: 127.0.0.1:2181(CONNECTED) 4] set /xiaobai world
cZxid = 0x2
ctime = Wed May 29 16:13:14 CST 2013
mZxid = 0x3
mtime = Wed May 29 16:13:33 CST 2013
pZxid = 0x2
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 5
numChildren = 0
[zk: 127.0.0.1:2181(CONNECTED) 5] get /xiaobai
world
cZxid = 0x2
ctime = Wed May 29 16:13:14 CST 2013
mZxid = 0x3
mtime = Wed May 29 16:13:33 CST 2013
pZxid = 0x2
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 5
numChildren = 0
[zk: 127.0.0.1:2181(CONNECTED) 6] delete /xiaobai
[zk: 127.0.0.1:2181(CONNECTED) 7] get /xiaobai
Node does not exist: /xiaobai
[zk: 127.0.0.1:2181(CONNECTED) 8]