zookeeper协调分布式应用系统

介绍

zookeeper是一个协调分布式应用系统,zookeeper分为两种运行模式,一种是独立模式,一种是复制模式

安装

下载

zookeeper-3.5.0-alpha.tar.gz

解压后,创建配置文件conf/zoo.cfg

tickTime=2000   #这个单位时间是以毫秒计算,用于心跳时间,且最小会话超时时间为2秒
dataDir=/var/lib/zookeeper  #存储到内存中的数据快照,这个日志更新到数据库
clientPort=2181  # 监听客户端连接的端口号

启动服务

bin/zkServer.sh start

连接到服务端

bin/zkCli.sh -server 127.0.0.1:2181

 服务端连接后基本操作

[zkshell: 0] help
ZooKeeper host:port cmd args
        get path [watch]
        ls path [watch]
        set path data [version]
        delquota [-n|-b] path
        quit
        printwatches on|off
        create path data acl
        stat path [watch]
        listquota path
        history
        setAcl path acl
        getAcl path
        sync path
        redo cmdno
        addauth scheme auth
        delete path [version]
        deleteall path
        setquota -n|-b val path
[zkshell: 8] ls /
[zookeeper]
[zkshell: 9] create /zk_test my_data
Created /zk_test
[zkshell: 11] ls /
[zookeeper, zk_test]
[zkshell: 12] get /zk_test
my_data
cZxid = 5
ctime = Fri Jun 05 13:57:06 PDT 2009
mZxid = 5
mtime = Fri Jun 05 13:57:06 PDT 2009
pZxid = 5
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0
dataLength = 7
numChildren = 0
[zkshell: 14] set /zk_test junk
cZxid = 5
ctime = Fri Jun 05 13:57:06 PDT 2009
mZxid = 6
mtime = Fri Jun 05 14:01:52 PDT 2009
pZxid = 5
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0
dataLength = 4
numChildren = 0
[zkshell: 15] get /zk_test
junk
cZxid = 5
ctime = Fri Jun 05 13:57:06 PDT 2009
mZxid = 6
mtime = Fri Jun 05 14:01:52 PDT 2009
pZxid = 5
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0
dataLength = 4
numChildren = 0
[zkshell: 16] delete /zk_test
[zkshell: 17] ls /
[zookeeper]
[zkshell: 18]

将独立模式更改为复制模式

tickTime=2000
dataDir=/var/lib/zookeeper
clientPort=2181
initLimit=5  # 这个超时时间是限制连接到主的zoo服务器的超时时间
syncLimit=2  # 这个时间是定义多长时间从主服务器同步数据
server.1=zoo1:2888:3888
server.2=zoo2:2888:3888
server.3=zoo3:2888:3888
note the two port numbers after each server name: " 2888" and "3888". Peers use the former port to connect to other peers. Such a connection is necessary so that peers can communicate, for example, to agree upon the order of updates. More specifically, a ZooKeeper server uses this port to connect followers to the leader. When a new leader arises, a follower opens a TCP connection to the leader using this port. Because the default leader election also uses TCP, we currently require another port for leader election. This is the second port in the server entry.


上面的zoo1,zoo2,zoo3对应的是三台主机名,通过dns或hosts可以解析到,也可以直接使用ip替换

注意,如果你想在一台机器上实现这个复制模式,需要复制多份你想模拟到多台主机协调模式,然后修改配置到数据存放目录,及不同的端口

If you want to test multiple servers on a single machine, specify the servername as localhost with unique quorum & leader election ports (i.e. 2888:3888, 2889:3889, 2890:3890 in the example above) for each server.X in that server's config file. Of course separate dataDirs and distinct clientPorts are also necessary (in the above replicated example, running on a single localhost, you would still have three config files).

更多细节,可参考http://zookeeper.apache.org/doc/r3.5.0-alpha/zookeeperStarted.html#sc_Prerequisites

http://zookeeper.apache.org/doc/r3.5.0-alpha/index.html

posted @ 2016-07-01 00:39  曾春云  阅读(137)  评论(0)    收藏  举报