共计 10816 个字符,预计需要花费 28 分钟才能阅读完成。
一、ZooKeeper 安装及配置
版本: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
要启动 ZooKeeper,还需要一个配置文件,可以创建 conf/zoo.cfg 配置文件,内容如下:
tickTime=2000
dataDir=/var/lib/zookeeper
clientPort=2181
zoo.cfg 是配置文件的默认名字。下面我们说说配置文件的各项含义。
tickTime:ZooKeeper 使用的基本时间单元(毫秒为单位)。它用于做心跳和最小的会话超时机制。
dataDir:存储内容数据库快照的目录,除非另行指定,否则更新的事务处理日志也会记录到数据库。
clientPort:客户端连接监听的端口
————————————– 分割线 ————————————–
ZooKeeper 集群配置 http://www.linuxidc.com/Linux/2013-06/86348.htm
使用 ZooKeeper 实现分布式共享锁 http://www.linuxidc.com/Linux/2013-06/85550.htm
分布式服务框架 ZooKeeper — 管理分布式环境中的数据 http://www.linuxidc.com/Linux/2013-06/85549.htm
ZooKeeper 集群环境搭建实践 http://www.linuxidc.com/Linux/2013-04/83562.htm
ZooKeeper 服务器集群环境配置实测 http://www.linuxidc.com/Linux/2013-04/83559.htm
ZooKeeper 集群安装 http://www.linuxidc.com/Linux/2012-10/72906.htm
————————————– 分割线 ————————————–
二、ZooKeeper 做独立服务器运行
创建了配置文件后,就可以启动 ZooKeeper 了。
$bin/zkServer.sh start
JMXenabled by default
Usingconfig: /home/chuser/zookeeper/bin/../conf/zoo.cfg
Startingzookeeper … STARTED
ZooKeeper 使用 Log4J 记录日志消息,你可以从控制台或日志输出文件来查看日志,具体取决于 Log4J 的配置。
三、ZooKeeper 客户端连接
客户端连接到 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
更多详情见请继续阅读下一页的精彩内容 :http://www.linuxidc.com/Linux/2014-05/102190p2.htm
四、ZooKeeper Java 客户端连接后的命令概述
键入 help,可以看到有如下命令:
connecthost:port
getpath [watch]
lspath [watch]
setpath data [version]
rmrpath
delquota[-n|-b] path
quit
printwatcheson|off
create[-s] [-e] path data acl
statpath [watch]
close
ls2path [watch]
history
listquotapath
setAclpath acl
getAclpath
syncpath
redocmdno
addauthscheme auth
deletepath [version]
setquota-n|-b val path
下面,我们来试用这些命令。
首先,看看列表命令,如下:
[zk: 127.0.0.1:2181(CONNECTED) 6] ls /
[zookeeper]
接下来,我们创建一个新的 znode 节点,这个 znode 节点相关的字符串为“my_data”,如下:
[zk: 127.0.0.1:2181(CONNECTED) 6] create/zk_test my_data
Created /zk_test
再次执行列表命令,如下:
[zk: 127.0.0.1:2181(CONNECTED) 6] ls /
[zookeeper, zk_test]
可以看到,zk_test 目录已经被创建。
下面,我们使用 get 命令来验证此 znode 节点相关的数据,如下:
[zk: 127.0.0.1:2181(CONNECTED) 7] get/zk_test
my_data
cZxid = 0x4
ctime = Fri May 02 08:28:40 UTC 2014
mZxid = 0x4
mtime = Fri May 02 08:28:40 UTC 2014
pZxid = 0x4
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 0
我们还可以使用 set 命令修改此 znode 相关的数据,如下:
[zk: 127.0.0.1:2181(CONNECTED) 8] set/zk_test junk
cZxid = 0x4
ctime = Fri May 02 08:28:40 UTC 2014
mZxid = 0x5
mtime = Fri May 02 08:34:32 UTC 2014
pZxid = 0x4
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
最后,我们可以删除此 znode 节点,如下:
[zk: 127.0.0.1:2181(CONNECTED) 10] delete/zk_test
[zk: 127.0.0.1:2181(CONNECTED) 11] ls /
[zookeeper]
退出 ZooKeeper 客户端连接,执行命令:
[zk: 127.0.0.1:2181(CONNECTED) 12] quit
Quitting…
2014-05-02 08:49:47,415 [myid:] – INFO [main:ZooKeeper@684] – Session:0x145bbdc9b460001 closed
2014-05-02 08:49:47,415 [myid:] – INFO [main-EventThread:ClientCnxn$EventThread@512]- EventThread shut down
ZooKeeper 的详细介绍 :请点这里
ZooKeeper 的下载地址 :请点这里
一、ZooKeeper 安装及配置
版本: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
要启动 ZooKeeper,还需要一个配置文件,可以创建 conf/zoo.cfg 配置文件,内容如下:
tickTime=2000
dataDir=/var/lib/zookeeper
clientPort=2181
zoo.cfg 是配置文件的默认名字。下面我们说说配置文件的各项含义。
tickTime:ZooKeeper 使用的基本时间单元(毫秒为单位)。它用于做心跳和最小的会话超时机制。
dataDir:存储内容数据库快照的目录,除非另行指定,否则更新的事务处理日志也会记录到数据库。
clientPort:客户端连接监听的端口
————————————– 分割线 ————————————–
ZooKeeper 集群配置 http://www.linuxidc.com/Linux/2013-06/86348.htm
使用 ZooKeeper 实现分布式共享锁 http://www.linuxidc.com/Linux/2013-06/85550.htm
分布式服务框架 ZooKeeper — 管理分布式环境中的数据 http://www.linuxidc.com/Linux/2013-06/85549.htm
ZooKeeper 集群环境搭建实践 http://www.linuxidc.com/Linux/2013-04/83562.htm
ZooKeeper 服务器集群环境配置实测 http://www.linuxidc.com/Linux/2013-04/83559.htm
ZooKeeper 集群安装 http://www.linuxidc.com/Linux/2012-10/72906.htm
————————————– 分割线 ————————————–
二、ZooKeeper 做独立服务器运行
创建了配置文件后,就可以启动 ZooKeeper 了。
$bin/zkServer.sh start
JMXenabled by default
Usingconfig: /home/chuser/zookeeper/bin/../conf/zoo.cfg
Startingzookeeper … STARTED
ZooKeeper 使用 Log4J 记录日志消息,你可以从控制台或日志输出文件来查看日志,具体取决于 Log4J 的配置。
三、ZooKeeper 客户端连接
客户端连接到 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
更多详情见请继续阅读下一页的精彩内容 :http://www.linuxidc.com/Linux/2014-05/102190p2.htm