ZooKeeper做独立server执行(上)
作者:chszs。转载需注明。博客主页:
一、ZooKeeper安装及配置
版本号:ZooKeeper v3.4.6稳定版
要设置ZooKeeper以单独server的模式执行是很直接的。
server包括在单个的JAR文件内,因此安装就仅仅需创建一个配置文件。
一旦下载了ZooKeeper稳定版。那么解压它,并进入ZooKeeper的根文件夹。
$ wgethttp://mirror.bit.edu.cn/apache/zookeeper/stable/zookeeper-3.4.6.tar.gz$ mvzookeeper-3.4.6 zookeeper要启动ZooKeeper,还须要一个配置文件,能够创建conf/zoo.cfg配置文件。内容例如以下:
tickTime=2000dataDir=/var/lib/zookeeperclientPort=2181zoo.cfg是配置文件的默认名字。以下我们说说配置文件的各项含义。
tickTime:ZooKeeper使用的基本时间单元(毫秒为单位)。它用于做心跳和最小的会话超时机制。
dataDir:存储内容数据库快照的文件夹,除非另行指定,否则更新的事务处理日志也会记录到数据库。
clientPort:client连接监听的端口
二、ZooKeeper做独立server执行
创建了配置文件后,就能够启动ZooKeeper了。
$bin/zkServer.sh startJMXenabled by defaultUsingconfig: /home/chuser/zookeeper/bin/../conf/zoo.cfgStartingzookeeper ... STARTEDZooKeeper使用Log4J记录日志消息,你能够从控制台或日志输出文件来查看日志,详细取决于Log4J的配置。
三、ZooKeeperclient连接
client连接到ZooKeeper
一旦ZooKeeper执行了。要连接到ZooKeeper,你有几个选择:
1)使用Java
$bin/zkCli.sh -server 127.0.0.1:2181Connecting to 127.0.0.1:21812014-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 GMT2014-05-02 07:39:20,394 [myid:] - INFO [main:Environment@100] - Clientenvironment:host.name=10.0.0.132014-05-02 07:39:20,394 [myid:] - INFO [main:Environment@100] - Clientenvironment:java.version=1.7.0_512014-05-02 07:39:20,398 [myid:] - INFO [main:Environment@100] - Clientenvironment:java.vendor=Oracle Corporation2014-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/jre2014-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/lib2014-05-02 07:39:20,399 [myid:] - INFO [main:Environment@100] - Clientenvironment:java.io.tmpdir=/tmp2014-05-02 07:39:20,400 [myid:] - INFO [main:Environment@100] - Clientenvironment:java.compiler=2014-05-02 07:39:20,400 [myid:] - INFO [main:Environment@100] - Clientenvironment:os.name=Linux2014-05-02 07:39:20,400 [myid:] - INFO [main:Environment@100] - Clientenvironment:os.arch=amd642014-05-02 07:39:20,400 [myid:] - INFO [main:Environment@100] - Clientenvironment:os.version=2.6.32-431.3.1.el6.x86_642014-05-02 07:39:20,400 [myid:] - INFO [main:Environment@100] - Clientenvironment:user.name=chuser2014-05-02 07:39:20,401 [myid:] - INFO [main:Environment@100] - Clientenvironment:user.home=/home/chuser2014-05-02 07:39:20,401 [myid:] - INFO [main:Environment@100] - Clientenvironment:user.dir=/home/chuser/zookeeper2014-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@748197b2Welcome 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 enabled2014-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 session2014-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,编译多线程clientcli_mt或单线程clientcli_st,再执行client:
LD_LIBRARY_PATH=. cli_mt 127.0.0.1:2181或
LD_LIBRARY_PATH=. cli_st 127.0.0.1:2181