【zookeeper学习笔记二】安装搭建

zookeeper的搭建方式

zookeeper有三种安装方式:

  1. 单机模式:zookeeper只运行在一台服务器上,适用于测试环境
  2. 伪集群模式:在一台物理机上运行多个zookeeper实例
  3. 集群模式:zookeeper运行于一个集群上,适用于生产环境,这个计算机集群被称为一个"集合体"(ensemble)

单机模式搭建

下载zookeeper

官网:http://mirror.bit.edu.cn/apache/zookeeper/

解压到安装目录

更改配置文件

在conf目录下,新建zoo.cfg

tickTime=2000
dataDir=/usr/local/zk/data
dataLogDir=/usr/local/zk/dataLog
clientPort=2181

配置环境变量

vi .bash_profile

添加后面两行的内容

export M2_HOME=/Users/sanbian/Documents/soft/apache-maven-3.3.9/
export PATH=$PATH:$M2_HOME/bin

export ZOOKEEPER_HOME=/usr/local/zk
export PATH=$PATH:$ZOOKEEPER_HOME/bin

启动zookeeper

在zookeeper的bin目录下,管理员启动

sudo sh zkServer.sh start

响应的,关闭:

sudo sh zkServer.sh stop

伪集群模式搭建

要在一台机器上的部署3个server,我们使用的是:每个配置文档模拟一台机器,实现单台机器上运行多个zookeeper实例。

必须保证的是:每个配置文档的端口号、dataDir都不能冲突!!!

搭建3个server

按照一下路径建立3个server:

sanbiandeMacBook-Pro:zookeeper sanbian$ tree -d -L 2
.
├── server1
│   ├── data
│   ├── dataLog
│   └── zk
├── server2
│   ├── data
│   ├── dataLog
│   └── zk
└── server3
    ├── data
    ├── dataLog
    └── zk

12 directories

配置文档 zoo.cfg

dir相对配置,clientPort也响应配置为2181,2182,2183

# 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=/Users/sanbian/Documents/soft/zookeeper/server3/data

dataLogDir=/Users/sanbian/Documents/soft/zookeeper/server3/dataLog
# 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.1=127.0.0.1:8881:7771
server.2=127.0.0.1:8882:7772
server.3=127.0.0.1:8883:7773

各个配置项的含义

tickTime=2000

zookeeper服务器之间或客户端与服务器之间维持心跳的时间间隔,每隔tickTime时间就会发送一个心跳。

initLimit

配置zookeeper接受客户端(zookeeper服务器集群中连接到Leader的Follower服务器)初始化连接时最长能忍受多少个心跳时间间隔数。当已超过initLimit*tickTime长度后,zookeeperr服务器若还没有收到客户端的返回信息,则表明客户端连接失败。

syncLimit

配置Leader与Follower之间发送消息,请求和应答时间长度,最长不能超过syncLimit*tickTime的时间长度

dataDir

zookeeper保存数据的目录,默认情况下,写数据的日志也保存在这个目录

dataLogDir

若没有的话,则用dataDir.

zookeeper的持久化都存储在这个目录里。

dataLogDir放的是顺序日志(WAL),而dataDir放的是内存数据结构的snapshot,便于快速回复和达到性能最大化。

clientPort

zookeeper服务器监听的端口,以接受客户端的访问请求。

server.A=B:C:D
  1. A:是一个数字,表示这是第几号服务器
  2. B:这个服务器的ip地址。
  3. C:这个服务器与集群中的Leader服务器交换信息的端口
  4. D:万一集群中的Leader服务伪集群器挂了,需要一个端口来重新进行选举新的Leader,此端口就是用来执行选举时服务器相互通信的端口。如果是伪集群的配置方式,由于B都是一样的,所以不同zookeeper实例通信端口号不能一样,所以要给它们分配不同的端口号。

在data下面新建myid文档,对应到server.n

sanbiandeMacBook-Pro:zookeeper sanbian$ cd server1/data
sanbiandeMacBook-Pro:data sanbian$ echo "1" myid

启动服务

sanbiandeMacBook-Pro:zookeeper sanbian$ server3/zk/bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /Users/sanbian/Documents/soft/zookeeper/server3/zk/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
sanbiandeMacBook-Pro:zookeeper sanbian$ server2/zk/bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /Users/sanbian/Documents/soft/zookeeper/server2/zk/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
sanbiandeMacBook-Pro:zookeeper sanbian$ server1/zk/bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /Users/sanbian/Documents/soft/zookeeper/server1/zk/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

查看启动状态

sanbiandeMacBook-Pro:zookeeper sanbian$ server1/zk/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /Users/sanbian/Documents/soft/zookeeper/server1/zk/bin/../conf/zoo.cfg
Mode: follower
sanbiandeMacBook-Pro:zookeeper sanbian$ server2/zk/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /Users/sanbian/Documents/soft/zookeeper/server2/zk/bin/../conf/zoo.cfg
Mode: follower
sanbiandeMacBook-Pro:zookeeper sanbian$ server3/zk/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /Users/sanbian/Documents/soft/zookeeper/server3/zk/bin/../conf/zoo.cfg
Mode: leader
sanbiandeMacBook-Pro:zookeeper sanbian$ 

关闭一个server,看其他server状态

sanbiandeMacBook-Pro:zookeeper sanbian$ server1/zk/bin/zkServer.sh stop
ZooKeeper JMX enabled by default
Using config: /Users/sanbian/Documents/soft/zookeeper/server1/zk/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
sanbiandeMacBook-Pro:zookeeper sanbian$ server1/zk/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /Users/sanbian/Documents/soft/zookeeper/server1/zk/bin/../conf/zoo.cfg
Error contacting service. It is probably not running.
sanbiandeMacBook-Pro:zookeeper sanbian$ server2/zk/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /Users/sanbian/Documents/soft/zookeeper/server2/zk/bin/../conf/zoo.cfg
Mode: leader
sanbiandeMacBook-Pro:zookeeper sanbian$ server3/zk/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /Users/sanbian/Documents/soft/zookeeper/server3/zk/bin/../conf/zoo.cfg
Mode: follower

可以看到,关闭掉Server1后,server2自动成为了leader

关闭2个server,启动一个server

sanbiandeMacBook-Pro:zookeeper sanbian$ server3/zk/bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /Users/sanbian/Documents/soft/zookeeper/server3/zk/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
sanbiandeMacBook-Pro:zookeeper sanbian$ server3/zk/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /Users/sanbian/Documents/soft/zookeeper/server3/zk/bin/../conf/zoo.cfg
Error contacting service. It is probably not running.

产生异常信息的原因是由于Zookeeper 服务的每个实例都拥有全局配置信息,他们在启动的时候会随时随地的进行Leader选举操作。此时,第一个启动的Zookeeper需要和另外两个 Zookeeper实例进行通信。但是,另外两个Zookeeper实例还没有启动起来,因此就产生了这的异样信息。

如果将配置文件zoo.cfg里的关联其他server的配置项去掉,再重新启动查看状态

sanbiandeMacBook-Pro:zookeeper sanbian$ server3/zk/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /Users/sanbian/Documents/soft/zookeeper/server3/zk/bin/../conf/zoo.cfg
Mode: standalone

这样就变成了单机模式(独立模式)。

所以在启动三个服务时,先启动一个服务也会出现这样的异常,等到第二个启动成功后,便正常了。

sanbiandeMacBook-Pro:zookeeper sanbian$ server3/zk/bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /Users/sanbian/Documents/soft/zookeeper/server3/zk/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
sanbiandeMacBook-Pro:zookeeper sanbian$ server3/zk/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /Users/sanbian/Documents/soft/zookeeper/server3/zk/bin/../conf/zoo.cfg
Error contacting service. It is probably not running.
sanbiandeMacBook-Pro:zookeeper sanbian$ server2/zk/bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /Users/sanbian/Documents/soft/zookeeper/server2/zk/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
sanbiandeMacBook-Pro:zookeeper sanbian$ server3/zk/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /Users/sanbian/Documents/soft/zookeeper/server3/zk/bin/../conf/zoo.cfg
Mode: leader
sanbiandeMacBook-Pro:zookeeper sanbian$ server2/zk/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /Users/sanbian/Documents/soft/zookeeper/server2/zk/bin/../conf/zoo.cfg
Mode: follower

这个时候再关掉一个Server

sanbiandeMacBook-Pro:zookeeper sanbian$ server3/zk/bin/zkServer.sh stop
ZooKeeper JMX enabled by default
Using config: /Users/sanbian/Documents/soft/zookeeper/server3/zk/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
sanbiandeMacBook-Pro:zookeeper sanbian$ server2/zk/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /Users/sanbian/Documents/soft/zookeeper/server2/zk/bin/../conf/zoo.cfg
Error contacting service. It is probably not running.

在选举leader时,找不到可以通信的Server,就又异常了。

如果先关闭掉follower,也是一样的状态。

遇到的坑

Error contacting service. It is probably not running.

网上查了很多办法,都没有解决。后来一句话点醒了我,看日志呀!!!日志在哪里呢???zookeeper.out

sanbiandeMacBook-Pro:zookeeper sanbian$ vi server2/zk/bin/zookeeper.out 

2017-09-06 16:49:41,767 [myid:] - INFO  [main:QuorumPeerConfig@134] - Reading configuration from: ../conf/zoo3.cfg
2017-09-06 16:49:41,777 [myid:] - INFO  [main:QuorumPeer$QuorumServer@167] - Resolved hostname: localhost to address: localhost/127.0.0.1
2017-09-06 16:49:41,778 [myid:] - INFO  [main:QuorumPeer$QuorumServer@167] - Resolved hostname: localhost to address: localhost/127.0.0.1
2017-09-06 16:49:41,778 [myid:] - INFO  [main:QuorumPeer$QuorumServer@167] - Resolved hostname: localhost to address: localhost/127.0.0.1
2017-09-06 16:49:41,779 [myid:] - ERROR [main:QuorumPeerMain@85] - Invalid config, exiting abnormally
org.apache.zookeeper.server.quorum.QuorumPeerConfig$ConfigException: Error processing ../conf/zoo3.cfg
        at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parse(QuorumPeerConfig.java:154)
        at org.apache.zookeeper.server.quorum.QuorumPeerMain.initializeAndRun(QuorumPeerMain.java:101)
        at org.apache.zookeeper.server.quorum.QuorumPeerMain.main(QuorumPeerMain.java:78)
Caused by: java.lang.IllegalArgumentException: initLimit is not set
        at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parseProperties(QuorumPeerConfig.java:355)
        at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parse(QuorumPeerConfig.java:150)
        ... 2 more
Invalid config, exiting abnormally
~                                   

很明显了吧。我之前的zoo.cfg是自己写进去的,少了initLimit

tickTime=2000
dataDir=/Users/sanbian/Documents/soft/zookeeper/server3/data
dataLogDir=/Users/sanbian/Documents/soft/zookeeper/server3/dataLog
clientPort=2183
server.1=127.0.0.1:8881:7771
server.2=127.0.0.1:8882:7772
server.3=127.0.0.1:8883:7773

按照zoo_sample.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=/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
#
# 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

然后。。就解决了!所以有错还是要先看自己的报错日志啊

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,362评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,330评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,247评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,560评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,580评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,569评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,929评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,587评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,840评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,596评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,678评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,366评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,945评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,929评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,165评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,271评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,403评论 2 342

推荐阅读更多精彩内容