概述
一,安装
第一步:zookeeper安装
1 下载 wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.10/zookeeper-3.4.10.tar.gz
2 解压 zookeeper-3.4.10.tar.gz
进入 zookeeper-3.4.10/conf目录,把zoo_sample.cfg 配置文件改名为 zoo.cfg
因为zookeeper启动默认加载的配置文件为zoo.cfg
3. 启动服务,与客户端
进入zookeeper-3.4.10/bin 目录
[root@localhost bin]# sh zkServer.sh start //启动
[root@localhost bin]# sh zkServer.sh stop //关闭
[root@localhost bin]# sh zkCli.sh //连接服务端
第二步,集群相关配置
安装好2n+1台虚拟机. 如 分别在三台虚拟机上安装好,如192.168.1.128,192.168.1.129,192.168.1.130
1.在/conf/zoo.cfg 添加,三台虚拟机上保持一致
server.1=192.168.1.128:2888:3888
server.2=192.168.1.129:2888:3888
server.3=192.168.1.130:2888:3888
2.zoo.cfg配置默认的数据存储在 dataDir=/tmp/zookeeper
在/tmp/zookeeper 目录中新建文件 myid
[root@localhost conf]# vim /tmp/zookeeper/myid
192.168.1.128中 myid的内容 为 1, 192.168.1.129中 myid的内容 为 2, 192.168.1.130中 myid的内容 为 3
与zoo.cfg 中保持一致
3.重新启动三台虚拟机的zookeeper.
[root@localhost bin]# tail -fn 100 zookeeper.out //查看日记
如果2888,3888两个端口没有打开的话会报错。
[root@localhost bin]# systemctl stop firewalld // 测试的话直接 关掉防火墙
4.查看主从,首次启动的时候,一般是先开启的为主,后开的为从.后面如果leader挂掉了,则通过leader选举机制重新选取
[root@localhost bin]# sh zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /data/program/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: leader
[root@localhost bin]# sh zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /data/program/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: follower
二,zookeeper的基本操作
随便打开客户端 192.168.1.128,创建一个key,在其它几台集群机器上可以看到添加了相应的key
[root@localhost bin]# sh zkCli.sh
[zk: localhost:2181(CONNECTED) 4] create /test1 a
[zk: localhost:2181(CONNECTED) 5] ls /
[zookeeper, test1]
1.创建节点 create [-s] [-e] path data acl
//同一节点下不能创建重复的节点
[zk: localhost:2181(CONNECTED) 19] create /lock v
Created /lock
[zk: localhost:2181(CONNECTED) 20] create /lock v
Node already exists: /lock
2.创建有序节点
[zk: localhost:2181(CONNECTED) 28] create -s /lock/ v
Created /lock/0000000000
[zk: localhost:2181(CONNECTED) 29] create -s /lock/ v
Created /lock/0000000001
[zk: localhost:2181(CONNECTED) 30] create -s /lock/ v
Created /lock/0000000002
[zk: localhost:2181(CONNECTED) 31] create -s /lock/ v
Created /lock/0000000003
[zk: localhost:2181(CONNECTED) 32] create -s /lock/ v
Created /lock/0000000004
[zk: localhost:2181(CONNECTED) 34] ls /lock
[0000000001, 0000000000, 0000000003, 0000000002, 0000000004]
[zk: localhost:2181(CONNECTED) 35]
3.创建临时节点
[zk: localhost:2181(CONNECTED) 0] create -e /temp a
Created /temp
//退出重新进入,/temp节点被删除了
[zk: localhost:2181(CONNECTED) 3] quit
Quitting...
2019-09-29 15:30:44,083 [myid:] - INFO [main:ZooKeeper@684] - Session: 0x16d7bc6e3490002 closed
2019-09-29 15:30:44,089 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@519] - EventThread shut down for session: 0x16d7bc6e3490002
[root@localhost bin]# sh zkCli.sh
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper, lock]
4.删除节点
//存在子节点时不能删除,须把子节点先删除
[zk: localhost:2181(CONNECTED) 47] delete /lock
Node not empty: /lock
[zk: localhost:2181(CONNECTED) 48] delete /lock/0000000004
[zk: localhost:2181(CONNECTED) 49] ls /lock
[0000000001, 0000000000, 0000000003, 0000000002]
5.修改
//修改
[zk: localhost:2181(CONNECTED) 13] set /tem a001
cZxid = 0x20
ctime = Sun Sep 29 15:44:08 CST 2019
mZxid = 0x21
mtime = Sun Sep 29 15:58:28 CST 2019
pZxid = 0x20
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
//查看
[zk: localhost:2181(CONNECTED) 14] get /tem
a001
cZxid = 0x20
ctime = Sun Sep 29 15:44:08 CST 2019
mZxid = 0x21
mtime = Sun Sep 29 15:58:28 CST 2019
pZxid = 0x20
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
6.watch机制 get path [watch],监听事件被触发一次后即失效
//watch /tem节点
[zk: localhost:2181(CONNECTED) 18] get /tem true
a001
cZxid = 0x20
ctime = Sun Sep 29 15:44:08 CST 2019
mZxid = 0x21
mtime = Sun Sep 29 15:58:28 CST 2019
pZxid = 0x20
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
[zk: localhost:2181(CONNECTED) 19]
[zk: localhost:2181(CONNECTED) 19]
[zk: localhost:2181(CONNECTED) 19]
[zk: localhost:2181(CONNECTED) 19]
//当/tmp节点被修改时,会触发
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/tem
三,应用
1,负载均衡
- 拿到目标服务下的所有地址
- 动态感知
2,分布式锁(有序节点,watch机制)
3,全局ID(基于有序节点实现)
最后
以上就是孝顺流沙为你收集整理的linux中zookeeper-3.4.10安装,集群的全部内容,希望文章能够帮你解决linux中zookeeper-3.4.10安装,集群所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复