zookeeper环境搭建学习
zookeeper介绍
zookeeper=文件系统+通知机制
1、Zookeeper:一个领导者(Leader),多个跟随者(Flower)组成的集群。
2、集群中只要有半数以上节点存活,Zookeeper集群就能正常服务。
3、全局数据一致, 每个server保存一份相同的数据副本,client无论连接到哪个server,数据都是一致的。
4、更新请求顺序进行,来自同一client的更新请求按其发送顺序依次执行。
5、数据更新原子性,一次数据更新要么成功,要么失败。
6、实时性,在一定时间范围内,client能读到最新的数据。
数据结构:
树形结构和unix文件系统类似,整体上可以看做一棵树,每个节点称作一个Znode,每一个Znode默认能够存储1MB的数据,每个Znode都可以通过其路径唯一标识。
本地环境,一共3台服务器
192.168.150.101
192.168.150.102
192.168.150.103
安装方法
先装java 1.8 jdk (略过)
官网下载地址:
https://zookeeper.apache.org/releases.html#download
tar xvf apache-zookeeper-3.5.8-bin.tar.gz -C /usr/local cd /usr/local mv apache-zookeeper-3.5.8-bin/ zookeeper cd /usr/local/zookeeper/conf mv zoo_sample.cfg zoo.cfg mkdir /usr/local/zookeeper/zkData vi zoo.cfg 修改数据文件保存目录 dataDir=/usr/local/zookeeper/zkData
启动zookeeper
[root@host101 zookeeper]# bin/zkServer.sh start /usr/bin/java ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg Starting zookeeper ... STARTED [root@host101 zookeeper]# jps 11809 Jps 11759 QuorumPeerMain ---多了这个进程
查看状态
[root@host101 zookeeper]# bin/zkServer.sh status /usr/bin/java ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Mode: standalone ---单机模式
启动客户端
[root@host101 zookeeper]# bin/zkCli.sh /usr/bin/java Connecting to localhost:2181 2020-11-26 23:30:33,401 [myid:] - INFO [main:Environment@109] - Client environment:zookeeper.version=3.5.8-f439ca583e70862c3068a1f2a7d4d068eec33315, built on 05/04/2020 15:07 GMT 2020-11-26 23:30:33,407 [myid:] - INFO [main:Environment@109] - Client environment:host.name=host101 2020-11-26 23:30:33,407 [myid:] - INFO [main:Environment@109] - Client environment:java.version=1.8.0_181 2020-11-26 23:30:33,421 [myid:] - INFO [main:Environment@109] - Client environment:java.vendor=Oracle Corporation 2020-11-26 23:30:33,421 [myid:] - INFO [main:Environment@109] - Client environment:java.home=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181-7.b13.el7.x86_64/jre 2020-11-26 23:30:33,421 [myid:] - INFO [main:Environment@109] - Client environment:java.class.path=/usr/local/zookeeper/bin/../zookeeper-server/target/classes:/usr/local/zookeeper/bin/../build/classes:/usr/local/zookeeper/bin/../zookeeper-server/target/lib/*.jar:/usr/local/zookeeper/bin/../build/lib/*.jar:/usr/local/zookeeper/bin/../lib/zookeeper-jute-3.5.8.jar:/usr/local/zookeeper/bin/../lib/zookeeper-3.5.8.jar:/usr/local/zookeeper/bin/../lib/slf4j-log4j12-1.7.25.jar:/usr/local/zookeeper/bin/../lib/slf4j-api-1.7.25.jar:/usr/local/zookeeper/bin/../lib/netty-transport-native-unix-common-4.1.48.Final.jar:/usr/local/zookeeper/bin/../lib/netty-transport-native-epoll-4.1.48.Final.jar:/usr/local/zookeeper/bin/../lib/netty-transport-4.1.48.Final.jar:/usr/local/zookeeper/bin/../lib/netty-resolver-4.1.48.Final.jar:/usr/local/zookeeper/bin/../lib/netty-handler-4.1.48.Final.jar:/usr/local/zookeeper/bin/../lib/netty-common-4.1.48.Final.jar:/usr/local/zookeeper/bin/../lib/netty-codec-4.1.48.Final.jar:/usr/local/zookeeper/bin/../lib/netty-buffer-4.1.48.Final.jar:/usr/local/zookeeper/bin/../lib/log4j-1.2.17.jar:/usr/local/zookeeper/bin/../lib/json-simple-1.1.1.jar:/usr/local/zookeeper/bin/../lib/jline-2.11.jar:/usr/local/zookeeper/bin/../lib/jetty-util-9.4.24.v20191120.jar:/usr/local/zookeeper/bin/../lib/jetty-servlet-9.4.24.v20191120.jar:/usr/local/zookeeper/bin/../lib/jetty-server-9.4.24.v20191120.jar:/usr/local/zookeeper/bin/../lib/jetty-security-9.4.24.v20191120.jar:/usr/local/zookeeper/bin/../lib/jetty-io-9.4.24.v20191120.jar:/usr/local/zookeeper/bin/../lib/jetty-http-9.4.24.v20191120.jar:/usr/local/zookeeper/bin/../lib/javax.servlet-api-3.1.0.jar:/usr/local/zookeeper/bin/../lib/jackson-databind-2.10.3.jar:/usr/local/zookeeper/bin/../lib/jackson-core-2.10.3.jar:/usr/local/zookeeper/bin/../lib/jackson-annotations-2.10.3.jar:/usr/local/zookeeper/bin/../lib/commons-cli-1.2.jar:/usr/local/zookeeper/bin/../lib/audience-annotations-0.5.0.jar:/usr/local/zookeeper/bin/../zookeeper-*.jar:/usr/local/zookeeper/bin/../zookeeper-server/src/main/resources/lib/*.jar:/usr/local/zookeeper/bin/../conf: 2020-11-26 23:30:33,422 [myid:] - INFO [main:Environment@109] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib 2020-11-26 23:30:33,422 [myid:] - INFO [main:Environment@109] - Client environment:java.io.tmpdir=/tmp 2020-11-26 23:30:33,422 [myid:] - INFO [main:Environment@109] - Client environment:java.compiler=<NA> 2020-11-26 23:30:33,422 [myid:] - INFO [main:Environment@109] - Client environment:os.name=Linux 2020-11-26 23:30:33,422 [myid:] - INFO [main:Environment@109] - Client environment:os.arch=amd64 2020-11-26 23:30:33,422 [myid:] - INFO [main:Environment@109] - Client environment:os.version=3.10.0-957.el7.x86_64 2020-11-26 23:30:33,423 [myid:] - INFO [main:Environment@109] - Client environment:user.name=root 2020-11-26 23:30:33,423 [myid:] - INFO [main:Environment@109] - Client environment:user.home=/root 2020-11-26 23:30:33,423 [myid:] - INFO [main:Environment@109] - Client environment:user.dir=/usr/local/zookeeper 2020-11-26 23:30:33,423 [myid:] - INFO [main:Environment@109] - Client environment:os.memory.free=24MB 2020-11-26 23:30:33,436 [myid:] - INFO [main:Environment@109] - Client environment:os.memory.max=247MB 2020-11-26 23:30:33,436 [myid:] - INFO [main:Environment@109] - Client environment:os.memory.total=29MB 2020-11-26 23:30:33,445 [myid:] - INFO [main:ZooKeeper@868] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@7e774085 2020-11-26 23:30:33,474 [myid:] - INFO [main:X509Util@79] - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation 2020-11-26 23:30:33,515 [myid:] - INFO [main:ClientCnxnSocket@237] - jute.maxbuffer value is 4194304 Bytes 2020-11-26 23:30:33,541 [myid:] - INFO [main:ClientCnxn@1653] - zookeeper.request.timeout value is 0. feature enabled= Welcome to ZooKeeper! 2020-11-26 23:30:33,602 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1112] - Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error) JLine support is enabled 2020-11-26 23:30:33,868 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@959] - Socket connection established, initiating session, client: /127.0.0.1:45378, server: localhost/127.0.0.1:2181 2020-11-26 23:30:33,930 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1394] - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x100002ff2310000, negotiated timeout = 30000 WATCHER:: WatchedEvent state:SyncConnected type:None path:null [zk: localhost:2181(CONNECTED) 0]
查看命令
[zk: localhost:2181(CONNECTED) 0] ls / [zookeeper] [zk: localhost:2181(CONNECTED) 1]
退出quit
[zk: localhost:2181(CONNECTED) 1] quit WATCHER:: WatchedEvent state:Closed type:None path:null 2020-11-26 23:31:44,737 [myid:] - INFO [main:ZooKeeper@1422] - Session: 0x100002ff2310000 closed 2020-11-26 23:31:44,738 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@524] - EventThread shut down for session: 0x100002ff2310000 [root@host101 zookeeper]#
停止zookeeper服务
[root@host101 zookeeper]# bin/zkServer.sh stop /usr/bin/java ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg Stopping zookeeper ... STOPPED [root@host101 zookeeper]# jps 12289 Jps [root@host101 zookeeper]#
配置文件说明
tickTime=2000
心跳时间
initLimit=10
集群启动时,最大延迟时间:initLimit*tickTime,
syncLimit=5
集群启动后,最大延迟时间:syncLimit*tickTime
dataDir=/usr/local/zookeeper/zkData
数据文件目录
clientPort=2181
端口号
节点类型:
持久型(Persistent):客户端和服务器连接断开后,创建的节点不删除。
1、持久化目录节点,客户端和zookeeper断开连接后,该节点依然存在。
2、持久化顺序编号目录节点,客户端和zookeeper断开连接后,该节点依然存在,只是zookeeper给该节点名称进行顺序编号。
说明,创建znode时设置顺序标识,znode名称后会附加一个值,顺序号是一个单调递增的计数器,由父节点维护。
短暂(Ephemeral):客户端和服务器连接断开后,创建的节点自己删除。
3、临时目录节点,客户端和zookeeper连接断开后,该节点被删除。
4、临时顺序编号目录节点,客户端和zookeeper断开连接后,该节点被删除,只是zookeeper给该节点名称进行顺序编号。
搭建集群
在102 103服务器分别参考之前的安装方法进行安装。
cd /usr/local/zookeeper/zkData
touch myid
vim myid
三台服务器分为写自己的ip地址后三位(101、102、103)
修改三台服务器zoo.cfg配置
server.101=192.168.150.101:2888:3888
server.102=192.168.150.102:2888:3888
server.103=192.168.150.103:2888:3888
参数格式:
server.A=B:C:D
A是一个数字,表示这是第几个服务器。
集群模式下配置一个文件myid,这个文件在dataDir目录下,这个文件里面有一个数据就是A的值,Zookeeper启动时读取此文件,拿到里面的数据与zoo.cfg里面的配置信息比较从而判断到底是哪个server。
B是这个服务器的ip地址;
C是这个服务器与集群中的Leader服务器交换信息的端口;
D是万一集群中的Leader服务器挂了,需要一个端口来重新进行选举,选出一个新的Leader,而这个端口就是用来执行选举时服务器相互通信的端口。
启动三台服务
[root@host103 zookeeper]# bin/zkServer.sh start /usr/bin/java ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg Starting zookeeper ... STARTED [root@host103 zookeeper]#
查看状态
[root@host103 zookeeper]# bin/zkServer.sh status /usr/bin/java ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Mode: leader [root@host103 zookeeper]#
客户端命令
help:显示所有操作命令
ls path [watch]:使用ls命令查看当前znode中所包含的内容
ls2 path [watch]:查看当前节点数据并能看到更新次数等数据
create:普通创建
-s 含有序列
-e 临时(重启或超时消失)
get path [watch]:获得节点的值
set:设置节点的具体值
stat:查看节点状态
delete:删除节点
rmr:递归删除节点
1、启动客户端
bin/zkCli.sh
2、显示所有操作命令 help
[zk: localhost:2181(CONNECTED) 5] help ZooKeeper -server host:port cmd args addauth scheme auth close config [-c] [-w] [-s] connect host:port create [-s] [-e] [-c] [-t ttl] path [data] [acl] delete [-v version] path deleteall path delquota [-n|-b] path get [-s] [-w] path getAcl [-s] path history listquota path ls [-s] [-w] [-R] path ls2 path [watch] printwatches on|off quit reconfig [-s] [-v version] [[-file path] | [-members serverID=host:port1:port2;port3[,...]*]] | [-add serverId=host:port1:port2;port3[,...]]* [-remove serverId[,...]*] redo cmdno removewatches path [-c|-d|-a] [-l] rmr path set [-s] [-v version] path data setAcl [-s] [-v version] [-R] path acl setquota -n|-b val path stat [-w] path sync path Command not found: Command not found help [zk: localhost:2181(CONNECTED) 6]
3、查看当前znode中所包含的内容 ls /
[zk: localhost:2181(CONNECTED) 6] ls / [zookeeper] [zk: localhost:2181(CONNECTED) 7]
4、查看当节点详细的数据 ls2 /
[zk: localhost:2181(CONNECTED) 7] ls2 / 'ls2' has been deprecated. Please use 'ls [-s] path' instead. [zookeeper] cZxid = 0x0 ctime = Thu Jan 01 08:00:00 CST 1970 mZxid = 0x0 mtime = Thu Jan 01 08:00:00 CST 1970 pZxid = 0x0 cversion = -1 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 0 numChildren = 1 [zk: localhost:2181(CONNECTED) 8]
5、创建两个数据节点
[zk: localhost:2181(CONNECTED) 8] create sanguo "jinlian" Path must start with / character [zk: localhost:2181(CONNECTED) 9] create /sanguo "jinlian" Created /sanguo [zk: localhost:2181(CONNECTED) 10] ls / [sanguo, zookeeper] [zk: localhost:2181(CONNECTED) 11] create /sanguo/shuguo "liubei" Created /sanguo/shuguo [zk: localhost:2181(CONNECTED) 12] ls /sanguo [shuguo]
6、获取节点的值
[zk: localhost:2181(CONNECTED) 13] get /sanguo/shuguo liubei
7、创建短暂节点
[zk: localhost:2181(CONNECTED) 14] create -e /sanguo/wuguo "zhouyu" Created /sanguo/wuguo [zk: localhost:2181(CONNECTED) 15] ls /sanguo [shuguo, wuguo] quit退出再进入,已经没有wuguo节点了 [zk: localhost:2181(CONNECTED) 0] ls /sanguo [shuguo]
8、创建带序号的节点
[zk: localhost:2181(CONNECTED) 1] create -s /sanguo/weiguo "caocao" Created /sanguo/weiguo0000000002 [zk: localhost:2181(CONNECTED) 2] ls /sanguo [shuguo, weiguo0000000002] [zk: localhost:2181(CONNECTED) 3] create -s /sanguo/weiguo "caocao" Created /sanguo/weiguo0000000003 [zk: localhost:2181(CONNECTED) 4] create -s /sanguo/weiguo "caocao" Created /sanguo/weiguo0000000004 [zk: localhost:2181(CONNECTED) 5] ls /sanguo [shuguo, weiguo0000000002, weiguo0000000003, weiguo0000000004]
9、修改节点的值
[zk: localhost:2181(CONNECTED) 6] set /sanguo/shuguo "diaochan" [zk: localhost:2181(CONNECTED) 8] get /sanguo/shuguo diaochan
10、节点的值变化监听
10.1在101上注册监控/sanguo节点数据变化。注册一次,仅有效一次 [zk: localhost:2181(CONNECTED) 3] get /sanguo watch 'get path [watch]' has been deprecated. Please use 'get [-s] [-w] path' instead. jinlian 103上去修改/sanguo的值 [zk: localhost:2181(CONNECTED) 9] set /sanguo "jingjing" 101上会提示如下 [zk: localhost:2181(CONNECTED) 3] get /sanguo watch 'get path [watch]' has been deprecated. Please use 'get [-s] [-w] path' instead. jinlian [zk: localhost:2181(CONNECTED) 4] WATCHER:: WatchedEvent state:SyncConnected type:NodeDataChanged path:/sanguo
11、节点的子节点变化监听(路径变化)
在101上注册监控/sanguo节点的子节点变化 101上执行 [zk: localhost:2181(CONNECTED) 4] ls /sanguo watch 'ls path [watch]' has been deprecated. Please use 'ls [-w] path' instead. [shuguo, weiguo0000000002, weiguo0000000003, weiguo0000000004] 103上执行 [zk: localhost:2181(CONNECTED) 10] create /sanguo/banzhang "banzhang" Created /sanguo/banzhang 101返回如下 [zk: localhost:2181(CONNECTED) 4] ls /sanguo watch 'ls path [watch]' has been deprecated. Please use 'ls [-w] path' instead. [shuguo, weiguo0000000002, weiguo0000000003, weiguo0000000004] [zk: localhost:2181(CONNECTED) 5] WATCHER:: WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/sanguo
12、删除节点
[zk: localhost:2181(CONNECTED) 6] ls /sanguo [shuguo, weiguo0000000002, weiguo0000000003, weiguo0000000004] [zk: localhost:2181(CONNECTED) 7]
13、递归删除节点
[zk: localhost:2181(CONNECTED) 7] rmr /sanguo The command 'rmr' has been deprecated. Please use 'deleteall' instead. [zk: localhost:2181(CONNECTED) 8] ls /sanguo Node does not exist: /sanguo [zk: localhost:2181(CONNECTED) 9]