版本:ZooKeeper v3.4.6稳定版
要设置ZooKeeper以单独服务器的模式运行是非常直接的。服务器包含在单个的JAR文件内,因此安装就只需创建一个配置文件。
一旦下载了ZooKeeper稳定版,那么解压它,并进入ZooKeeper的根目录。
$ wgethttp://mirror.bit.edu.cn/apache/zookeeper/stable/zookeeper-3.4.6.tar.gz $ mvzookeeper-3.4.6 zookeeper
tickTime=2000 dataDir=/var/lib/zookeeper clientPort=2181
tickTime:ZooKeeper使用的基本时间单元(毫秒为单位)。它用于做心跳和最小的会话超时机制。
dataDir:存储内容数据库快照的目录,除非另行指定,否则更新的事务处理日志也会记录到数据库。
clientPort:客户端连接监听的端口
创建了配置文件后,就可以启动ZooKeeper了。
$bin/zkServer.sh start JMXenabled by default Usingconfig: /home/chuser/zookeeper/bin/../conf/zoo.cfg Startingzookeeper ... STARTED
客户端连接到ZooKeeper
一旦ZooKeeper运行了,要连接到ZooKeeper,你有几个选择:
1)使用Java
$bin/zkCli.sh -server 127.0.0.1:2181 Connecting to 127.0.0.1:2181 2014-05-02 07:39:20,388 [myid:] - INFO [main:Environment@100] - Clientenvironment:zookeeper.version=3.4.6-1569965, built on 02/20/2014 09:09 GMT 2014-05-02 07:39:20,394 [myid:] - INFO [main:Environment@100] - Clientenvironment:host.name=10.0.0.13 2014-05-02 07:39:20,394 [myid:] - INFO [main:Environment@100] - Clientenvironment:java.version=1.7.0_51 2014-05-02 07:39:20,398 [myid:] - INFO [main:Environment@100] - Clientenvironment:java.vendor=Oracle Corporation 2014-05-02 07:39:20,399 [myid:] - INFO [main:Environment@100] - Clientenvironment:java.home=/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.51.x86_64/jre 2014-05-02 07:39:20,399 [myid:] - INFO [main:Environment@100] - Clientenvironment:java.class.path=/home/chuser/zookeeper/bin/../build/classes:/home/chuser/zookeeper/bin/../build/lib/*.jar:/home/chuser/zookeeper/bin/../lib/slf4j-log4j12-1.6.1.jar:/home/chuser/zookeeper/bin/../lib/slf4j-api-1.6.1.jar:/home/chuser/zookeeper/bin/../lib/netty-3.7.0.Final.jar:/home/chuser/zookeeper/bin/../lib/log4j-1.2.16.jar:/home/chuser/zookeeper/bin/../lib/jline-0.9.94.jar:/home/chuser/zookeeper/bin/../zookeeper-3.4.6.jar:/home/chuser/zookeeper/bin/../src/java/lib/*.jar:/home/chuser/zookeeper/bin/../conf: 2014-05-02 07:39:20,399 [myid:] - INFO [main:Environment@100] - Clientenvironment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib 2014-05-02 07:39:20,399 [myid:] - INFO [main:Environment@100] - Clientenvironment:java.io.tmpdir=/tmp 2014-05-02 07:39:20,400 [myid:] - INFO [main:Environment@100] - Clientenvironment:java.compiler=<NA> 2014-05-02 07:39:20,400 [myid:] - INFO [main:Environment@100] - Clientenvironment:os.name=Linux 2014-05-02 07:39:20,400 [myid:] - INFO [main:Environment@100] - Clientenvironment:os.arch=amd64 2014-05-02 07:39:20,400 [myid:] - INFO [main:Environment@100] - Clientenvironment:os.version=2.6.32-431.3.1.el6.x86_64 2014-05-02 07:39:20,400 [myid:] - INFO [main:Environment@100] - Clientenvironment:user.name=chuser 2014-05-02 07:39:20,401 [myid:] - INFO [main:Environment@100] - Clientenvironment:user.home=/home/chuser 2014-05-02 07:39:20,401 [myid:] - INFO [main:Environment@100] - Clientenvironment:user.dir=/home/chuser/zookeeper 2014-05-02 07:39:20,403 [myid:] - INFO [main:ZooKeeper@438] - Initiating clientconnection, connectString=127.0.0.1:2181 sessionTimeout=30000watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@748197b2 Welcome to ZooKeeper! 2014-05-02 07:39:20,448 [myid:] - INFO [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@975] - Openingsocket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt toauthenticate using SASL (unknown error) JLine support is enabled 2014-05-02 07:39:20,460 [myid:] - INFO [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@852] - Socketconnection established to 127.0.0.1/127.0.0.1:2181, initiating session 2014-05-02 07:39:20,491 [myid:] - INFO [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@1235] - Sessionestablishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid =0x145bbdc9b460001, negotiated timeout = 30000 WATCHER:: WatchedEvent state:SyncConnected type:Nonepath:null [zk: 127.0.0.1:2181(CONNECTED) 0]
2)使用C语言
在ZooKeeper的源码src/c子目录通过运行make cli_mt或make cli_st,编译多线程客户端cli_mt或单线程客户端cli_st,再运行客户端:
LD_LIBRARY_PATH=. cli_mt 127.0.0.1:2181或
LD_LIBRARY_PATH=. cli_st 127.0.0.1:2181