什么是Zookeeper
Zookeeper是一个高性能的分布式应用协调服务的框架。Zookeeper=Zoo+keeper,中文直译是动物园的看守者。
为什么要使用Zookeeper
Zookeeper可以用于解决很多分布式系统遇到的问题,比如分布式锁,分布式协调,分布式消息队列等。
安装Zookeeper
在官网下载最新的Zookeeper稳定版,http://zookeeper.apache.org/releases.html#download,将下载的文件存放在本地目录及安装完毕,本文将Zookeeper安装在/Developer/java/project目录。本文下载的是zookeeper-3.4.9版本。
单机模式启动Zookeeper
下载完之后,将conf目录下的zoo_sample.cfg文件重命名成zoo.cfg文件。这个文件存放着Zookeeper的所有配置信息。
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/tmp/zookeeper
clientPort=2181
maxClientCnxns=60
autopurge.snapRetainCount=3
autopurge.purgeInterval=1
- tickTime:客户端和服务器端维持心跳的时间,单位为毫秒。
- initLimit:
- syncLimit:
- dataDir:用来存放快照数据,官方建议不要使用/tmp目录,可以使用/tmp/zookeeper。
- clientPort;启动Zookeeper服务的默认端口。
- maxClientCnxns:最大客户端连接数。如果客户端的数量超过了指定的数量,则新连接的客户端会抛出java.io.IOException: Connection reset by peer异常。
- autopurge.snapRetainCount:
- autopurge.purgeInterval:
通过zkServer.sh start命令启动Zookeeper,启动后默认端口是2181,启动成功后命令行会回显已经启动成功的标示STARTED。
tengfeifangtf:bin tengfei.fangtf$ sh /Developer/java/project/zookeeper-3.4.9/bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /Developer/java/project/zookeeper-3.4.9/bin/../conf/zoo.cfg
-n Starting zookeeper ...
STARTED
通过命令行调用Zookeeper
连接Zookeeper
通过如下命令连接Zookeeper服务器端。如果服务器端不存在,则会抛出java.net.ConnectException: Connection refused异常
tengfeifangtf:bin tengfei.fangtf$ sh /Developer/java/project/zookeeper-3.4.9/bin/zkCli.sh -server 127.0.0.1:2181
Connecting to 127.0.0.1:2181
2016-09-17 22:02:00,703 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.9-1757313, built on 08/23/2016 06:50 GMT
2016-09-17 22:02:00,707 [myid:] - INFO [main:Environment@100] - Client environment:host.name=192.168.100.224
2016-09-17 22:02:00,707 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.6.0_65
2016-09-17 22:02:00,710 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Apple Inc.
2016-09-17 22:02:00,710 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
2016-09-17 22:02:00,710 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/Developer/java/project/zookeeper-3.4.9/bin/../build/classes:/Developer/java/project/zookeeper-3.4.9/bin/../build/lib/*.jar:/Developer/java/project/zookeeper-3.4.9/bin/../lib/slf4j-log4j12-1.6.1.jar:/Developer/java/project/zookeeper-3.4.9/bin/../lib/slf4j-api-1.6.1.jar:/Developer/java/project/zookeeper-3.4.9/bin/../lib/netty-3.10.5.Final.jar:/Developer/java/project/zookeeper-3.4.9/bin/../lib/log4j-1.2.16.jar:/Developer/java/project/zookeeper-3.4.9/bin/../lib/jline-0.9.94.jar:/Developer/java/project/zookeeper-3.4.9/bin/../zookeeper-3.4.9.jar:/Developer/java/project/zookeeper-3.4.9/bin/../src/java/lib/*.jar:/Developer/java/project/zookeeper-3.4.9/bin/../conf:
2016-09-17 22:02:00,711 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=.:/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java
2016-09-17 22:02:00,711 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/var/folders/6s/z6xt4ypn387239k7q1ty_rkc0000gn/T/
2016-09-17 22:02:00,713 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>
2016-09-17 22:02:00,713 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Mac OS X
2016-09-17 22:02:00,714 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=x86_64
2016-09-17 22:02:00,714 [myid:] - INFO [main:Environment@100] - Client environment:os.version=10.11.6
2016-09-17 22:02:00,714 [myid:] - INFO [main:Environment@100] - Client environment:user.name=tengfei.fangtf
2016-09-17 22:02:00,714 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/Users/tengfei
2016-09-17 22:02:00,714 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/Developer/java/project/zookeeper-3.4.9/bin
2016-09-17 22:02:00,716 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@3a21b220
Welcome to ZooKeeper!
2016-09-17 22:02:00,754 [myid:] - INFO [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (java.lang.SecurityException: Unable to locate a login configuration)
JLine support is enabled
2016-09-17 22:02:00,775 [myid:] - INFO [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@876] - Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
[zk: 127.0.0.1:2181(CONNECTING) 0] 2016-09-17 22:02:01,075 [myid:] - INFO [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x157386e843d0004, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
连接成功后会进入Zookeeper交互式窗口,命令行左边显示如下,然后你就可以使用Zookeeper提供的命令来创建,修改和删除节点。
[zk: 127.0.0.1:2181(CONNECTED) 0]
创建节点
使用create参数创建节点,第一个参数是节点路径,第二个参数是节点的数据。
[zk: 127.0.0.1:2181(CONNECTED) 1] create /dmcNode data
Created /dmcNode
注意:节点路径是大小写敏感的。
查看节点
使用get命令查询节点的数据,会显示节点的数据长度,数据的版本
[zk: 127.0.0.1:2181(CONNECTED) 2] get /dmcNode
data
cZxid = 0x54
ctime = Sat Sep 17 22:07:49 CST 2016
mZxid = 0x54
mtime = Sat Sep 17 22:07:49 CST 2016
pZxid = 0x54
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
通过get命令我们可以看到Zookeeper节点的数据结构
- cxxid:当前节点事务的xxid。
- ctime:当前节点的创建时间。
- mxxid: 当前节点最近修改的xxid。
- pZxid : 子节点的最后版本
- cversion:这个节点的子节点变化次数,创建一个子节点会加1。
- dataVersion:数据的版本,每次修改会加一。
- aclVersion:这个节点的ACL变化次数。
- ephemeralOwner :如果这个节点是临时节点,表示创建者的会话id。如果不是临时节点,这个值是0。
- dataLength:这个节点存放的数据长度。
- numChildren:这个节点的子节点个数。
- mtime:这个节点的最后修改时间。
这些属性可以参考《Zookeeper官方文档》Zookeeper tatStructure章节https://zookeeper.apache.org/doc/r3.4.6/zookeeperProgrammers.html#sc_zkStatStructure
修改节点
可以发现dataVersion从刚才的0变成了1,数据长度也变化了。
[zk: 127.0.0.1:2181(CONNECTED) 3] set /dmcNode data1
cZxid = 0x54
ctime = Sat Sep 17 22:07:49 CST 2016
mZxid = 0x55
mtime = Sat Sep 17 22:10:46 CST 2016
pZxid = 0x54
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 5
numChildren = 0
删除节点
通过delete [path]命令删除节点
[zk: 127.0.0.1:2181(CONNECTED) 12] delete /dmcNode
help命令
我们通过help命令可以看下Zookeeper提供哪些操作命令。
[zk: 127.0.0.1:2181(CONNECTED) 0] help
ZooKeeper -server host:port cmd args
connect host:port
get path [watch]
ls path [watch]
set path data [version]
rmr path
delquota [-n|-b] path
quit
printwatches on|off
create [-s] [-e] path data acl
stat path [watch]
close
ls2 path [watch]
history
listquota path
setAcl path acl
getAcl path
sync path
redo cmdno
addauth scheme auth
delete path [version]
setquota -n|-b val path
- quit:退出连接。
- history:查看当前连接历史执行命令。
关闭Zookeeper
如果先关闭了Zookeeper服务端,而客户端没有关闭,则客户端会不断的重连服务器端,并抛出异常closing socket connection and attempting reconnect。
sh /Developer/java/project/zookeeper-3.4.9/bin/zkServer.sh stop
通过Java API调用Zookeeper
1:引入POM
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.7</version>
</dependency>
2:Java测试代码
演示了ZooKeeper节点的创建,修改和删除操作。
import java.io.IOException;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
/**
*
* @author tengfei.fangtf
* @version $Id: TestZookeeper.java, v 0.1 Sep 14, 2016 5:05:07 PM tengfei.fangtf Exp $
*/
public class TestZookeeper {
public static void main(String[] args) throws IOException, InterruptedException,
KeeperException {
//创建ZooKeeper实例
ZooKeeper zk = new ZooKeeper("127.0.0.1:2181", 100, null);
String path = "/dmc";
//创建一个节点,模式是PERSISTENT
zk.create(path, "1".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println("创建节点" + path + ",数据为:" + new String(zk.getData(path, null, null)));
//修改节点数据
zk.setData(path, "2".getBytes(), -1);
System.out.println("修改节点" + path + ",数据为:" + new String(zk.getData(path, null, null)));
//删除一个节点
System.out.println(zk.exists(path, null));
zk.delete(path, -1);
//节点是否存在
System.out.println(zk.exists(path, null));
}
}
输出的结果如下:
创建节点/dmc,数据为:1
修改节点/dmc,数据为:2
78,79,1474120607561,1474120607575,1,0,0,0,1,0,78
null
参考资料
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/114196.html