安装包:apache-zookeeper-3.5.5-bin.tar.gz,下载路径 https://archive.apache.org/dist/zookeeper/
环境要求:zookeeper 3.5.5要求JDK版本不低于1.8,centos7和rhel7的jdk默认就是1.8版本
安装过程(在单机上安装3个实例,实例的目录和端口不同):
①上传及解压安装包
[root@centos7-2 ~]# mkdir -p /cslc/zookeeper [root@centos7-2 ~]# cd /cslc/zookeeper/ #通过SFTP将安装包上传到这个目录 [root@centos7-2 zookeeper]# tar zxf apache-zookeeper-3.5.5-bin.tar.gz [root@centos7-2 zookeeper]# cp -r apache-zookeeper-3.5.5-bin zookeeper1 [root@centos7-2 zookeeper]# cp -r apache-zookeeper-3.5.5-bin zookeeper2 [root@centos7-2 zookeeper]# cp -r apache-zookeeper-3.5.5-bin zookeeper3 #三个实例 [root@centos7-2 zookeeper]# ll zookeeper1/ 总用量 32 drwxr-xr-x. 2 root root 232 2月 23 21:36 bin drwxr-xr-x. 2 root root 77 2月 23 21:36 conf drwxr-xr-x. 5 root root 4096 2月 23 21:36 docs drwxr-xr-x. 2 root root 4096 2月 23 21:36 lib -rw-r--r--. 1 root root 11358 2月 23 21:36 LICENSE.txt -rw-r--r--. 1 root root 432 2月 23 21:36 NOTICE.txt -rw-r--r--. 1 root root 1560 2月 23 21:36 README.md -rw-r--r--. 1 root root 1347 2月 23 21:36 README_packaging.txt
②创建配置文件
[root@centos7-2 zookeeper]# mkdir {zookeeper1,zookeeper2,zookeeper3}/data #创建数据文件目录 [root@centos7-2 zookeeper]# cd zookeeper1/conf/ [root@centos7-2 conf]# cp zoo_sample.cfg zoo.cfg #复制生成配置文件
修改zoo.cfg,修改部分如下,然后分别修改zookeeper2和zookeeper3的配置文件,三个配置文件只有dataDir和clientPort不同。
# 存放数据文件 dataDir=/cslc/zookeeper/zookeeper1/data #其他两个为zookeeper2/data和zookeeper3/data # 服务端口 clientPort=2181 #其他两个为2182和2183 # zookeeper cluster,2888为选举端口,3888为心跳端口 server.1=192.168.85.20:2887:3887 server.2=192.168.85.20:2888:3888 server.3=192.168.85.20:2889:3889
③创建myid文件
myid文件的内容为一个数字,用来标识当前主机,conf/zoo.cfg文件中配置的server.X中X是什么数字,myid文件中就输入这个数字。
[root@centos7-2 conf]# echo "1" > /cslc/zookeeper/zookeeper1/data/myid [root@centos7-2 conf]# echo "2" > /cslc/zookeeper/zookeeper2/data/myid [root@centos7-2 conf]# echo "3" > /cslc/zookeeper/zookeeper3/data/myid
④启动和关闭和查看状态
启动命令在bin目录下,./zkServer.sh start、stop、status可分别启动、停止和查看状态。
要同时启动三个实例的应用,启动后会开始选取leader和follower,耗时约30s~120s,leader选取期间集群服务不可用。
启动完成后通过status查看(由于leader选取耗时刚启动状态可能为not running),只有一个实例的Mode为leader即主,其他实例Mode为follower即从。
[root@centos7-2 conf]# cd ../bin [root@centos7-2 bin]# ls README.txt zkCli.cmd zkEnv.cmd zkServer.cmd zkServer.sh zkTxnLogToolkit.sh zkCleanup.sh zkCli.sh zkEnv.sh zkServer-initialize.sh zkTxnLogToolkit.cmd [root@centos7-2 bin]# ./zkServer.sh start /usr/bin/java ZooKeeper JMX enabled by default Using config: /cslc/zookeeper/zookeeper1/bin/../conf/zoo.cfg Starting zookeeper ... STARTED [root@centos7-2 bin]# ./zkServer.sh status /usr/bin/java ZooKeeper JMX enabled by default Using config: /cslc/zookeeper/zookeeper1/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Mode: leader
服务发现原理
zookeeper本质是一个文件系统,进入文件系统的操作,路径必须从/开始
# 通过客户端登录zookeeper [root@centos7-2 bin]# ./zkCli.sh -server localhost:2181 /usr/bin/java Connecting to localhost:2181 2020-02-23 22:49:53,274 [myid:] - INFO [main:Environment@109] - Client environment:zookeeper.version=3.5.5-390fe37ea45dee…… [zk: localhost:2181(CONNECTED) 0] ls / [zookeeper] # 以服务发现为例,假设服务名为elp,下属两个服务,分别为172.26.0.[1,2]:8080,即在zookeeper文件系统下创建两个目录表示elp服务下的IP及端口 # 分别创建/elp/172.26.0.1:8080与/elp/172.26.0.1:8080 [zk: localhost:2181(CONNECTED) 1] create /elp Created /elp [zk: localhost:2181(CONNECTED) 2] cd /elp [zk: localhost:2181(CONNECTED) 3] create /elp/172.26.0.1:8080 Created /elp/172.26.0.1:8080 [zk: localhost:2181(CONNECTED) 4] create /elp/172.26.0.2:8080 Created /elp/172.26.0.2:8080 # 查看 [zk: localhost:2181(CONNECTED) 5] ls /elp [172.26.0.1:8080, 172.26.0.2:8080] # zookeeper提供了“心跳检测”功能,它会定时向各个服务提供者发送一个请求,如果长期没有响应,服务中心就认为该服务提供者已经“挂了”,并将其剔除。 # 例如:zookeeper判断172.26.0.2:8080心跳失败 [zk: localhost:2181(CONNECTED) 6] delete /elp/172.26.0.2:8080 [zk: localhost:2181(CONNECTED) 6] ls /elp # 查看结果 [172.26.0.1:8080]