安装zookeeper

环境要求:JDK1.8

下载

1
2
mkdir -p /opt/zookeeper && cd /opt/zookeeper
wget https://mirrors.cnnic.cn/apache/zookeeper/zookeeper-3.6.1/apache-zookeeper-3.6.1-bin.tar.gz

解压

1
tar -zxvf apache-zookeeper-3.6.1-bin.tar.gz

关闭防火墙

1
2
3
4
## 停止firewall
systemctl stop firewalld.service
## 禁止firewall开机启动
systemctl disable firewalld.service

配置环境变量

1
2
3
4
5
6
vi /etc/profile

#将下面的代码放到末尾
#set zookeeper environment
export ZK_HOME=/opt/zookeeper/apache-zookeeper-3.6.1-bin
export PATH=$PATH:$ZK_HOME/bin

重新加载环境变量

1
source /etc/profile

配置

1
2
3
cd /opt/zookeeper/apache-zookeeper-3.6.1-bin/conf
cp zoo_sample.cfg zoo.cfg
vi zoo.cfg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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
1
2
3
4
#数据文件夹
dataDir=/opt/zookeeper/apache-zookeeper-3.6.1-bin/data
#监听端口
clientPort=2181

命令

1
2
3
4
zkServer.sh start
zkServer.sh stop
zkServer.sh status
zkServer.sh stop

如不加环境变量需要进入目录使用命令

1
2
3
4
5
cd /opt/zookeeper/apache-zookeeper-3.6.1-bin/bin
./zkServer.sh start

## 如果无法启动,查看启动日志
./zkServer.sh start-foreground

查看进程

1
ps -ef|grep zookeeper

zookeeper命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#查看broker的id
[zk: localhost:2181(CONNECTED) 0] ls /brokers/ids
[1]
#查看消息
[zk: localhost:2181(CONNECTED) 1] ls /brokers/topics
[__consumer_offsets, fund-deploy-dev, nmk, test, tmallgift-dev, tmallgift-tdev]
#查看删除的消息
ls /admin/delete_topics
[account]
#查看broker的信息
get /brokers/ids/0
[zk: localhost:2181(CONNECTED) 1] get /brokers/ids/1
{"listener_security_protocol_map":{"PLAINTEXT":"PLAINTEXT"},"endpoints":["PLAINTEXT://172.17.65.46:9092"],"jmx_port":-1,"host":"172.17.65.46","timestamp":"1589882903946","port":9092,"version":4}
#删除消息
rmr /brokers/topics/test
rmr /admin/delete_topics/test

单机搭建zookeeper集群

所谓单机搭建zookeeper集群其实就是在一台机器上启动多个zookeeper,在启动每个zookeeper时分别使用不同的配置文件zoo.cfg来启动,每个配置文件使用不同的配置参数(clientPort端口号、dataDir数据目录、dataLogDir数据日志目录)在同一台机器上启动多次。

配置多个zoo.cfg配置文件

zookeeper-1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[root@server conf]# cat zoo.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=/opt/zookeeper-1/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
server.0=localhost:2287:3387
server.1=localhost:2288:3388
server.2=localhost:2289:3389
admin.serverPort=8881

zookeeper-2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[root@server conf]# cat zoo.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=/opt/zookeeper-2/zookeeper
# the port at which the clients will connect
clientPort=2182
# 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
server.0=localhost:2287:3387
server.1=localhost:2288:3388
server.2=localhost:2289:3389
admin.serverPort=8882

zookeeper-3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[root@server conf]# cat zoo.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=/opt/zookeeper-3/zookeeper
# the port at which the clients will connect
clientPort=2183
# 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
server.0=localhost:2287:3387
server.1=localhost:2288:3388
server.2=localhost:2289:3389
admin.serverPort=8883
  • tickTime: 基本事件单元,以毫秒为单位。这个时间是作为zookeeper服务器之间或客户端与服务器间维持心跳的时间。也就是每隔tickTime时间就会发送一个心跳
  • dataDir: 存储内存中数据库快照的位置,就是zookeeper保存数据的目录,默认情况下,zookeeper将数据的日志问也保存在这个目录里
  • clientPort: 客户端连接zookeeper服务器的端口,默认是2181,zookeeper会监听这个端口,接收客户端的访问请求
  • initLimit: 这个配置项是用来配置zookeeper接收客户端初始化连接能忍受多少个心跳时间间隔数。当已经超过10个心跳的时间(tickTime)长度后,zookeeper服务器还没有接收到客户端的返回信息,那么表明这个客户端连接失败。总的时间长度就是 10*2000 = 20 秒
  • syncLimit: 这个配置项标识Leader和Follower之间发送消息,请求和应答的长度,最长不能超过多少个tickTime的时间长度,总的时间长度就是 5 * 2000 = 10秒
  • server.myid=IP:Port1:Port2, myid是服务器的编号,一个正整数,一般是0、1、2、3等待,port1表示的是服务器与集群中的Leader服务器交换信息的端口,一般用2288,Port2表示的是万一集群中的Leader服务器宕机了,需要一个端口来重新进行宣讲,选出一个新的Leader,一般用3388

创建myid文件并配置

1
2
3
4
5
6
[root@public-service zookeeper]# cat /opt/zookeeper-1/zookeeper/myid
0
[root@public-service zookeeper]# cat /opt/zookeeper-2/zookeeper/myid
1
[root@public-service zookeeper]# cat /opt/zookeeper-3/zookeeper/myid
2
启动3个zookeeper服务
1
2
3
./zkServer.sh start /opt/zookeeper-1/conf/zoo.cfg 
./zkServer.sh start /opt/zookeeper-2/conf/zoo.cfg
./zkServer.sh start /opt/zookeeper-3/conf/zoo.cfg
查看每个zookeeper对应的角色
1
2
3
./zkServer.sh status /opt/zookeeper-1/conf/zoo.cfg 
./zkServer.sh status /opt/zookeeper-2/conf/zoo.cfg
./zkServer.sh status /opt/zookeeper-3/conf/zoo.cfg
停止zookeeper服务
1
2
3
zkServer stop /opt/zookeeper-1/conf/zoo.cfg
zkServer stop /opt/zookeeper-2/conf/zoo.cfg
zkServer stop /opt/zookeeper-3/conf/zoo.cfg