笔记系列之zookeeper在单机模拟集群安装

0.目的

zookeeper单节点的搭建方法比较简单,在实际使用当中一般也是搭建集群使用。由于资源限制,不想安装太多虚拟机,也没有安装docker的话,那么在一台机器上也是可以模拟集群搭建的。

1.下载zookeeper并解压

官网下载就好,我这个人比较喜新厌旧,有最新的我都喜欢尝试最新的版本。解压完又如下文件:
zookeeper解压后内容

2. 编辑配置文件

首先进入到conf目录下面,我们会看到一个配置文件的示例文件zoo_sample.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=/tmp/zookeeper
# 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

## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true

具体配置的意思都有注释,我们先来写三个配置文件:
zoo_1.cfg

tickTime=2000
initLimit=10
syncLimit=5
dataDir=/tmp/zk/data1/
clientPort=2181

server.1=localhost:10086:8886
server.2=localhost:10087:8887
server.3=localhost:10088:8888

这里的内容需要解释一下:

  • dataDir
    这里是数据存放的目录,我把它指定到了/tmp目录下,然后自己创建了zk目录,在zk目录下又新建了一个data1目录,表示这里存放的是第一个节点的数据。
  • clientPort
    这里表示的是端口号,这里使用的是2181
  • server.1=localhost:10086:8888
    这里前面的server.1表示的是节点1,后面是ip地址加上端口10086,用于leader和follower之间的数据同步和其他通信; 后面的8888是用来进行leader节点选举端口,投票通信使用该端口。

再创建第二个配置文件
zoo_2.cfg

tickTime=2000
initLimit=10
syncLimit=5
dataDir=/tmp/zk/data2/
clientPort=2182

server.1=localhost:10086:8886
server.2=localhost:10087:8887
server.3=localhost:10088:8888

使用不同的端口就好了。

zoo_3.cfg

tickTime=2000
initLimit=10
syncLimit=5
dataDir=/tmp/zk/data3/
clientPort=2183

server.1=localhost:10086:8886
server.2=localhost:10087:8887
server.3=localhost:10088:8888

3.目录创建

我们在/tmp/zk/data1下面再创建一个文件myid,在里面填写唯一的id来表示这个节点,这里就简单写了1.
类似的,在/tmp/zk/data2目录创建myid写入2,在/tmp/zk/data3目录下创建myid,写入3。

4.启动

分别启动他们。由于在一台机器上启动,因此需要指定配置文件,使用的指令是

././zkServer.sh start  ../conf/zoo_1.cfg

依次启动三个端口的服务

././zkServer.sh start  ../conf/zoo_2.cfg
././zkServer.sh start  ../conf/zoo_3.cfg

这时候,不出意外的话,我们应该都会启动好了,那我们来看一下各自的状态好了:

yy@host-192-168-0-26:~/Downloads/apache-zookeeper-3.7.0-bin/bin$ sudo ./zkServer.sh status  ../conf/zoo_3.cfg 
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: ../conf/zoo_3.cfg
Client port found: 2183. Client address: localhost. Client SSL: false.
Mode: follower
yy@host-192-168-0-26:~/Downloads/apache-zookeeper-3.7.0-bin/bin$ sudo ./zkServer.sh status  ../conf/zoo_2.cfg 
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: ../conf/zoo_2.cfg
Client port found: 2182. Client address: localhost. Client SSL: false.
Mode: leader
yy@host-192-168-0-26:~/Downloads/apache-zookeeper-3.7.0-bin/bin$ sudo ./zkServer.sh status  ../conf/zoo_1.cfg 
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: ../conf/zoo_1.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower

从上面可以看到,节点2是leader节点,节点1和3是follower节点。(leader节点如何选举,那就先挖个坑,有需要就来填)

5. 客户端连接

使用客户端连接zookeeper

./zkCli.sh #连接本地的zookeeper服务器
./zkCli.sh -server ip:port #连接指定的服务器

这里我直接偷懒,不指定连接的服务器,默认选择的是第一个,连接成功后一堆详细信息:

2021-08-31 18:08:14,829 [myid:] - INFO  [main:Environment@98] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2021-08-31 18:08:14,830 [myid:] - INFO  [main:Environment@98] - Client environment:java.io.tmpdir=/tmp
2021-08-31 18:08:14,830 [myid:] - INFO  [main:Environment@98] - Client environment:java.compiler=<NA>
2021-08-31 18:08:14,830 [myid:] - INFO  [main:Environment@98] - Client environment:os.name=Linux
2021-08-31 18:08:14,830 [myid:] - INFO  [main:Environment@98] - Client environment:os.arch=amd64
2021-08-31 18:08:14,830 [myid:] - INFO  [main:Environment@98] - Client environment:os.version=4.15.0-48-generic
2021-08-31 18:08:14,830 [myid:] - INFO  [main:Environment@98] - Client environment:user.name=yayuan
2021-08-31 18:08:14,831 [myid:] - INFO  [main:Environment@98] - Client environment:user.home=/home/yy
2021-08-31 18:08:14,831 [myid:] - INFO  [main:Environment@98] - Client environment:user.dir=/home/yy/Downloads/apache-zookeeper-3.7.0-bin/bin
2021-08-31 18:08:14,831 [myid:] - INFO  [main:Environment@98] - Client environment:os.memory.free=232MB
2021-08-31 18:08:14,833 [myid:] - INFO  [main:Environment@98] - Client environment:os.memory.max=245MB
2021-08-31 18:08:14,833 [myid:] - INFO  [main:Environment@98] - Client environment:os.memory.total=245MB
2021-08-31 18:08:14,838 [myid:] - INFO  [main:ZooKeeper@637] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@4fccd51b
2021-08-31 18:08:14,842 [myid:] - INFO  [main:X509Util@77] - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
2021-08-31 18:08:14,849 [myid:] - INFO  [main:ClientCnxnSocket@239] - jute.maxbuffer value is 1048575 Bytes
2021-08-31 18:08:14,860 [myid:] - INFO  [main:ClientCnxn@1726] - zookeeper.request.timeout value is 0. feature enabled=false
Welcome to ZooKeeper!
2021-08-31 18:08:14,881 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1171] - Opening socket connection to server localhost/0:0:0:0:0:0:0:1:2181.
2021-08-31 18:08:14,881 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1173] - SASL config status: Will not attempt to authenticate using SASL (unknown error)
2021-08-31 18:08:14,889 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1005] - Socket connection established, initiating session, client: /0:0:0:0:0:0:0:1:53802, server: localhost/0:0:0:0:0:0:0:1:2181
JLine support is enabled
2021-08-31 18:08:14,925 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1438] - Session establishment complete on server localhost/0:0:0:0:0:0:0:1:2181, session id = 0x100761b51a00000, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0] 

5.1 创建节点

使用create创建节点,但是节点会分永久节点、临时节点和顺序节点:

create [-s][-e] path data acl
其中,-s或-e分别指定节点特性,顺序或临时节点,若不指定,则创建持久节点;acl用来进行权限控制。

5.1.1 创建顺序节点

[zk: localhost:2181(CONNECTED) 0] create -s /zk-sorted first
Created /zk-sorted0000000000

在根节点下创建了一个zk-sorted的节点,内容是first。但是在存储时,zookeeper加上了0000000000来加以区分。

5.1.2 创建临时节点

[zk: localhost:2181(CONNECTED) 3] create -e /zk-tmp tmp
Created /zk-tmp

在根节点下创建了一个zk-tmp的节点,内容是tmp。这个节点在断开连接后,载近来就没有了。只是临时存在的。

5.1.3 创建永久节点

[zk: localhost:2181(CONNECTED) 5] create /zk-per per
Created /zk-per

5.2 读取节点

读取节点一般有ls或者get

[zk: localhost:2181(CONNECTED) 6] ls /
[zk-per, zk-sorted0000000000, zk-tmp, zookeeper]

ls后面加上路径即可。

[zk: localhost:2181(CONNECTED) 7] get /zk-per
per

get同样也是在后面加上路径。
(鲁迅加门前有两棵树,一棵是枣树,另一棵也是枣树。手动狗头)

5.3更新节点

使用set进行更新

[zk: localhost:2181(CONNECTED) 8] set /zk-per per-plus

需要指定更改的路径和内容,我更改的是将/zk-per节点里的内容改为per-plus,来check一下

[zk: localhost:2181(CONNECTED) 9] get /zk-per
per-plus

内容已经更改,不够详细是吗?没看到版本修改是吗?看

[zk: localhost:2181(CONNECTED) 10] get -s /zk-per
per-plus
cZxid = 0x200000004
ctime = Tue Aug 31 18:29:56 CST 2021
mZxid = 0x200000005
mtime = Tue Aug 31 18:35:30 CST 2021
pZxid = 0x200000004
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 8
numChildren = 0

这里可以看到,dataVersion已经变成了1了。

5.4 删除节点

简单操作
delete 加路径

[zk: localhost:2181(CONNECTED) 11] delete /zk-per 
[zk: localhost:2181(CONNECTED) 12] get /zk-per
org.apache.zookeeper.KeeperException$NoNodeException: KeeperErrorCode = NoNode for /zk-per
[zk: localhost:2181(CONNECTED) 13] ls /
[zk-sorted0000000000, zk-tmp, zookeeper]

可以看到,节点被删除了。
值得注意的是,若删除节点存在子节点,那么无法删除该节点,必须先删除子节点,再删除父节点。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值