社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
ZooKeeper
是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。
ZooKeeper
的目标就是封装好复杂易出错的关键服务,将简单易用的接口和性能高效、功能稳定的系统提供给用户。
ZooKeeper
包含一个简单的原语集,提供Java和C的接口。
ZooKeeper
代码版本中,提供了分布式独享锁、选举、队列的接口,代码在zookeeper-3.4.8srcrecipes。其中分布锁和队列有Java和C两个版本,选举只有Java版本。
下载地址:https://mirrors.cnnic.cn/apache/zookeeper/
zookeeper
要求Java
运行环境,并且需要jdk版本1.6以上。至于JDK的安装可以参考:JDK安装(bin)或JDK安装(tar.gz)
[root@zookeeper-server src]# wget https://mirrors.cnnic.cn/apache/zookeeper/zookeeper-3.4.11/zookeeper-3.4.11.tar.gz
[root@zookeeper-server src]# tar zxvf zookeeper-3.4.11.tar.gz -C /usr/local/
下载并解压ZooKeeper软件压缩包后,可以看到zk包含以下的文件和目录:
上面提到,conf目录下提供了配置的样例zoo_sample.cfg,要将zk运行起来,需要将其名称修改为zoo.cfg。 打开zoo.cfg,可以看到默认的一些配置。
注:修改时,cp一份新的出来作为备份。
[root@zookeeper-server zookeeper-3.4.11]# cd conf/
[root@zookeeper-server conf]# cp zoo_sample.cfg zoo.cfg
[root@zookeeper-server conf]# ls
configuration.xsl log4j.properties zoo.cfg zoo_sample.cfg
来看一下配文件的默认内容:
[root@zookeeper-server conf]# vim 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=/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
我们修改一下dataDir的路径和添加dataLogDir参数:
注:创建相应的目录
dataDir=/usr/local/zookeeper-3.4.11/data
dataLogDir=/usr/local/zookeeper-3.4.11/log
为了以后的操作方便,可以对zookeeper
的环境变量进行配置(该步骤可忽略)。方法如下,在/etc/profile
文件中加入以下内容:
[root@zookeeper-server conf]# vim /etc/profile
#Set Zookeeper Environment
export ZOOKEEPER_HOME=/usr/local/zookeeper-3.4.11/
export PATH=$ZOOKEEPER_HOME/bin:$PATH
export PATH
使 /etc/ 目录下的 profile 文件即可生效:
[root@zookeeper-server conf]# source /etc/profile
[root@zookeeper-server conf]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.11/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
出现以上信息表明启动成功。
# Define some default values that can be overridden by system properties
zookeeper.root.logger=INFO, CONSOLE
改为
# Define some default values that can be overridden by system properties
zookeeper.root.logger=INFO, ROLLINGFILE
#
# Add ROLLINGFILE to rootLogger to get log file output
# Log DEBUG level and above messages to a log file
log4j.appender.ROLLINGFILE=org.apache.log4j.RollingFileAppender
改为—每天一个log日志文件,而不是在同一个log文件中递增日志
#
# Add ROLLINGFILE to rootLogger to get log file output
# Log DEBUG level and above messages to a log file
log4j.appender.ROLLINGFILE=org.apache.log4j.DailyRollingFileAppender
if [ "x${ZOO_LOG_DIR}" = "x" ]
then
ZOO_LOG_DIR="."
fi
if [ "x${ZOO_LOG4J_PROP}" = "x" ]
then
ZOO_LOG4J_PROP="INFO,CONSOLE"
fi
改为
if [ "x${ZOO_LOG_DIR}" = "x" ]
then
ZOO_LOG_DIR="$ZOOBINDIR/../logs"
fi
if [ "x${ZOO_LOG4J_PROP}" = "x" ]
then
ZOO_LOG4J_PROP="INFO,ROLLINGFILE"
fi
启动 Zookeeper 后,可以看到日志的输出情况:
[root@slave-01 log]# pwd
/usr/local/zookeeper-3.4.11/log
[root@slave-01 log]# ll
总用量 40
drwxr-xr-x 2 root root 47 11月 30 14:45 version-2
-rw-r--r-- 1 root root 35337 11月 30 15:21 zookeeper.log
-rw-r--r-- 1 root root 88 11月 30 15:16 zookeeper.out
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!