1.linux安装包
导入zookeeper-3.4.11.tar.gz 到opt目录下
2.进入到opt目录下 cd /opt
3.解压tar -zxvf zookeeper-3.4.11.tar.gz
4.在opt下创建一个文件夹myzookeeper
5.将解压后的文件拷贝到myzookeeper文件夹中,rf为递归拷贝 cp -rf zookeeper-3.4.11 /opt/myzookeeper/
6.进入到conf 目录下cd zookeeper-3.4.11/conf ,cp zoo_sample.cfg zoo.cfg 拷贝配置文件,养成习惯。
6.启动 进入到拷贝后的文件夹下的bin目录下,cd /opt/myzookeeper/zookeeper-3.4.11/bin
7.执行启动命令 ll 查询所有的可执行文件,这里需要启动服务 ./zkServer.sh start(注意前面的./)
8.看启动是否成功 echo ruok | nc 127.0.0.1 2181 (这个zookeeper的默认端口为2181),若回应为imok,则为启动成功
(也可查看状态./zkServer.sh status)
9.客户端连接 ./zkCli.sh(注意前面的./)
[root@liuyuan bin]# ./zkCli.sh
Connecting to localhost:2181
2019-08-03 20:01:53,970 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.11-37e277162d567b55a07d1755f0b31c32e93c01a0, built on 11/01/2017 18:06 GMT
2019-08-03 20:01:53,982 [myid:] - INFO [main:Environment@100] - Client environment:host.name=liuyuan
2019-08-03 20:01:53,982 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_131
2019-08-03 20:01:53,997 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2019-08-03 20:01:53,998 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.131-11.b12.el7.x86_64/jre
2019-08-03 20:01:53,998 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/opt/myzookeeper/zookeeper-3.4.11/bin/../build/classes:/opt/myzookeeper/zookeeper-3.4.11/bin/../build/lib/*.jar:/opt/myzookeeper/zookeeper-3.4.11/bin/../lib/slf4j-log4j12-1.6.1.jar:/opt/myzookeeper/zookeeper-3.4.11/bin/../lib/slf4j-api-1.6.1.jar:/opt/myzookeeper/zookeeper-3.4.11/bin/../lib/netty-3.10.5.Final.jar:/opt/myzookeeper/zookeeper-3.4.11/bin/../lib/log4j-1.2.16.jar:/opt/myzookeeper/zookeeper-3.4.11/bin/../lib/jline-0.9.94.jar:/opt/myzookeeper/zookeeper-3.4.11/bin/../lib/audience-annotations-0.5.0.jar:/opt/myzookeeper/zookeeper-3.4.11/bin/../zookeeper-3.4.11.jar:/opt/myzookeeper/zookeeper-3.4.11/bin/../src/java/lib/*.jar:/opt/myzookeeper/zookeeper-3.4.11/bin/../conf:
2019-08-03 20:01:53,998 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2019-08-03 20:01:53,998 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2019-08-03 20:01:53,999 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=
2019-08-03 20:01:53,999 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
2019-08-03 20:01:53,999 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
2019-08-03 20:01:53,999 [myid:] - INFO [main:Environment@100] - Client environment:os.version=3.10.0-693.el7.x86_64
2019-08-03 20:01:54,000 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root
2019-08-03 20:01:54,000 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root
2019-08-03 20:01:54,000 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/opt/myzookeeper/zookeeper-3.4.11/bin
2019-08-03 20:01:54,008 [myid:] - INFO [main:ZooKeeper@441] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@42110406
Welcome to ZooKeeper!
2019-08-03 20:01:54,072 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1035] - Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2019-08-03 20:01:54,155 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@877] - Socket connection established to localhost/127.0.0.1:2181, initiating session
2019-08-03 20:01:54,185 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1302] - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x100001903b00000, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0]
10.退出quit
11.接下来操作一下
创建节点,节点由path+data+stat组成
rmr递归删除,delete为普通删除,路径下面不能嵌套路径
这是单机的接下来是集群版的,这里配置了三台服务器
这个和redis集群是有区别的,形式是主从形式,不过实现的效果却是集群的,所以从效果来讲,这属于集群,实际上也是这么叫的,这个会对第一台zookeeper服务作为Leader,也就是master 之后的所有服务器为从服务器Follower,也就是slave(貌似人权原因,这个名字不太好),zookeeper中不论主还是从都保持有所有的数据的,这点和redis集群有区别,
redis集群所有的主或者所有的从服务器共同组成了所有的数据,并且zookeeper主要是管理服务的,不存在并发问题。而redis则不同。
1.递归拷贝
zookeeper-3.4.9.tar.gz解压后拷贝到/myzookeeper目录下并重新名为zk01,再复制zk01形成zk02、zk03,共计3份
2.进入zk01/02/03分别新建文件夹mydata 和mylog
3.编辑zoo.cfg
分别进入zk01-zk03各自的conf文件夹,编辑zoo.cfg
设置自己的数据和log路径
dataDir=/opt/myzookeeper/zk01/mydata
dataLogDir=/opt/myzookeeper/zk01/mylog
# 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/myzookeeper/zk01/mydata
dataLogDir=/opt/myzookeeper/zk01/mylog
# the port at which the clients will connect
clientPort=2191
server.3=127.0.0.1:2993:3993
server.1=127.0.0.1:2991:3991
server.2=127.0.0.1:2992:3992
# 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
以上需要改对应的才可以,这个要特别注意
在各自mydata下面创建myid的文件,在里面写入server的数字
分别启动三个服务器,到对应的解压包下的bin下执行
./zkCli.sh -server 127.0.0.1:2191
./zkCli.sh -server 127.0.0.1:2192
./zkCli.sh -server 127.0.0.1:2193
2991/2992/2993任意用客户端链接一台,会发现只需要有一个改变了,整个集群的内容自动一致性同步。
在zk02机器上可以读的到,再在上面修改
成功