- 准备jdk和zookeeper安装包【官网即可下载】
zookeeper-3.5.1-alpha.tar.gz
jdk1.7.0_8020200612.tar
- 准备三台linux虚拟机【具体以项目实际需要为准】,并安装jdk和zookeeper
虚拟机地址如下:194.1.1.86(server.1)、194.1.1.74(server.2)、194.1.1.45(server.3)
- 安装zookeeper(/app/soft/安装目录自定义)
3.1、分别将zookeeper-3.5.1-alpha.tar.gz安装包拷贝至194.1.1.86、194.1.1.74、194.1.1.45服务器的/app/soft/目录,然后执行以下命令解压:
[domains@ensemble1 soft]$ tar -zxvf zookeeper-3.5.1-alpha.tar.gz zookeeper
然后进入zookeeper目录创建data、logs目录
[domains@ensemble1 zookeeper]$ pwd
/app/soft/zookeeper
[domains@ensemble1 zookeeper]$ mkdir logs data
3.2、配置zoo.cfg文件【3台虚拟机分别执行】:
进入conf目录执行以下命令拷贝创建zoo.cfg文件
[domains@ensemble1 conf]$ cpzoo_sample.cfgzoo.cfg
[domains@ensemble1 conf]$ ll
vi zoo.cfg配置集群信息1/2/3要与对应服务器和myid文件一致,其中2888是数据同步和通信端口,3888是选举端口:
server.1= 194.1.1.86:2888:3888
server.2= 194.1.1.74:2888:3888
server.3= 194.1.1.45:2888:3888
dataDir=/app/soft/zookeeper/data【配置zookeeper数据文件存储目录】
3.3、在服务存放zookeeper数据文件目录执行以下命令穿件myid文件,并分别写入各节点对应的数字,例如在194.1.1.86的myid文件中写入1:
[domains@ensemble1 data]$ pwd
/app/soft/zookeeper/data
[domains@ensemble1 data]$ echo 1 >myid
194.1.1.74的myid文件中写入2:
[domains@ensemble1 data]$ pwd
/app/soft/zookeeper/data
[domains@ensemble1 data]$ echo 2 >myid
194.1.1.45的myid文件中写入3:
[domains@ensemble1 data]$ pwd
/app/soft/zookeeper/data
[domains@ensemble1 data]$ echo 3>myid
3.4、可以根据需要修改日志路径:
日志文件输入路径在/app/soft/zookeeper/bin/zkEnv.sh变量里找到
if [ "x${ZOO_LOG_DIR}" = "x" ]
then
ZOO_LOG_DIR="$ZOOKEEPER_PREFIX/logs"
Fi
- 启动zookeeper集群,分别进入3台虚拟机的/app/soft/zookeeper/bin/目录执行以下命令启动zookeeper服务:
[domains@ensemble1 bin]$ ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /app/soft/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
然后查看当前服务状态:
[domains@ensemble1 bin]$ ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /app/soft/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower
注意:3台服务器的zookeeper服务都启动完成后,查看zookeeper状态会发现3台服务会选举一各leader节点和2个follower节点。
- 验证zookeeper集群是否搭建完成:
输入以下命令:[domains@ensemble1 bin]$ ./zkCli.sh -server 194.1.1.74:2181
输入ls/path
【查看zookeeper集群中注册的服务,在zookeeper中各服务的存储Id,客户端
调用服务时也是通过id值找到集群服务中对应的应用服务然后调用】
- 集群中各节点宕机后的影响和处理方式:
以上3个节点的zookeeper集群中会选举出一个leader和2个follower节点,follower几点负责接收客户端的请求并处理,leader节点负责服务端服务id的写入并同步给follower节点。
6.1、2个follower节点中任意一个节点挂掉
当2个follower节点中任意一个节点挂掉是不会影响zookeeper集群对外提供服务,这个时候恢复集群只需要把对应宕机节点的zookeeper服务重新启动即可。
6.2、 2个follower节点同时宕机
当2个follower节点同时宕机挂掉是不会影响zookeeper集群对外提供服务,但是应用服务后台会提示zookeeper连接拒绝异常:
2024-03-15 15:25:13,108 : eaip-XA1010080-0 WARN main-SendThread(194.1.35.8:2181) org.apache.zookeeper.ClientCnxn run 1235: Session 0x318dd1c13950000 for server 194.1.1.74/194.1.1.74:2181, unexpected error, closing socket connection and attempting reconnect
java.net.ConnectException: Connection refused: no further information
atsun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:357)
at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1214)
2024-03-15 15:25:13,612 : eaip-XA1010080-0 WARN main-SendThread(194.1.1.86:2181) org.apache.zookeeper.ClientCnxn run 1235: Session 0x21eff7c26120000 for server 194.1.1.74/194.1.1.74:2181, unexpected error, closing socket connection and attempting reconnect
java.net.ConnectException: Connection refused: no further information
atsun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:357)
at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1214)这个时候恢复集群只需要把对应宕机节点的zookeeper服务重新启动即可。
6.2、leader节点宕机挂掉
当leader节点宕机挂掉后,应用服务是不会出现异常。这个时候2个follower节点会自动选举一个新的leader节点,此时只需要把宕机的节点重新启动即可。
6.3、leader节点和一个follower节点同时宕机
当eader节点和一个follower节点同时宕机掉后,客户端调用应用服务会出现异常。但是这个时候zookeeper集群服务应挂掉,需要重启3台服务器zookeeper服务器。
2024-03-15 15:36:59,995 : gw-nbs-XA1010080-0 WARN RMI TCP Connection(194.1.35.8:2181) org.apache.zookeeper.ClientCnxn run 1235: Session 0x10f9f2774430000 for server 194.1.1.74/194.1.1.74:2181, unexpected error, closing socket connection and attempting reconnect
java.net.ConnectException: Connection refused: no further information
atsun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:357)
at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1214)
2024-03-15 15:37:00,330 : gw-nbs-XA1010080-0 WARN RMI TCP Connection(194.1.1.86:2181) org.apache.zookeeper.ClientCnxn run 1235: Session 0x0 for server 194.1.1.74/194.1.1.74:2181, unexpected error, closing socket connection and attempting reconnect
java.net.ConnectException: Connection refused: no further information
atsun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:357)
at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1214)
2024-03-15 15:37:00,912 : gw-nbs-XA1010080-0 WARN RMI TCP Connection(194.1.1.86:2181) org.apache.zookeeper.ClientCnxn run 1235: Session 0x0 for server 194.1.1.74/194.1.1.74:2181, unexpected error, closing socket connection and attempting reconnect
6.4、3个集群节点同时宕机
需要重启3台服务器zookeeper服务器。