zookeeper 指令碼 zkServer.sh zkCli.sh zkCleanup.sh zkEnv.sh 詳解(一)
阿新 • • 發佈:2019-01-07
#!/bin/sh
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# If this scripted is run out of /usr/bin or some other system bin directory
# it should be linked to and not copied. Things like java jar files are found
# relative to the canonical path of this script.
#
# See the following page for extensive details on setting
# up the JVM to accept JMX remote management:
# http://java.sun.com/javase/6/docs/technotes/guides/management/agent.html
# by default we allow local JMX connections
if [ "x$JMXLOCALONLY" = "x" ] 判斷變數JMXLOCALONLY是否為空 若為空設定 JMXLOCALONLY=false
then
JMXLOCALONLY=false
fi
if [ "x$JMXDISABLE" = "x" ] 變數JMXDISABLE若為空 使用預設的JMX 只允許本地進行連線;若不為空則不允許使用JMX
then
echo "JMX enabled by default" >&2
# for some reason these two options are necessary on jdk6 on Ubuntu
# accord to the docs they are not necessary, but otw jconsole cannot
# do a local attach
ZOOMAIN="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=$JMXLOCALONLY org.apache.zookeeper.server.quorum.QuorumPeerMain"
else
echo "JMX disabled by user request" >&2
ZOOMAIN="org.apache.zookeeper.server.quorum.QuorumPeerMain"
fi
JMX(JMX java Management Extensions java管理擴充套件 這是一個管理功能框架 能獲得系統執行時資訊)
zookeeper 從3.3.0版本開始使用標準的JMX方式來對外提供執行時資訊查詢和便捷的管控介面,zookeeper
預設開啟JMX功能,但是隻限於本地機器連線但不能通過遠端機器進行連線,如果想要JMX通過遠端連線需要對第40行程式碼
進行如下改寫:
ZOOMAIN="-Dcom.sun.management.jmxremote.port=21811
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false
org.apache.zookeeper.server.quorum.QuorumPeerMain"
開啟21811埠不使用許可權認證連線,通常情況下我們可以通過JConsole(Java監視和管理控制檯)來進行JMX連線
# Only follow symlinks if readlink supports it
if readlink -f "$0" > /dev/null 2>&1
then
ZOOBIN=`readlink -f "$0"` 獲取當前指令碼檔案的絕對路徑
else
ZOOBIN="$0"
fi
ZOOBINDIR=`dirname "$ZOOBIN"` 提取當前指令碼所在目錄
. "$ZOOBINDIR"/zkEnv.sh 執行環境設定指令碼
if [ "x$SERVER_JVMFLAGS" ]
then
JVMFLAGS="$SERVER_JVMFLAGS $JVMFLAGS"
fi
if [ "x$2" != "x" ]
then
ZOOCFG="$ZOOCFGDIR/$2"
fi
設定zookeeper配置檔案的絕對路徑
# 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"
echo "Using config:$2" >&2
fi
如果我們在執行zkServer.sh 沒有配置ZOOCFGDIR變數 但是指定了配置檔案的絕對路徑 此時 就需要如上的邏輯加以判斷
直接將配置檔案設為ZOOCFG="$2"
if $cygwin
then
ZOOCFG=`cygpath -wp "$ZOOCFG"`
# cygwin has a "kill" in the shell itself, gets confused
KILL=/bin/kill
else
KILL=kill
fi
echo "Using config: $ZOOCFG" >&2
if [ -z $ZOOPIDFILE ]
then ZOOPIDFILE=$(grep dataDir "$ZOOCFG" | sed -e 's/.*=//')/zookeeper_server.pid
fi
我們在配置zookeeper時必須要新建一個名為 zookeeper_server.pid的檔案 只有1行內容指定的就是zookeeper程序id
一般將這個檔案放置在dataDir配置的目錄下面
_ZOO_DAEMON_OUT="$ZOO_LOG_DIR/zookeeper.out"
在zEnv.sh 指令碼中將ZOO_LOG_DIR變數設定為與當前指令碼為同一級目錄下面
case $1 in
start)
echo -n "Starting zookeeper ... "
if [ -f $ZOOPIDFILE ]; then
if kill -0 `cat $ZOOPIDFILE` > /dev/null 2>&1; then
echo $command already running as process `cat $ZOOPIDFILE`.
exit 0
fi
fi
nohup $JAVA "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \
-cp "$CLASSPATH" $JVMFLAGS $ZOOMAIN "$ZOOCFG" > "$_ZOO_DAEMON_OUT" 2>&1 < /dev/null &
if [ $? -eq 0 ]
then
if /bin/echo -n $! > "$ZOOPIDFILE"
then
sleep 1
echo STARTED
else
echo FAILED TO WRITE PID
exit 1
fi
else
echo SERVER DID NOT START
exit 1
fi
;;
執行命令zkServer.sh start啟動zookeeper服務 kill -0 `cat $ZOOPIDFILE` 先檢查一下server.pid程序是否存在 如果存在就表名該服務已經啟動
如果pid不存在就執行啟動zooKeeper服務命令 執行完成功之後將程序id寫入ZOOPIDFILE檔案中
注意我們這裡啟動是將啟動java命令放入後臺執行 如果想在前臺執行就執行下面的引數start-foreground
start-foreground)
ZOO_CMD="exec $JAVA"
if [ "${ZOO_NOEXEC}" != "" ]; then
ZOO_CMD="$JAVA"
fi
$ZOO_CMD "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \
-cp "$CLASSPATH" $JVMFLAGS $ZOOMAIN "$ZOOCFG"
;;
print-cmd)
echo "$JAVA -Dzookeeper.log.dir=\"${ZOO_LOG_DIR}\" -Dzookeeper.root.logger=\"${ZOO_LOG4J_PROP}\" -cp \"$CLASSPATH\" $JVMFLAGS $ZOOMAIN \"$ZOOCFG\" > \"$_ZOO_DAEMON_OUT\" 2>&1 < /dev/null"
;;
stop)
echo -n "Stopping zookeeper ... "
if [ ! -f "$ZOOPIDFILE" ]
then
echo "no zookeeper to stop (could not find file $ZOOPIDFILE)"
else
$KILL -9 $(cat "$ZOOPIDFILE")
rm "$ZOOPIDFILE"
echo STOPPED
fi
;;
執行命令./zkServer.sh stop 關閉zookeeper服務端程序
upgrade)
shift
echo "upgrading the servers to 3.*"
$JAVA "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \
-cp "$CLASSPATH" $JVMFLAGS org.apache.zookeeper.server.upgrade.UpgradeMain ${@}
echo "Upgrading ... "
;;
升級zookeeper服務
restart)
shift
"$0" stop ${@}
sleep 3
"$0" start ${@}
;;
執行./zkServer.sh restart命令重啟zookeeper服務
先呼叫shift命令左移一個引數 將restart引數移除 然後再執行./zkServer stop 之後程序sleep 3 再執行./zkServer start
status)
# -q is necessary on some versions of linux where nc returns too quickly, and no stat result is output
STAT=`$JAVA "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \
-cp "$CLASSPATH" $JVMFLAGS org.apache.zookeeper.client.FourLetterWordMain localhost \
$(grep "^[[:space:]]*clientPort" "$ZOOCFG" | sed -e 's/.*=//') srvr 2> /dev/null \
| grep Mode`
if [ "x$STAT" = "x" ]
then
echo "Error contacting service. It is probably not running."
exit 1
else
echo $STAT
exit 0
fi
;;
*)
執行srvr命令 輸出服務端資訊 這個狀態不包括客戶端的連線狀況
echo "Usage: $0 {start|start-foreground|stop|restart|status|upgrade|print-cmd}" >&2
esac
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# If this scripted is run out of /usr/bin or some other system bin directory
# it should be linked to and not copied. Things like java jar files are found
# relative to the canonical path of this script.
#
# See the following page for extensive details on setting
# up the JVM to accept JMX remote management:
# http://java.sun.com/javase/6/docs/technotes/guides/management/agent.html
# by default we allow local JMX connections
if [ "x$JMXLOCALONLY" = "x" ] 判斷變數JMXLOCALONLY是否為空 若為空設定 JMXLOCALONLY=false
then
JMXLOCALONLY=false
fi
if [ "x$JMXDISABLE" = "x" ] 變數JMXDISABLE若為空 使用預設的JMX 只允許本地進行連線;若不為空則不允許使用JMX
then
echo "JMX enabled by default" >&2
# for some reason these two options are necessary on jdk6 on Ubuntu
# accord to the docs they are not necessary, but otw jconsole cannot
# do a local attach
ZOOMAIN="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=$JMXLOCALONLY org.apache.zookeeper.server.quorum.QuorumPeerMain"
else
echo "JMX disabled by user request" >&2
ZOOMAIN="org.apache.zookeeper.server.quorum.QuorumPeerMain"
fi
JMX(JMX java Management Extensions java管理擴充套件 這是一個管理功能框架 能獲得系統執行時資訊)
zookeeper 從3.3.0版本開始使用標準的JMX方式來對外提供執行時資訊查詢和便捷的管控介面,zookeeper
預設開啟JMX功能,但是隻限於本地機器連線但不能通過遠端機器進行連線,如果想要JMX通過遠端連線需要對第40行程式碼
進行如下改寫:
ZOOMAIN="-Dcom.sun.management.jmxremote.port=21811
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false
org.apache.zookeeper.server.quorum.QuorumPeerMain"
開啟21811埠不使用許可權認證連線,通常情況下我們可以通過JConsole(Java監視和管理控制檯)來進行JMX連線
# Only follow symlinks if readlink supports it
if readlink -f "$0" > /dev/null 2>&1
then
ZOOBIN=`readlink -f "$0"` 獲取當前指令碼檔案的絕對路徑
else
ZOOBIN="$0"
fi
ZOOBINDIR=`dirname "$ZOOBIN"` 提取當前指令碼所在目錄
. "$ZOOBINDIR"/zkEnv.sh 執行環境設定指令碼
if [ "x$SERVER_JVMFLAGS" ]
then
JVMFLAGS="$SERVER_JVMFLAGS $JVMFLAGS"
fi
if [ "x$2" != "x" ]
then
ZOOCFG="$ZOOCFGDIR/$2"
fi
設定zookeeper配置檔案的絕對路徑
# 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"
echo "Using config:$2" >&2
fi
如果我們在執行zkServer.sh 沒有配置ZOOCFGDIR變數 但是指定了配置檔案的絕對路徑 此時 就需要如上的邏輯加以判斷
直接將配置檔案設為ZOOCFG="$2"
if $cygwin
then
ZOOCFG=`cygpath -wp "$ZOOCFG"`
# cygwin has a "kill" in the shell itself, gets confused
KILL=/bin/kill
else
KILL=kill
fi
echo "Using config: $ZOOCFG" >&2
if [ -z $ZOOPIDFILE ]
then ZOOPIDFILE=$(grep dataDir "$ZOOCFG" | sed -e 's/.*=//')/zookeeper_server.pid
fi
我們在配置zookeeper時必須要新建一個名為 zookeeper_server.pid的檔案 只有1行內容指定的就是zookeeper程序id
一般將這個檔案放置在dataDir配置的目錄下面
_ZOO_DAEMON_OUT="$ZOO_LOG_DIR/zookeeper.out"
在zEnv.sh 指令碼中將ZOO_LOG_DIR變數設定為與當前指令碼為同一級目錄下面
case $1 in
start)
echo -n "Starting zookeeper ... "
if [ -f $ZOOPIDFILE ]; then
if kill -0 `cat $ZOOPIDFILE` > /dev/null 2>&1; then
echo $command already running as process `cat $ZOOPIDFILE`.
exit 0
fi
fi
nohup $JAVA "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \
-cp "$CLASSPATH" $JVMFLAGS $ZOOMAIN "$ZOOCFG" > "$_ZOO_DAEMON_OUT" 2>&1 < /dev/null &
if [ $? -eq 0 ]
then
if /bin/echo -n $! > "$ZOOPIDFILE"
then
sleep 1
echo STARTED
else
echo FAILED TO WRITE PID
exit 1
fi
else
echo SERVER DID NOT START
exit 1
fi
;;
執行命令zkServer.sh start啟動zookeeper服務 kill -0 `cat $ZOOPIDFILE` 先檢查一下server.pid程序是否存在 如果存在就表名該服務已經啟動
如果pid不存在就執行啟動zooKeeper服務命令 執行完成功之後將程序id寫入ZOOPIDFILE檔案中
注意我們這裡啟動是將啟動java命令放入後臺執行 如果想在前臺執行就執行下面的引數start-foreground
start-foreground)
ZOO_CMD="exec $JAVA"
if [ "${ZOO_NOEXEC}" != "" ]; then
ZOO_CMD="$JAVA"
fi
$ZOO_CMD "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \
-cp "$CLASSPATH" $JVMFLAGS $ZOOMAIN "$ZOOCFG"
;;
print-cmd)
echo "$JAVA -Dzookeeper.log.dir=\"${ZOO_LOG_DIR}\" -Dzookeeper.root.logger=\"${ZOO_LOG4J_PROP}\" -cp \"$CLASSPATH\" $JVMFLAGS $ZOOMAIN \"$ZOOCFG\" > \"$_ZOO_DAEMON_OUT\" 2>&1 < /dev/null"
;;
stop)
echo -n "Stopping zookeeper ... "
if [ ! -f "$ZOOPIDFILE" ]
then
echo "no zookeeper to stop (could not find file $ZOOPIDFILE)"
else
$KILL -9 $(cat "$ZOOPIDFILE")
rm "$ZOOPIDFILE"
echo STOPPED
fi
;;
執行命令./zkServer.sh stop 關閉zookeeper服務端程序
upgrade)
shift
echo "upgrading the servers to 3.*"
$JAVA "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \
-cp "$CLASSPATH" $JVMFLAGS org.apache.zookeeper.server.upgrade.UpgradeMain ${@}
echo "Upgrading ... "
;;
升級zookeeper服務
restart)
shift
"$0" stop ${@}
sleep 3
"$0" start ${@}
;;
執行./zkServer.sh restart命令重啟zookeeper服務
先呼叫shift命令左移一個引數 將restart引數移除 然後再執行./zkServer stop 之後程序sleep 3 再執行./zkServer start
status)
# -q is necessary on some versions of linux where nc returns too quickly, and no stat result is output
STAT=`$JAVA "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \
-cp "$CLASSPATH" $JVMFLAGS org.apache.zookeeper.client.FourLetterWordMain localhost \
$(grep "^[[:space:]]*clientPort" "$ZOOCFG" | sed -e 's/.*=//') srvr 2> /dev/null \
| grep Mode`
if [ "x$STAT" = "x" ]
then
echo "Error contacting service. It is probably not running."
exit 1
else
echo $STAT
exit 0
fi
;;
*)
執行srvr命令 輸出服務端資訊 這個狀態不包括客戶端的連線狀況
echo "Usage: $0 {start|start-foreground|stop|restart|status|upgrade|print-cmd}" >&2
esac