Mode: standalone
下载后解压配置好jdk环境。下面正式开始。
配置文件 /zookeeper3.6/conf
jun@jun-H310MHC2:~/zookeeper3.6/conf$ cp zoo_sample.cfg zoo.cfg
jun@jun-H310MHC2:~/zookeeper3.6$ bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /home/jun/zookeeper3.6/bin/../conf/zoo.cfg
Starting zookeeper ... already running as process 21867.
jun@jun-H310MHC2:~/zookeeper3.6$
查看状态
jun@jun-H310MHC2:~/zookeeper3.6$ bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /home/jun/zookeeper3.6/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: standalone
jun@jun-H310MHC2:~/zookeeper3.6$
关闭
jun@jun-H310MHC2:~/zookeeper3.6$ bin/zkServer.sh stop
ZooKeeper JMX enabled by default
Using config: /home/jun/zookeeper3.6/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
jun@jun-H310MHC2:~/zookeeper3.6$ bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /home/jun/zookeeper3.6/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Error contacting service. It is probably not running.
客户端连接zookeeper
jun@jun-H310MHC2:~/zookeeper3.6$ bin/zkCli.sh
Connecting to localhost:2181
....
[zk: localhost:2181(CONNECTED) 0]
[zk: localhost:2181(CONNECTED) 0]
参考如下文章:
https://blog.csdn.net/java_66666/article/details/81015302
创建节点
[zk: localhost:2181(CONNECTED) 1] create /mynode mydataInNode
Created /mynode
获取节点数据
[zk: localhost:2181(CONNECTED) 2] get /mynode
mydataInNode
修改节点数据
[zk: localhost:2181(CONNECTED) 3] set /mynode modifyMydata
[zk: localhost:2181(CONNECTED) 4] get /mynode
modifyMydata
删除节点
[zk: localhost:2181(CONNECTED) 5] delete /mynode
[zk: localhost:2181(CONNECTED) 6] get /mynode
org.apache.zookeeper.KeeperException$NoNodeException: KeeperErrorCode = NoNode for /mynode
分布式搭建
伪分布式–真正的全分布式,只是将配置文件一个机器放一个 然后启动
配置方式 /zookeeper3.6/conf 下配置文件 配置三台服务器实例 然后配置每一台zoo-{}.cfg
jun@jun-H310MHC2:~/zookeeper3.6/conf$ cp zoo.cfg zoo-1.cfg
jun@jun-H310MHC2:~/zookeeper3.6/conf$ cp zoo.cfg zoo-2.cfg
jun@jun-H310MHC2:~/zookeeper3.6/conf$ cp zoo.cfg zoo-3.cfg
jun@jun-H310MHC2:~/zookeeper3.6/conf$ ll
总用量 36
drwxr-xr-x 2 jun jun 4096 5月 28 17:00 ./
drwxrwxr-x 7 jun jun 4096 5月 28 15:40 ../
-rw-r--r-- 1 jun jun 535 4月 21 22:59 configuration.xsl
-rw-r--r-- 1 jun jun 3435 4月 21 22:59 log4j.properties
-rw-r--r-- 1 jun jun 1148 5月 28 17:00 zoo-1.cfg
-rw-r--r-- 1 jun jun 1148 5月 28 17:00 zoo-2.cfg
-rw-r--r-- 1 jun jun 1148 5月 28 17:00 zoo-3.cfg
-rw-r--r-- 1 jun jun 1148 5月 28 15:38 zoo.cfg
-rw-r--r-- 1 jun jun 1148 4月 21 22:59 zoo_sample.cfg
jun@jun-H310MHC2:~/zookeeper3.6/conf$
配置第一台zk实例
# The number of ticks that the initial
# 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=/home/jun/zookeeper_data_dir/zk1
# the port at which the clients will connect
clientPort=2181
server.1=127.0.0.1:2888:3888
server.2=127.0.0.1:2889:3889
server.3=127.0.0.1:2890:3890
配置第2台zk实例
# 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.
# the port at which the clients will connect
clientPort=2182
dataDir=/home/jun/zookeeper_data_dir/zk2
# the port at which the clients will connect
server.1=127.0.0.1:2888:3888
server.2=127.0.0.1:2889:3889
server.3=127.0.0.1:2890:3890
配置第2台zk实例
# 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.
# the port at which the clients will connect
clientPort=2183
dataDir=/home/jun/zookeeper_data_dir/zk3
# the port at which the clients will connect
server.1=127.0.0.1:2888:3888
server.2=127.0.0.1:2889:3889
server.3=127.0.0.1:2890:3890
这里可以配置端口号相同,但是是在同一台机器上搭建的伪分布式,所以所有端口号都不一样。
配置说明
tickTime:这个时间是作为 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。
initLimit:这个配置项是用来配置 Zookeeper 接受客户端(这里所说的客户端不是用户连接 Zookeeper 服务器的客户端,而是 Zookeeper 服务器集群中连接到 Leader 的 Follower 服务器)初始化连接时最长能忍受多少个心跳时间间隔数。当已经超过 10个心跳的时间(也就是 tickTime)长度后 Zookeeper 服务器还没有收到客户端的返回信息,那么表明这个客户端连接失败。总的时间长度就是 10*2000=20 秒
syncLimit:这个配置项标识 Leader 与 Follower 之间发送消息,请求和应答时间长度,最长不能超过多少个 tickTime 的时间长度,总的时间长度就是 5*2000=10秒
dataDir:顾名思义就是 Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。
clientPort:这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。
server.A=B:C:D:其中 A 是一个数字,表示这个是第几号服务器;B 是这个服务器的 ip 地址;C 表示的是这个服务器与集群中的 Leader 服务器交换信息的端口;D 表示的是万一集群中的 Leader 服务器挂了,需要一个端口来重新进行选举,选出一个新的 Leader,而这个端口就是用来执行选举时服务器相互通信的端口。如果是伪集群的配置方式,由于 B 都是一样,所以不同的 Zookeeper 实例通信端口号不能一样,所以要给它们分配不同的端口号。
标识Server ID
jun@jun-H310MHC2:~$ cd zookeeper_data_dir/
jun@jun-H310MHC2:~/zookeeper_data_dir$ cd zk1/
jun@jun-H310MHC2:~/zookeeper_data_dir/zk1$ vim myid
1
jun@jun-H310MHC2:~/zookeeper_data_dir/zk1$ cd ..
jun@jun-H310MHC2:~/zookeeper_data_dir$ cd zk2/
jun@jun-H310MHC2:~/zookeeper_data_dir/zk2$ vim myid
2
jun@jun-H310MHC2:~/zookeeper_data_dir/zk2$ cd ..
jun@jun-H310MHC2:~/zookeeper_data_dir$ cd zk3/
jun@jun-H310MHC2:~/zookeeper_data_dir/zk3$ vim myid
3
jun@jun-H310MHC2:~/zookeeper_data_dir/zk3$
启动
jun@jun-H310MHC2:~/zookeeper3.6$ bin/zkServer.sh start conf/zoo-1.cfg
jun@jun-H310MHC2:~/zookeeper3.6$ bin/zkServer.sh start conf/zoo-2.cfg
jun@jun-H310MHC2:~/zookeeper3.6$ bin/zkServer.sh start conf/zoo-3.cfg
bin/zkServer.sh status conf/zoo-1.cfg
可以查看选举的状态