下载
下载地址 zookeeper,建议下载 bin 结尾的tar包。
在 readme.md 中可以看到
apache-zookeeper-[version].tar.gz Contains all the source files which can be built by running: mvn clean install To generate an aggregated apidocs for zookeeper-server and zookeeper-jute: mvn javadoc:aggregate (generated files will be at target/site/apidocs) apache-zookeeper-[version]-bin.tar.gz Contains all the jar files required to run ZooKeeper Full documentation can also be found in the docs folder
不以 bin 结尾的需要 mvn clean install,我试过这个 tar 包,麻烦...
单机安装
解压 tar 包
进入 zookeeper 目录,建立 data 文件夹
进入conf,修改配置文件
初始的配置文件是 zoo_sample.cfg,zookeeper 默认使用 zoo.cfg,可以新建一个 zoo.cfg,也可以把 zoo_sample cp 成 zoo
tickTime=2000 initLimit=10 syncLimit=5 dataDir=/usr/local/kafka/apache-zookeeper-3.5.5-bin/data clientPort=2181
tickTime:zk 服务器之间或者客户端与服务器之间心跳的时间间隔,毫秒
initLimit:
dataDir:zk 数据存储路径
clientPort:zk 服务器监听的端口
配置完成,就可以启动服务器了,正常情况下输出如下
[root@localhost apache-zookeeper-3.5.5-bin]# bin/zkServer.sh start /usr/bin/java ZooKeeper JMX enabled by default Using config: /usr/local/kafka/apache-zookeeper-3.5.5-bin/bin/../conf/zoo.cfg Starting zookeeper ... STARTED
但是上面并不能说明服务器已经启动,可以查看服务器状态
[root@localhost apache-zookeeper-3.5.5-bin]# bin/zkServer.sh status /usr/bin/java ZooKeeper JMX enabled by default Using config: /usr/local/kafka/apache-zookeeper-3.5.5-bin/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Error contacting service. It is probably not running.
我们发现并未成功启动。
此时,需要查看日志
打开日志,可以看到 Error,提示 连不上 8080 端口,我想到是不是 8080 被占用了,查了一下果然
netstat -anp | grep 8080
结束该进程,重新启动服务器,OK了
[root@localhost apache-zookeeper-3.5.5-bin]# bin/zkServer.sh start /usr/bin/java ZooKeeper JMX enabled by default Using config: /usr/local/kafka/apache-zookeeper-3.5.5-bin/bin/../conf/zoo.cfg Starting zookeeper ... STARTED [root@localhost apache-zookeeper-3.5.5-bin]# bin/zkServer.sh status /usr/bin/java ZooKeeper JMX enabled by default Using config: /usr/local/kafka/apache-zookeeper-3.5.5-bin/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Mode: standalone
输出如上代表启动成功。
停止服务器
[root@localhost apache-zookeeper-3.5.5-bin]# bin/zkServer.sh stop /usr/bin/java ZooKeeper JMX enabled by default Using config: /usr/local/kafka/apache-zookeeper-3.5.5-bin/bin/../conf/zoo.cfg Stopping zookeeper ... STOPPED
思考 zookeeper 在哪里用到 8080 端口了呢?在 AdminServe 中找到了这个配置
<ul> <li><p><em>admin.enableServer</em> : (Java system property: <strong>zookeeper.admin.enableServer</strong>) Set to "false" to disable the AdminServer. By default the AdminServer is enabled.</p></li> <li><p><em>admin.serverAddress</em> : (Java system property: <strong>zookeeper.admin.serverAddress</strong>) The address the embedded Jetty server listens on. Defaults to 0.0.0.0.</p></li> <li><p><em>admin.serverPort</em> : (Java system property: <strong>zookeeper.admin.serverPort</strong>) The port the embedded Jetty server listens on. Defaults to 8080.</p></li> <li><p><em>admin.idleTimeout</em> : (Java system property: <strong>zookeeper.admin.idleTimeout</strong>) Set the maximum idle time in milliseconds that a connection can wait before sending or receiving data. Defaults to 30000 ms.</p></li> <li><p><em>admin.commandURL</em> : (Java system property: <strong>zookeeper.admin.commandURL</strong>) The URL for listing and issuing commands relative to the root URL. Defaults to "/commands".</p></li> </ul>
解决这个问题的方法有三种:
1. 修改端口
两种方式:启动时增加配置项 -Dzookeeper.admin.serverPort=你的端口号;在 zoo.cfg 中增加配置 admin.serverPort=没有被占用的端口号
2. 删除 jetty
3. 停用这个服务,在启动时增加配置项 -Dzookeeper.admin.enableServer=false
服务器启动后,就可以启动客户端进程测试了
[root@localhost apache-zookeeper-3.5.5-bin]# bin/zkCli.sh
出现如下界面
WatchedEvent state:SyncConnected type:None path:null [zk: localhost:2181(CONNECTED) 0]
输入 help 查看命令
[zk: localhost:2181(CONNECTED) 0] help ZooKeeper -server host:port cmd args addauth scheme auth close config [-c] [-w] [-s] connect host:port create [-s] [-e] [-c] [-t ttl] path [data] [acl] delete [-v version] path deleteall path delquota [-n|-b] path get [-s] [-w] path getAcl [-s] path history listquota path ls [-s] [-w] [-R] path ls2 path [watch] printwatches on|off quit reconfig [-s] [-v version] [[-file path] | [-members serverID=host:port1:port2;port3[,...]*]] | [-add serverId=host:port1:port2;port3[,...]]* [-remove serverId[,...]*] redo cmdno removewatches path [-c|-d|-a] [-l] rmr path set [-s] [-v version] path data setAcl [-s] [-v version] [-R] path acl setquota -n|-b val path stat [-w] path sync path
创建节点,赋值,取值,删除,查看根节点
[zk: localhost:2181(CONNECTED) 17] ls / [zookeeper] [zk: localhost:2181(CONNECTED) 18] create /mykey1 Created /mykey1 [zk: localhost:2181(CONNECTED) 19] create /mykey2 Created /mykey2 [zk: localhost:2181(CONNECTED) 20] set /mykey1 3 [zk: localhost:2181(CONNECTED) 21] get /mykey1 3 [zk: localhost:2181(CONNECTED) 22] ls / [mykey1, mykey2, zookeeper] [zk: localhost:2181(CONNECTED) 23] delete /mykey1 [zk: localhost:2181(CONNECTED) 24] ls / [mykey2, zookeeper]
集群安装
由于只有一台服务器,本次安装伪分布式集群,与分布式安装过程雷同。
伪集群是指用一台服务器的多个端口模拟多台服务器,用多个配置文件启动多个 zookeeper 实例。
与集群安装不同点:
1. 由于在一台服务器上,zookeeper 服务器监听的端口不能相同,即 clientPort 不相同,而集群可以相同
2. 数据存储路径不同,即 dataDir 不相同;也有必要把 dataLogDir 启动日志分开存储,当然 log 路径相同也可以
3. server.x 与 myid :server.x 中的 x 等于 myid 文件里的数字 【这两个在单个服务器安装中是没有的】
// myid 文件中只写了一个数字,代表该节点在集群中的角色 id,myid 文件需要手动创建
// server.x 对应服务器 IP 和端口,同一台服务器上需要连两个端口,且不同 server 端口不能相同
安装过程
第一步,新建3个配置文件 zoo1.cfg, zoo2.cfg, zoo3.cfg
zoo1.cfg 配置如下
tickTime=2000 initLimit=10 syncLimit=5 dataDir=/usr/local/zookeeper/zk1/data clientPort=2182 dataLogDir=/usr/local/zookeeper/zk1/logs server.1=localhost:2287:3387 server.2=localhost:2288:3388 server.3=localhost:2289:3389
其中3个配置文件的 dataDir 不能相同,dataLogDir 也不要相同;
后面的 server.x 必须写,3个配置文件一样;
第二步,建立 dataDir 和 dataLogDir 对应的路径
第三步,在每个 dataDir 下创建 myid 文件,并写入一个数字,对应 server.x 中的 x 【注意必须把每个服务的 myid 都先写好,不能写一个启动一个】
第四步,启动 zookeeper 服务,每个服务指定不同的配置文件
[root@localhost apache-zookeeper-3.5.5-bin]# bin/zkServer.sh start conf/zoo1.cfg /usr/bin/java ZooKeeper JMX enabled by default Using config: conf/zoo1.cfg Starting zookeeper ... STARTED [root@localhost apache-zookeeper-3.5.5-bin]# bin/zkServer.sh status conf/zoo1.cfg /usr/bin/java ZooKeeper JMX enabled by default Using config: conf/zoo1.cfg Client port found: 2191. Client address: localhost. Error contacting service. It is probably not running.
启动第一个 zookeeper 服务时,发生错误了,这其实是正常现象
因为 zookeeper 服务的每个实例都拥有全局配置信息,他们在启动时会随时随地选举 leader,也就是 要与其他 zookeeper 实例进行通信,而此时其他的 zookeeper 服务还没启动,自然无法通信,故报错
此时我们忽略此错误,直接把 3 个服务全部启动
然后检查状态,发现一切正常了。
[root@localhost apache-zookeeper-3.5.5-bin]# bin/zkServer.sh status conf/zoo1.cfg /usr/bin/java ZooKeeper JMX enabled by default Using config: conf/zoo1.cfg Client port found: 2191. Client address: localhost. Mode: follower [root@localhost apache-zookeeper-3.5.5-bin]# bin/zkServer.sh status conf/zoo2.cfg /usr/bin/java ZooKeeper JMX enabled by default Using config: conf/zoo2.cfg Client port found: 2192. Client address: localhost. Mode: leader
并且显示了 主从关系。
参考资料:
https://blog.csdn.net/qq_27868061/article/details/91383759 zookeeper3.5.5安装
https://www.jianshu.com/p/f6c96b2d4b6a centos7 zookeeper3.5.5 安装
https://www.cnblogs.com/crazylqy/p/7119030.html 安装zookeeper(单机,伪集群)
https://www.cnblogs.com/songfayuan/articles/7290175.html ZooKeeper启动占用8080端口