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 命令。
- 原因四,难于与其他编程语言或工具进行集成。
- Web interface -- 通过鼠标点击就可以查看各种信息
- REST interface -- 通过 HTTP URI 实现对各种功能的访问
- CLI interface -- 通过 rabbitmqadmin 脚本访问 (Python2.x)
- 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 进行管理和监控。
来源:oschina
链接:https://my.oschina.net/u/617889/blog/170965