下面介绍Linux系统中Zookeeper的初次使用方法。
1、jdk安装和zookeeper下载
首先从jdk官网中下载jdk文件,然后将文件放在/usr/local/java目录下解压,并打开.bashrc文件进行设置:
export JAVA_HOME=/usr/local/java/jdk1.8.0_111 export JRE_HOME=${JAVA_HOME}/jre export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib export PATH=${JAVA_HOME}/bin:$PATH
再从zookeeper官网中下载zookeeper然后解压。
2、建立会话
2.1、配置文件
conf目录下放置一个zoo.cfg文件,可以直接复制zoo_sample.cfg文件。
2.2、设置data目录
指定配置文件的dataDir路径,比如/home/me/zookeeper。
2.3、启动服务器
可以运行./bin/zkServer.sh start命令在后台启动服务器,或者运行./bin/zkServer.sh start-foreground在前台运行服务器。服务器运行成功后控制台会输出以下信息:
Using config:../conf/zoo.cfg
Starting zookeeper ... STARTED
2.4、启动客户端
可以运行./bin/zkCli.sh默认连接本地zookeeper服务器,或者./bin/zkCli.sh -server ip:port指定zookeeper服务器。连接成功后,会显示以下信息:
2017-01-01 23:35:28,619 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@446cdf90
Welcome to ZooKeeper!
2017-01-01 23:35:28,665 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2017-01-01 23:35:28,794 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@876] - Socket connection established to localhost/127.0.0.1:2181, initiating session
[zk: localhost:2181(CONNECTING) 0] 2017-01-01 23:35:28,920 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x1595e1ab3730000, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null