zookeeper是构建分布式系统的重要组件,今天尝试了使用homebrew在mac上安装zookeeper,本文是安装过程的完整记录.
安装zookeeper
查看可用版本
使用命令$ brew info zookeeper
查看可用版本信息:
zookeeper: stable 3.4.8, HEAD
Centralized server for distributed coordination of services
https://zookeeper.apache.org/
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/zookeeper.rb
==> Options
--with-perl
Build Perl bindings
--with-python
Build with python support
--HEAD
Install HEAD version
==> Caveats
To have launchd start zookeeper now and restart at login:
brew services start zookeeper
Or, if you don't want/need a background service you can just run:
zkServer start
可见homebrew提供的最新稳定版是3.4.8,zookeeper官网上最新的稳定版是3.4.11.
安装应用
使用命令$ brew install zookeeper
安装:
Error: The following formula:
zookeeper
cannot be installed as a binary package and must be built from source.
To continue, you must install Xcode from the App Store,
or the CLT by running:
xcode-select --install
结果报错了,提示先安装xcode-select,使用命令$ xcode-select --install
安装,输出
xcode-select: note: install requested for command line developer tools
安装好以后再次执行$ brew install zookeeper
:
Warning: You are using OS X 10.13.
We do not provide support for this pre-release version.
You may encounter build failures or other breakages.
Please create pull-requests instead of filing issues.
==> Downloading https://www.apache.org/dyn/closer.cgi?path=zookeeper/zookeeper-3
==> Best Mirror http://mirrors.shu.edu.cn/apache/zookeeper/zookeeper-3.4.8/zooke
curl: (22) The requested URL returned error: 404 Not Found
Trying a mirror...
==> Downloading https://archive.apache.org/dist/zookeeper/zookeeper-3.4.8/zookee
######################################################################## 100.0%
==> ./configure --prefix=/usr/local/Cellar/zookeeper/3.4.8 --without-cppunit
==> make install
==> Caveats
To have launchd start zookeeper now and restart at login:
brew services start zookeeper
Or, if you don't want/need a background service you can just run:
zkServer start
==> Summary
🍺 /usr/local/Cellar/zookeeper/3.4.8: 237 files, 17.6M, built in 52 seconds
此时使用$ ls /usr/local/etc
再次查看配置文件目录:
bash_completion.d mongod.conf redis-sentinel.conf zookeeper
dump.rdb openssl redis.conf
发现多了zookeeper,执行$ cd /usr/local/etc/zookeeper
进入zookeeper,执行$ ls
查看:
defaults log4j.properties zoo.cfg zoo_sample.cfg
发现有zoo.cfg和zoo_sample.cfg这2个配置文件.使用$ cat zoo.cfg
查看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=/usr/local/var/run/zookeeper/data
# 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
zookeeper操作
启动zookeeper
使用命令$ zkServer
,结果启动失败:
ZooKeeper JMX enabled by default
Using config: /usr/local/etc/zookeeper/zoo.cfg
Usage: ./zkServer.sh {start|start-foreground|stop|restart|status|upgrade|print-cmd}
接下来使用$ zkServer start
:
ZooKeeper JMX enabled by default
Using config: /usr/local/etc/zookeeper/zoo.cfg
Starting zookeeper ... STARTED
可见启动成功,并且使用了/usr/local/etc/zookeeper/
下的配置文件zoo.cfg
连接zookeeper
执行命令$ zkCli
,输出
Connecting to localhost:2181
Welcome to ZooKeeper!
JLine support is enabled
[zk: localhost:2181(CONNECTING) 0]
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
就像在这里卡住了一样,随便输入ls
,然后终端变成了
ls
[zk: localhost:2181(CONNECTED) 1] ls
[zk: localhost:2181(CONNECTED) 2] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 3] ls /zookeeper
[quota]
[zk: localhost:2181(CONNECTED) 4]
其实这是JLine交互控制台,会开启一个和zookeeper服务器之间的session.另外,关于jline参考ZooKeeper 初探:安装、使用.可以通过执行quit命令,即[zk: localhost:2181(CONNECTED) 4] quit
退出交互控制台.
查看zookeeper状态
执行$ zkServer status
:
ZooKeeper JMX enabled by default
Using config: /usr/local/etc/zookeeper/zoo.cfg
Mode: standalone
这里的standalone指的是单机模式
关闭zookeeper
执行$ zkServer stop
:
ZooKeeper JMX enabled by default
Using config: /usr/local/etc/zookeeper/zoo.cfg
Stopping zookeeper ... STOPPED