zookeeper 入门(一)

1 下载安装

wget http://mirrors.hust.edu.cn/apache/zookeeper/stable/zookeeper-3.4.6.tar.gz
cp zookeeper-3.4.6.tar.gz /usr/local
cd /usr/local
tar zxvf zookeeper-3.4.6.tar.gz
ln -s /usr/local/zookeeper-3.4.6 /usr/local/zookeeper

zookeeper 管理脚本

ls -al /usr/local/zookeeper/bin
####################
README.txt
zkCleanup.sh
zkCli.cmd
zkCli.sh
zkEnv.cmd
zkEnv.sh
zkServer.cmd
zkServer.sh

2  分析启动过程

zookeeper 启动命令

/usr/local/zookeeper/bin/zkServer.sh start 

这里简单分析下启动脚本 zkServer.sh

.......

ZOOBIN="${BASH_SOURCE-$0}"
ZOOBIN="$(dirname "${ZOOBIN}")"
ZOOBINDIR="$(cd "${ZOOBIN}"; pwd)" //获得当前脚本文件所在路径 本例中 ZOOBINDIR 的值为 /usr/local/zookeeper/bin if [ -e "$ZOOBIN/../libexec/zkEnv.sh" ]; then
. "$ZOOBINDIR/../libexec/zkEnv.sh"
else
. "$ZOOBINDIR/zkEnv.sh" //执行环境配置脚本
fi

打开 zkEnv.sh

ZOOBINDIR="${ZOOBINDIR:-/usr/bin}"                //如果ZOOBINDIR 为空的话 那么默认指为 /usr/bin
ZOOKEEPER_PREFIX="${ZOOBINDIR}/.." if [ "x$ZOOCFGDIR" = "x" ]
then
if [ -e "${ZOOKEEPER_PREFIX}/conf" ]; then //如果前面 ZOOKEEPER_PREFIX/conf 存在
ZOOCFGDIR="$ZOOBINDIR/../conf" //那么配置文件目录 就为该值 在本例中 为 /usr/local/zookeeper/conf
else
ZOOCFGDIR="$ZOOBINDIR/../etc/zookeeper"
fi
fi if [ -f "${ZOOCFGDIR}/zookeeper-env.sh" ]; then
. "${ZOOCFGDIR}/zookeeper-env.sh"
fi if [ "x$ZOOCFG" = "x" ]
then
ZOOCFG="zoo.cfg" //配置文件默认名称 zoo.cfg
fi ZOOCFG="$ZOOCFGDIR/$ZOOCFG" //完整配置路径 /usr/local/zookeeper/conf/zoo.cfg
......
//后面代码主要是用来声明 classpath

回到zkServer.sh

.......
//看这段代码
if [ "x$2" != "x" ] //如果启动文件 输入了第2个参数 那么配置文件使用指定的文件名
then
ZOOCFG="$ZOOCFGDIR/$2"
fi

再往下面看

# if we give a more complicated path to the config, don't screw around in $ZOOCFGDIR
if [ "x$(dirname "$ZOOCFG")" != "x$ZOOCFGDIR" ]
then
ZOOCFG="$2"
fi

定义了pid 文件路径和  data路径

if [ -z "$ZOOPIDFILE" ]; then
ZOO_DATADIR="$(grep "^[[:space:]]*dataDir" "$ZOOCFG" | sed -e 's/.*=//')"
if [ ! -d "$ZOO_DATADIR" ]; then
mkdir -p "$ZOO_DATADIR"
fi
ZOOPIDFILE="$ZOO_DATADIR/zookeeper_server.pid"
else
# ensure it exists, otw stop will fail
mkdir -p "$(dirname "$ZOOPIDFILE")"
fi

3. 单机启动zookeeper服务

先回到步骤1 安装成功后 做以下操作

cd /usr/local/zookeeper/conf/
cp zoo_sample.cfg zoo.cfg //zoo.cfg 就是启动需要读取的配置文件
mkdir -p /usr/local/zookeeper/data

打开zoo.cfg

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/usr/local/zookeeper/data
# the port at which the clients will connect
clientPort=

启动 zookeeper 服务

/usr/local/zookeeper/bin/zkServer.sh start

启动客户端

/usr/local/zookeeper/bin/zkCli.sh -server 127.0.0.1
上一篇:mfc动态演示排序算法


下一篇:oracle中表加锁死锁的现象、原因及解决方案