玩手机游戏zookeeper 观察者模式时第一个模...

ZHOUCHAOQIANG
首先需要下载zookeeper的tar包,地址为,然后再linux中解压并编译tar包。# tar-xvzf zookeeper-3.4.5.tar.gz如果你是使用的是window操作系统的话,需要找解压缩工具来解压上述jar包,并且需要配置JDK的java开发环境,要求JDK在1.6以上在解压完的目录中,bin目录包含了一些命令的脚本,例如.sh结尾的是在linux中执行的脚本命令,.cmd结尾的是在window中执行的命令的脚本的信息。Lib目录中包含了java中需要的jar操作的类库文件第一个zookeeper会话下面在单机模式中创建zookeeper的回话,我们会利用zkServer和zkCli的命令工具来完成我们的操作。有经验的管理员也常用他们来调试和管理zookeeper。进入到conf目录,重新命名zoo_sample.cfg文件# mvconf/zoo_sample.cfg conf/zoo.cfg下面的操作是可选的,把zookeeper的数据存储目录与zookeeper***目录隔离开来dataDir=/users/me/zookeeper最后来启动zookeeper服务# bin/zkServer.sh start
JMX enabled by default
Using config: ../conf/zoo.cfg
Starting zookeeper ... STARTED
上面的命令最终会是zookeeper服务运行在后台,下面的命令会使zookeeper运行在前台程序中,并且从输出中我们也可以看到一些重要输出的信息。# bin/zkServer.shstart-foreground上述命令能够让我们看到更多的输出信息下面让我们来开启一个客户端#bin/zkCli.sh&some omitted output&
12:07:23,545 [myid:] - INFO
[main:ZooKeeper@438] -
Initiating client connection, connectString=localhost:2181
sessionTimeout=30000 watcher=org.apache.zookeeper.
ZooKeeperMain$MyWatcher@2c641e9a
Welcome to ZooKeeper!
12:07:23,702 [myid:] - INFO
[main-SendThread
Getting Started with ZooKeeper
27(localhost:2181):ClientCnxn$SendThread@966] - Opening
socket connection to server localhost/127.0.0.1:2181.
Will not attempt to authenticate using SASL (Unable to
locate a login configuration)
JLine support is enabled
12:07:23,717 [myid:] - INFO
[main-SendThread
(localhost:2181):ClientCnxn$SendThread@849] - Socket
connection established to localhost/127.0.0.1:2181, initiating
session [zk: localhost:2181(CONNECTING) 0]
12:07:23,987 [myid:] - INFO
[main-SendThread
(localhost:2181):ClientCnxn$SendThread@1207] - Session
establishment complete on server localhost/127.0.0.1:2181,
sessionid = 0x13b6fe376cd0000, negotiated timeout = 30000
WatchedEvent state:SyncConnected type:None path:null
上面的执行过程为1.&&&&&&客户端开始建立一个回话2.&&&&&&客户端尝试连接127.0.0.1:21813.&&&&&&客户端连接成功,服务端开始初始化回话状态4.&&&&&&回话状态初始化成功5.&&&&&&服务端发功一个同步的事件给客户端下面来让我们执行一些命令操作,来玩一下zookeeper[zk: localhost:2181(CONNECTED) 0][zk: localhost:2181(CONNECTED) 0] ls /[zookeeper][zk: localhost:2181(CONNECTED) 1] create/workers &&Created /workers[zk: localhost:2181(CONNECTED) 2] ls /[workers, zookeeper][zk: localhost:2181(CONNECTED) 3]&[zk: localhost:2181(CONNECTED) 3] delete/workers[zk: localhost:2181(CONNECTED) 4] ls /[zookeeper][zk: localhost:2181(CONNECTED) 5] quitQuitting... 12:28:18,200 [myid:] - INFO& [main-EventThread:ClientCnxn$EventThread@509] - EventThread shut down 12:28:18,200 [myid:] - INFO& [main:ZooKeeper@684] - Session:0x13b6fe376cd0000 closed# bin/zkServer.sh stopJMX enabled by defaultUsing config: ../conf/zoo.cfgStopping zookeeper ... STOPPED#会话的声明周期回话的声明周期贯穿它的起始与结束,整个声明周期状态,如下图所示Zookeeper集群模式上面只是一个独立的模式,下面来搭建一个集群的模式。还是一个一个主机为例,在一个主机上搭建三个不同的zookeeper服务。为了实现集群模式操作。参考一下文章:&Zookeeper中的锁在zookeeper中有多种不同类型的锁,比如读写锁,全局锁等。并且有很多方式去实现zookeeper中的锁,下面我们将讨论一种最简单的,去说明应用中是如何来利用zookeeper的。试想,我们有一个应用,并且该应用中有n个进程都在尽力的争取锁,考虑到zookeeper不是直接的暴漏它的语法,因此我们需要用zookeeper的接口去操作节点和实现锁的机制。为了获取锁,每个进程都尽力的去创建节点,如果获得了锁,就会创建成功。这种情况下,一种潜在的问题是,p进程如果死掉了,但是并没有释放锁的话,那么其他进程将没有机会获得到锁,此时系统将处于一种死锁的状态,为了避免这种情况,因此我们需要创建临时节点。只要该节点存在,那么其他的进程将会创建失败。那么其他进程将会监视该节点,一点该节点删除掉,那么他们就有机会获取到锁。
阅读排行榜zookeeper replicated模式 - 博客频道 - CSDN.NET
Dean Chen的专栏
分类:zookeeper
standalone适合开发,因为是单机,因此不能作为产品环境使用。先准备三台Ubuntu虚拟机,IP地址分别为:192.168.1.70 zka
192.168.1.71 zkb
192.168.1.72 zkc
然后在zoo.cfg文件中如下配置,三台相同:# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/opt/zk_data
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
maxClientCnxns=60
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to &0& to disable auto purge feature
#autopurge.purgeInterval=1
server.1=zka:
server.2=zkb:
server.3=zkc:注意最下面三行,zka/zkb/zkc是hostname,因为前面在hosts都已经配置了相应的IP地址,因此不会找不到主机。2888端口是zookeeper服务相互通信使用的,3888端口是zookeeper服务选举使用的。server.后面的1,2和3需要做些配置,在每台服务器的dataDir目录下(我这里是/opt/zk_data),创建myid文件,里面包含对应的1,2和3.然后分别启动三台服务器上的zookeeper server。bin/zkServer.sh start由于我没有设置日志目录,所以可以看一下默认日志文件zookeeper.out, 没有错误,一切正常。 15:43:00,685 [myid:1] - INFO
[QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Environment@100] - Server environment:java.library.path=/usr/java/pa\
ckages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
15:43:00,685 [myid:1] - INFO
[QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Environment@100] - Server environment:java.io.tmpdir=/tmp
15:43:00,685 [myid:1] - INFO
[QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Environment@100] - Server piler=&NA&
15:43:00,685 [myid:1] - INFO
[QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Environment@100] - Server environment:os.name=Linux
15:43:00,686 [myid:1] - INFO
[QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Environment@100] - Server environment:os.arch=amd64
15:43:00,686 [myid:1] - INFO
[QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Environment@100] - Server environment:os.version=3.2.0-59-virtual
15:43:00,686 [myid:1] - INFO
[QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Environment@100] - Server environment:user.name=root
15:43:00,686 [myid:1] - INFO
[QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Environment@100] - Server environment:user.home=/root
15:43:00,686 [myid:1] - INFO
[QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Environment@100] - Server environment:user.dir=/opt/zookeeper-3.4.6
15:43:00,687 [myid:1] - INFO
[QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:ZooKeeperServer@162] - Created server with tickTime 2000 minSessionT\
imeout 4000 maxSessionTimeout 40000 datadir /opt/zk_data/version-2 snapdir /opt/zk_data/version-2
15:43:00,687 [myid:1] - INFO
[QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Follower@63] - FOLLOWING - LEADER ELECTION TOOK - 34
15:43:00,738 [myid:1] - INFO
[QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Learner@326] - Getting a snapshot from leader
15:43:00,745 [myid:1] - INFO
[QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:FileTxnSnapLog@240] - Snapshotting: 0x to /opt/zk_data/vers\
ion-2/snapshot.
sheismylife
排名:第36名
(17)(1)(5)(4)(9)(34)(2)(35)(6)(9)(15)(13)(1)(7)(96)(9)(30)(17)(17)(19)(21)(7)(12)(5)(0)(7)(17)(10)(1)(41)(4)(4)(63)(0)(8)(3)(24)(2)(2)(8)(9)(5)(17)(12)(64)(35)(51)(7)(2)(6)(7)(19)(1)(11)(6)(8)(3)(18)(4)(15)(4)(10)(13)(2)(23)(22)(18)(5)(24)(1)(3)(73)(1)(6)(5)(6)(3)(3)(1)(8)(3)(2)(2)(16)(4)(3)(1)(1)(1)(1)(8)(1)(3)(24)(26)(1)(6)(4)(4)(9)(5)(3)(4)(9)(6)(13)(2)(7)(2)(26)(13)(5)(1)(2)(1)(2)(6)
算法专家朋友的博客
清如许的博客

参考资料

 

随机推荐