【原創】管理 RabbitMQ 服務器的幾種方式


1.經過 rabbitmqctl 腳本

rabbitmqctl 是 shell 腳本,其經過 exec 調用了 erl 程序。會間接調用到以下兩個 shell 腳本:
  • rabbitmq-env
  • rabbitmq-defaults
在使用該腳本時容許用戶定製的環境變量爲:
  • RABBITMQ_NODENAME          默認值 ${NODENAME}
  • RABBITMQ_CTL_ERL_ARGS       默認值 ${CTL_ERL_ARGS}
下面分別看下這幾個腳本都幹了哪些事情。
rabbitmqctl
[root@Betty chapter-9]# vi /usr/sbin/rabbitmqctl 

#!/bin/sh
##  The contents of this file are subject to the Mozilla Public License
##  Version 1.1 (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.mozilla.org/MPL/
##
##  Software distributed under the License is distributed on an "AS IS"
##  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
##  the License for the specific language governing rights and
##  limitations under the License.
##
##  The Original Code is RabbitMQ.
##
##  The Initial Developer of the Original Code is VMware, Inc.
##  Copyright (c) 2007-2013 VMware, Inc.  All rights reserved.
##

# Get default settings with user overrides for (RABBITMQ_)<var_name>
# Non-empty defaults should be set in rabbitmq-env
. `dirname $0`/rabbitmq-env     # $0 爲 /usr/sbin/rabbitmqctl 因此 `dirname $0`/rabbitmq-env 
                                # 爲 /usr/sbin/rabbitmq-env
                                # 這裏是初始化了一些默認的環境變量值(詳見下面)

##--- Set environment vars RABBITMQ_<var_name> to defaults if not set

# 若未手動設置 $RABBITMQ_NODENAME 則設置其爲默認值 ${NODENAME}
[ "x" = "x$RABBITMQ_NODENAME" ] && RABBITMQ_NODENAME=${NODENAME}
# 若未手動設置 $RABBITMQ_CTL_ERL_ARGS 則設置其值爲默認值 ${CTL_ERL_ARGS}
[ "x" = "x$RABBITMQ_CTL_ERL_ARGS" ] && RABBITMQ_CTL_ERL_ARGS=${CTL_ERL_ARGS}

##--- End of overridden <var_name> variables

exec ${ERL_DIR}erl \
    -pa "${RABBITMQ_HOME}/ebin" \
    -noinput \
    -hidden \
    ${RABBITMQ_CTL_ERL_ARGS} \
    -sname rabbitmqctl$$ \                  # $$ 爲當前進程的 pid
    -boot "${CLEAN_BOOT_FILE}" \
    -s rabbit_control_main \
    -nodename $RABBITMQ_NODENAME \
    -extra "$@"
rabbitmq-env
[root@Betty ~]# vi /usr/sbin/rabbitmq-env

#!/bin/sh
##  The contents of this file are subject to the Mozilla Public License
##  Version 1.1 (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.mozilla.org/MPL/
##
##  Software distributed under the License is distributed on an "AS IS"
##  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
##  the License for the specific language governing rights and
##  limitations under the License.
##
##  The Original Code is RabbitMQ.
##
##  The Initial Developer of the Original Code is VMware, Inc.
##  Copyright (c) 2007-2013 VMware, Inc.  All rights reserved.
##

# Determine where this script is really located (if this script is
# invoked from another script, this is the location of the caller)
SCRIPT_PATH="$0"    # $0 的值應該爲 /usr/sbin/rabbitmq-env
while [ -h "$SCRIPT_PATH" ] ; do       # -h 用於斷定是否爲符號連接
    # readlink -f 獲取符號連接 rabbitmq-env 所對應的完整路徑 /usr/lib/rabbitmq/sbin/rabbitmq-env
    FULL_PATH=`readlink -f $SCRIPT_PATH 2>/dev/null`
    if [ "$?" != "0" ]; then   # 斷定上一步執行結果是否爲成功
    # 直接執行 readlink 將獲取符號連接 rabbitmq-env 所對應的相對路徑 ../lib/rabbitmq/sbin/rabbitmq-env
      REL_PATH=`readlink $SCRIPT_PATH`
      # expr STRING : REGEXP 斷定 $REL_PATH 是否是以 '/' 開頭
      if expr "$REL_PATH" : '/.*' > /dev/null; then
        SCRIPT_PATH="$REL_PATH"   # 以 '/' 開頭
      else
        SCRIPT_PATH="`dirname "$SCRIPT_PATH"`/$REL_PATH"
      fi
    else
      SCRIPT_PATH=$FULL_PATH
    fi
done

SCRIPT_DIR=`dirname $SCRIPT_PATH`    # 獲得 SCRIPT_DIR = /usr/lib/rabbitmq/sbin
RABBITMQ_HOME="${SCRIPT_DIR}/.."     # 獲得 RABBITMQ_HOME = /usr/lib/rabbitmq/
[ "x" = "x$HOSTNAME" ] && HOSTNAME=`env hostname`  # 獲得 HOSTNAME = Betty
NODENAME=rabbit@${HOSTNAME%%.*}      # 獲得 NODENAME = rabbit@Betty 其中 ${HOSTNAME%%.*} 的含義是
                                     # 以 '.' 爲分隔位置,從後開始做最長匹配的刪除
                                     # 若是是一個 '%' 則爲最短匹配

## Set defaults
. ${SCRIPT_DIR}/rabbitmq-defaults    # 初始化默認環境變量(詳見下面)

## Common defaults
SERVER_ERL_ARGS="+K true +A30 +P 1048576 \
  -kernel inet_default_connect_options [{nodelay,true}]"    # 默認的通用配置選項

# warn about old rabbitmq.conf file, if no new one
if [ -f /etc/rabbitmq/rabbitmq.conf ] && \
   [ ! -f ${CONF_ENV_FILE} ] ; then
    echo -n "WARNING: ignoring /etc/rabbitmq/rabbitmq.conf -- "   # echo -n 不輸出換行符
    echo "location has moved to ${CONF_ENV_FILE}"
fi

## Get configuration variables from the configure environment file
[ -f ${CONF_ENV_FILE} ] && . ${CONF_ENV_FILE} # 若是存在 ${CONF_ENV_FILE} 文件則執行該 環境配置 文件
rabbitmq-defaults
[root@Betty ~]# vi /usr/sbin/rabbitmq-defaults 

#!/bin/sh
##  The contents of this file are subject to the Mozilla Public License
##  Version 1.1 (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.mozilla.org/MPL/
##
##  Software distributed under the License is distributed on an "AS IS"
##  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
##  the License for the specific language governing rights and
##  limitations under the License.
##
##  The Original Code is RabbitMQ.
##
##  The Initial Developer of the Original Code is VMware, Inc.
##  Copyright (c) 2012-2013 VMware, Inc.  All rights reserved.
##

### next line potentially updated in package install steps
SYS_PREFIX=

### next line will be updated when generating a standalone release
ERL_DIR=

CLEAN_BOOT_FILE=start_clean
SASL_BOOT_FILE=start_sasl

## Set default values

CONFIG_FILE=${SYS_PREFIX}/etc/rabbitmq/rabbitmq                 # 配置文件路徑
LOG_BASE=${SYS_PREFIX}/var/log/rabbitmq                         # 日誌文件路徑
MNESIA_BASE=${SYS_PREFIX}/var/lib/rabbitmq/mnesia               # 數據庫路徑
ENABLED_PLUGINS_FILE=${SYS_PREFIX}/etc/rabbitmq/enabled_plugins # 記錄已使能插件的文件

PLUGINS_DIR="${RABBITMQ_HOME}/plugins"                          # 插件所在路徑

CONF_ENV_FILE=${SYS_PREFIX}/etc/rabbitmq/rabbitmq-env.conf      # 環境配置文件

2. 經過 RabbitMQ Management plugin
首先想到的問題是:爲何有了 rabbitmqctl 還要 RabbitMQ Management plugin ?
  • 緣由一,若想經過 rabbitmqctl 來對 rabbitmq server 進行操做,要求必需要有 Erlang 運行環境、必須擁有和目標 rabbitmq server 使用的相同 erlang cookie 文件。
  • 緣由二,一旦知足了上述條件,那麼該訪問者將擁有對當前 rabbitmq server 隨心所欲的能力,很是危險。
  • 緣由三,不是全部人都喜歡敲、以及會敲 CLI 命令。
  • 緣由四,難於與其餘編程語言或工具進行集成。
RabbitMQ Management plugin 提供了以下幾種入口:
  • Web interface    --  經過鼠標點擊就能夠查看各類信息
  • REST interface   --   經過 HTTP URI 實現對各類功能的訪問
  • CLI interface      --   經過 rabbitmqadmin 腳本訪問 (Python2.x)
與《Rabbitmq in Action》中的說明進行對照:
  • Management: Web UI
  • Management: HTTP API
  • Management: Command Line Tool 
其中,
  • WEB UI 提供了在 rabbitmq server 上的可視化操做;
  • REST API 提供了一種與其餘語言和工具集成的方式。返回的結構均以 JSON 格式提供,故須要支持 JSON 解析;須要支持 HTTP basic authentication ;須要手動構造完整的 HTTP request 。支持對返回結果的排序、顯示過濾,以及獲取歷史數據。(參考 http://youripaddir:15672/api/)
  • CLI interface 優於 REST-based API 的地方是不須要構建 request 中的所有內容,提供了優雅的格式化的輸出。rabbitmqadmin 已經實現了對 REST API 的封裝,提供了更加簡潔的調用接口,可以對 rabbitmq server 進行管理和監控。
      官網原文以下介紹該工具:rabbitmqadmin 提供了 Web UI 上所具備的所有功能,且更易於在腳本中使用。rabbitmqadmin 是一個特化的 HTTP 客戶端,但若是你打算在本身的應用程序中調用 rabbitmqadmin ,那麼仍是建議你採用直接訪問 HTTP API 的方式 另外, rabb itmqadmin 通常會在增長可執行權限後放到 /usr/local/bin 中。
相關文章
相關標籤/搜索