欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

rabbitmq3.4.2 shell脚本分析

程序员文章站 2022-07-13 15:02:57
...
rabbitmq3.4.2 shell脚本分析 http://aperise.iteye.com/blog/2346584
rabbitmq3.4.2单机安装和集群安装 http://aperise.iteye.com/blog/2346179
rabbitmq3.4.1脑裂问题(转载) http://blog.csdn.net/u013256816/article/details/53291907
spring-amqp官方文档(转载) http://projects.spring.io/spring-amqp/#quick-start
spring-amqp在GITHUB源码(转载) https://github.com/spring-projects/spring-amqp
  • rabbitmq3.4.2提供了哪些shell脚本?脚本之间调用关系?
  • rabbitmq3.4.2各个shell脚本详细解析?
  • rabbitmq3.4.2的默认配置文件在哪里?默认插件放在哪里?日志放在哪里?
  • 为什么有了rabitmqctl还需要RabbitMQ Management plugin?
  • Rattitmq Management plugin提供了哪些功能?

1.rabbitmq3.4.2提供了哪些shell脚本?脚本之间调用关系?

    这里安装的rabbitmq版本为3.4.2,安装方式是编译安装,安装位于目录/opt/rabbitmq-3.4.2,在/opt/rabbitmq-server-3.4.2/sbin下面提供了如下5个shell脚本:

rabbitmq3.4.2 shell脚本分析
            
    
    博客分类: 消息队列rabbitmq rabbitmq3.4.2配置 

脚本 说明
rabbitmq-defaults shell脚本,初始化关键的配置默认信息
rabbitmq-env shell脚本,设置运行环境各种默认配置
rabbitmqctl

rabbitmqctl本质是shell脚本里通过erl运行程序

/opt//opt/rabbitmq-server-3.4.2

/ebin/rabbit_control_main.beam这个erlang程序

rabbitmq-plugins

rabbitmq-plugins本质是shell脚本里通过erl运行

/opt/rabbitmq-server-3.4.2

/ebin/rabbit_plugins.beam这个erlang程序

rabbitmq-server

rabbitmq-server本质是shell通过erl运行

/opt/rabbitmq-server-3.4.2

/ebin/rrabbit.beam这个erlang程序

 

rabbitmq3.4.2 shell脚本分析
            
    
    博客分类: 消息队列rabbitmq rabbitmq3.4.2配置 

    从上面图可以看出:5个脚本其实平时用到最多的是rabbitmqctl、rabbitmq-server、rabbitmq-plugins,而rabbitmq-env和rabbitmq-defaults是前面3个脚本的运行前提,主要提供运行环境的初始设置和整个环境的默认设置,在用户未做任何配置情况下,这些脚本本身就考虑到了很多默认设置,方便用户使用,在必要时候提醒相关错误信息

 

2.rabbitmq3.4.2各个shell脚本详细解析?

    2.1 linux中shell变量的含义: 

  • $$      Shell本身的PID(ProcessID) 
  • $!      Shell最后运行的后台Process的PID 
  • $?      最后运行的命令的结束代码(返回值) 
  • $-      使用Set命令设定的Flag一览 
  • $*      所有参数列表。如"$*"用「"」括起来的情况、以"$1 $2 … $n"的形式输出所有参数。 
  • $@      所有参数列表。如"$@"用「"」括起来的情况、以"$1" "$2" … "$n" 的形式输出所有参数。 
  • $#      添加到Shell的参数个数 
  • $0      Shell本身的文件名 
  • $1~$n  添加到Shell的各参数值。$1是第1参数、$2是第2参数…。 

    2.2 rabbitmq-defaults

#!/bin/sh -e
##  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 GoPivotal, Inc.
##  Copyright (c) 2012-2014 GoPivotal, 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

# 继承rabbitmq-env已有配置的子配置文件,注意不是目录
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"

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

    这里为了打印rabbitmq-defaults里的变量值,故在rabbitmq-defaults文件里末尾追加如下脚本,如下:

echo "SYS_PREFIX:"${SYS_PREFIX}
echo "CLEAN_BOOT_FILE:"${CLEAN_BOOT_FILE}
echo "SASL_BOOT_FILE:"${SASL_BOOT_FILE}
echo "CONFIG_FILE:"${CONFIG_FILE}
echo "LOG_BASE:"${LOG_BASE}
echo "MNESIA_BASE:"${MNESIA_BASE}
echo "ENABLED_PLUGINS_FILE:"${ENABLED_PLUGINS_FILE}
echo "PLUGINS_DIR:"${PLUGINS_DIR}
echo "CONF_ENV_FILE:"${CONF_ENV_FILE}

    然后执行rabbitmq-defaults得到的结果如下:

[root@slavery01 sbin]# rabbitmq-defaults 
SYS_PREFIX:
CLEAN_BOOT_FILE:start_clean
SASL_BOOT_FILE:start_sasl
CONFIG_FILE:/etc/rabbitmq/rabbitmq
LOG_BASE:/var/log/rabbitmq
MNESIA_BASE:/var/lib/rabbitmq/mnesia
ENABLED_PLUGINS_FILE:/etc/rabbitmq/enabled_plugins
PLUGINS_DIR:/plugins
CONF_ENV_FILE:/etc/rabbitmq/rabbitmq-env.conf
[root@slavery01 sbin]# 

    说明:

  • sbin/rabbitmq-defaults主要初始化了几个重要的变量值,这些值如果你要修改,请不要修改文件sbin/rabbitmq-defaults文件,这个文件是初始化的默认值,要覆盖的话,请在文件/etc/rabbitmq/rabbitmq文件中进行修改;
  • 如果安装完后rabbitmq不做任何配置,默认rabbitmq的配置文件为/etc/rabbitmq/rabbitmq,环境配置文件为/etc/rabbitmq/rabbitmq-env.conf,日志放置目录为/var/log/rabbitmq ,数据持久化目录为/var/lib/rabbitmq/mnesia,启用的插件目录位于/etc/rabbitmq/enabled_plugins
  • 上面这些默认配置很关键,在很多博客是一上来就生硬的告诉读者去/etc/rabbitmq目录配置rabbitmq相关信息,所以学习任何一门开源框架技术,搞懂其脚本调用关系和脚本详细执行过程,特别是里面的变量,对于快速学习非常重要。

 

    2.3 rabbitmq-env

#!/bin/sh -e
#shell编程是以"#"为注释,但对"#!/bin/sh"却不是。"#!/bin/sh"是对shell的声明,说明你所用的是那种类型的shell及其路径所在。
##  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 GoPivotal, Inc.
##  Copyright (c) 2007-2014 GoPivotal, Inc.  All rights reserved.
##

# We set +e here since since our test for "readlink -f" below needs to
# be able to fail.
set +e
# 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"    #shell自身的文件名,这里指/opt/rabbitmq-server-3.4.2/sbin/rabbitmq-env
while [ -h "$SCRIPT_PATH" ] ; do    # -h 用于判定是否为符号链接
    # Determine if readlink -f is supported at all. TODO clean this up.
    # readlink -f 获取符号链接 rabbitmq-env 所对应的完整路径 /opt/rabbitmq-server-3.4.2/sbin/rabbitmq-env
    FULL_PATH=`readlink -f $SCRIPT_PATH 2>/dev/null`
    if [ "$?" != "0" ]; then     # 判定上一步执行结果是否为成功
      REL_PATH=`readlink $SCRIPT_PATH`    #执行readlink获取符号链接rabbitmq-env所对应的相对路径 ../sbin/rabbitmq-env
      #正则表达式判定 $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
set -e

SCRIPT_DIR=`dirname $SCRIPT_PATH`    # 得到 SCRIPT_DIR = /opt/rabbitmq-server-3.4.2/sbin
RABBITMQ_HOME="${SCRIPT_DIR}/.."     # 得到 RABBITMQ_HOME = /opt/rabbitmq-server-3.4.2/sbin/..

## Set defaults
. ${SCRIPT_DIR}/rabbitmq-defaults    # 调用并执行脚本rabbitmq-defaults

## Common defaults
# 默认的通用配置选项SERVER_ERL_ARGS=+K true +A30 +P 1048576 -kernel inet_default_connect_options [{nodelay,true}]
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 "location has moved to ${CONF_ENV_FILE}"
fi

## Get configuration variables from the configure environment file
[ -f ${CONF_ENV_FILE} ] && . ${CONF_ENV_FILE} || true

[ "x" = "x$RABBITMQ_USE_LONGNAME" ] && RABBITMQ_USE_LONGNAME=${USE_LONGNAME}
if [ "xtrue" = "x$RABBITMQ_USE_LONGNAME" ] ; then
    RABBITMQ_NAME_TYPE=-name
    [ "x" = "x$HOSTNAME" ] && HOSTNAME=`env hostname --fqdn`
    [ "x" = "x$NODENAME" ] && NODENAME=rabbit@${HOSTNAME}
else
    RABBITMQ_NAME_TYPE=-sname
    [ "x" = "x$HOSTNAME" ] && HOSTNAME=`env hostname`
    [ "x" = "x$NODENAME" ] && NODENAME=rabbit@${HOSTNAME%%.*}
fi

##--- Set environment vars RABBITMQ_<var_name> to defaults if not set
#如果相关参数没有配置那么表达式"x" = "x$参数KEY"永远成立,则设置相关参数的默认值
DEFAULT_NODE_IP_ADDRESS=auto
DEFAULT_NODE_PORT=5672
[ "x" = "x$RABBITMQ_NODE_IP_ADDRESS" ] && RABBITMQ_NODE_IP_ADDRESS=${NODE_IP_ADDRESS}
[ "x" = "x$RABBITMQ_NODE_PORT" ] && RABBITMQ_NODE_PORT=${NODE_PORT}

[ "x" = "x$RABBITMQ_NODE_IP_ADDRESS" ] && [ "x" != "x$RABBITMQ_NODE_PORT" ] && RABBITMQ_NODE_IP_ADDRESS=${DEFAULT_NODE_IP_ADDRESS}
[ "x" != "x$RABBITMQ_NODE_IP_ADDRESS" ] && [ "x" = "x$RABBITMQ_NODE_PORT" ] && RABBITMQ_NODE_PORT=${DEFAULT_NODE_PORT}

[ "x" = "x$RABBITMQ_DIST_PORT" ] && RABBITMQ_DIST_PORT=${DIST_PORT}
[ "x" = "x$RABBITMQ_DIST_PORT" ] && [ "x" = "x$RABBITMQ_NODE_PORT" ] && RABBITMQ_DIST_PORT=$((${DEFAULT_NODE_PORT} + 20000))
[ "x" = "x$RABBITMQ_DIST_PORT" ] && [ "x" != "x$RABBITMQ_NODE_PORT" ] && RABBITMQ_DIST_PORT=$((${RABBITMQ_NODE_PORT} + 20000))

[ "x" = "x$RABBITMQ_NODENAME" ] && RABBITMQ_NODENAME=${NODENAME}
[ "x" = "x$RABBITMQ_SERVER_ERL_ARGS" ] && RABBITMQ_SERVER_ERL_ARGS=${SERVER_ERL_ARGS}
[ "x" = "x$RABBITMQ_CONFIG_FILE" ] && RABBITMQ_CONFIG_FILE=${CONFIG_FILE}
[ "x" = "x$RABBITMQ_LOG_BASE" ] && RABBITMQ_LOG_BASE=${LOG_BASE}
[ "x" = "x$RABBITMQ_MNESIA_BASE" ] && RABBITMQ_MNESIA_BASE=${MNESIA_BASE}
[ "x" = "x$RABBITMQ_SERVER_START_ARGS" ] && RABBITMQ_SERVER_START_ARGS=${SERVER_START_ARGS}
[ "x" = "x$RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS" ] && RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS=${SERVER_ADDITIONAL_ERL_ARGS}
[ "x" = "x$RABBITMQ_MNESIA_DIR" ] && RABBITMQ_MNESIA_DIR=${MNESIA_DIR}
[ "x" = "x$RABBITMQ_MNESIA_DIR" ] && RABBITMQ_MNESIA_DIR=${RABBITMQ_MNESIA_BASE}/${RABBITMQ_NODENAME}

[ "x" = "x$RABBITMQ_PID_FILE" ] && RABBITMQ_PID_FILE=${PID_FILE}
[ "x" = "x$RABBITMQ_PID_FILE" ] && RABBITMQ_PID_FILE=${RABBITMQ_MNESIA_DIR}.pid

[ "x" = "x$RABBITMQ_PLUGINS_EXPAND_DIR" ] && RABBITMQ_PLUGINS_EXPAND_DIR=${PLUGINS_EXPAND_DIR}
[ "x" = "x$RABBITMQ_PLUGINS_EXPAND_DIR" ] && RABBITMQ_PLUGINS_EXPAND_DIR=${RABBITMQ_MNESIA_BASE}/${RABBITMQ_NODENAME}-plugins-expand

[ "x" = "x$RABBITMQ_ENABLED_PLUGINS_FILE" ] && RABBITMQ_ENABLED_PLUGINS_FILE=${ENABLED_PLUGINS_FILE}

[ "x" = "x$RABBITMQ_PLUGINS_DIR" ] && RABBITMQ_PLUGINS_DIR=${PLUGINS_DIR}

## Log rotation
[ "x" = "x$RABBITMQ_LOGS" ] && RABBITMQ_LOGS=${LOGS}
[ "x" = "x$RABBITMQ_LOGS" ] && RABBITMQ_LOGS="${RABBITMQ_LOG_BASE}/${RABBITMQ_NODENAME}.log"
[ "x" = "x$RABBITMQ_SASL_LOGS" ] && RABBITMQ_SASL_LOGS=${SASL_LOGS}
[ "x" = "x$RABBITMQ_SASL_LOGS" ] && RABBITMQ_SASL_LOGS="${RABBITMQ_LOG_BASE}/${RABBITMQ_NODENAME}-sasl.log"

[ "x" = "x$RABBITMQ_CTL_ERL_ARGS" ] && RABBITMQ_CTL_ERL_ARGS=${CTL_ERL_ARGS}

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

    这里为了打印rabbitmq-env里的变量值,故在rabbitmq-env文件里末尾追加如下脚本,如下:

echo "SCRIPT_PATH:"${SCRIPT_PATH}
echo "SCRIPT_DIR:"${SCRIPT_DIR}
echo "RABBITMQ_HOME:"${RABBITMQ_HOME}
echo "SERVER_ERL_ARGS:"${SERVER_ERL_ARGS}
echo "CONF_ENV_FILE:"${CONF_ENV_FILE}
echo "RABBITMQ_USE_LONGNAME:"${RABBITMQ_USE_LONGNAME}
echo "HOSTNAME:"${HOSTNAME}
echo "NODENAME:"${NODENAME}
echo "RABBITMQ_NODE_IP_ADDRESS:"${RABBITMQ_NODE_IP_ADDRESS}
echo "RABBITMQ_NODE_PORT:"${RABBITMQ_NODE_PORT}
echo "RABBITMQ_DIST_PORT:"${RABBITMQ_DIST_PORT}
echo "RABBITMQ_NODENAME:"${RABBITMQ_NODENAME}
echo "RABBITMQ_SERVER_ERL_ARGS:"${RABBITMQ_SERVER_ERL_ARGS}
echo "RABBITMQ_CONFIG_FILE:"${RABBITMQ_CONFIG_FILE}
echo "RABBITMQ_LOG_BASE:"${RABBITMQ_LOG_BASE}
echo "RABBITMQ_MNESIA_BASE:"${RABBITMQ_MNESIA_BASE}
echo "RABBITMQ_SERVER_START_ARGS:"${RABBITMQ_SERVER_START_ARGS}
echo "RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS:"${RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS}
echo "RABBITMQ_MNESIA_DIR:"${RABBITMQ_MNESIA_DIR}
echo "RABBITMQ_PID_FILE:"${RABBITMQ_PID_FILE}
echo "RABBITMQ_PLUGINS_EXPAND_DIR:"${RABBITMQ_PLUGINS_EXPAND_DIR}
echo "RABBITMQ_ENABLED_PLUGINS_FILE:"${RABBITMQ_ENABLED_PLUGINS_FILE}
echo "RABBITMQ_PLUGINS_DIR:"${RABBITMQ_PLUGINS_DIR}
echo "RABBITMQ_LOGS:"${RABBITMQ_LOGS}
echo "RABBITMQ_SASL_LOGS:"${RABBITMQ_SASL_LOGS}
echo "RABBITMQ_CTL_ERL_ARGS:"${RABBITMQ_CTL_ERL_ARGS}

    然后执行rabbitmq-env得到的结果如下:

[root@slavery01 sbin]# rabbitmq-env 
SCRIPT_PATH:/opt/rabbitmq-server-3.4.2/sbin/rabbitmq-env
SCRIPT_DIR:/opt/rabbitmq-server-3.4.2/sbin
RABBITMQ_HOME:/opt/rabbitmq-server-3.4.2/sbin/..
SERVER_ERL_ARGS:+K true +A30 +P 1048576 -kernel inet_default_connect_options [{nodelay,true}]
CONF_ENV_FILE:/etc/rabbitmq/rabbitmq-env.conf
RABBITMQ_USE_LONGNAME:
HOSTNAME:slavery01
NODENAME:rabbit@slavery01
RABBITMQ_NODE_IP_ADDRESS:
RABBITMQ_NODE_PORT:
RABBITMQ_DIST_PORT:25672
RABBITMQ_NODENAME:rabbit@slavery01
RABBITMQ_SERVER_ERL_ARGS:+K true +A30 +P 1048576 -kernel inet_default_connect_options [{nodelay,true}]
RABBITMQ_CONFIG_FILE:/etc/rabbitmq/rabbitmq
RABBITMQ_LOG_BASE:/var/log/rabbitmq
RABBITMQ_MNESIA_BASE:/var/lib/rabbitmq/mnesia
RABBITMQ_SERVER_START_ARGS:
RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS:
RABBITMQ_MNESIA_DIR:/var/lib/rabbitmq/mnesia/rabbit@slavery01
RABBITMQ_PID_FILE:/var/lib/rabbitmq/mnesia/rabbit@slavery01.pid
RABBITMQ_PLUGINS_EXPAND_DIR:/var/lib/rabbitmq/mnesia/rabbit@slavery01-plugins-expand
RABBITMQ_ENABLED_PLUGINS_FILE:/etc/rabbitmq/enabled_plugins
RABBITMQ_PLUGINS_DIR:/opt/rabbitmq-server-3.4.2/sbin/../plugins
RABBITMQ_LOGS:/var/log/rabbitmq/rabbit@slavery01.log
RABBITMQ_SASL_LOGS:/var/log/rabbitmq/rabbit@slavery01-sasl.log
RABBITMQ_CTL_ERL_ARGS:
[root@slavery01 sbin]# 

    说明:

  • sbin/rabbitmq-env是对所有环境变量进行默认设置;
  • 如果你要修改默认环境变量值,请在文件/etc/rabbitmq/rabbitmq-env.conf进行修改;

 

    2.4 rabbitmqctl

#!/bin/sh -e
##  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 GoPivotal, Inc.
##  Copyright (c) 2007-2014 GoPivotal, 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    #调用并执行脚本rabbitmq-env

#epmd是Erlang Port Mapper Daemon的缩写,epmd完成Erlang节点和IP,端口的映射关系,首先要保证epmd处于运行状态,这里先让它运行
# rabbitmqctl starts distribution itself, so we need to make sure epmd
# is running.
${ERL_DIR}erl ${RABBITMQ_NAME_TYPE} rabbitmqctl-prelaunch-$$ -noinput \
-eval 'erlang:halt().' -boot "${CLEAN_BOOT_FILE}"

# We specify Mnesia dir and sasl error logger since some actions
# (e.g. forget_cluster_node --offline) require us to impersonate the
# real node.
RABBITMQ_USE_LONGNAME=${RABBITMQ_USE_LONGNAME} \
exec ${ERL_DIR}erl \
    -pa "${RABBITMQ_HOME}/ebin" \
    -noinput \
    -hidden \
    ${RABBITMQ_CTL_ERL_ARGS} \
    -boot "${CLEAN_BOOT_FILE}" \
    -sasl errlog_type error \
    -mnesia dir "\"${RABBITMQ_MNESIA_DIR}\"" \
    -s rabbit_control_main \
    -nodename $RABBITMQ_NODENAME \
    -extra "$@"

    这里为了打印rabbitmqctl里的变量值,故在rabbitmqctl文件里末尾追加如下脚本,如下:

 

 

echo "ERL_DIR:"${ERL_DIR}
echo "RABBITMQ_NAME_TYPE:"${RABBITMQ_NAME_TYPE}
echo "CLEAN_BOOT_FILE:"${CLEAN_BOOT_FILE}
echo "RABBITMQ_USE_LONGNAME:"${RABBITMQ_USE_LONGNAME}
echo "RABBITMQ_HOME:"${RABBITMQ_HOME}
echo "RABBITMQ_CTL_ERL_ARGS:"${RABBITMQ_CTL_ERL_ARGS}
echo "RABBITMQ_MNESIA_DIR:"${RABBITMQ_MNESIA_DIR}
echo "RABBITMQ_NODENAME:"${RABBITMQ_NODENAME}

    然后执行rabbitmqctl得到的结果如下:

[root@slavery01 sbin]# rabbitmqctl
ERL_DIR:
RABBITMQ_NAME_TYPE:-sname
CLEAN_BOOT_FILE:start_clean
RABBITMQ_USE_LONGNAME:
RABBITMQ_HOME:/opt/rabbitmq-server-3.4.2/sbin/..
RABBITMQ_CTL_ERL_ARGS:
RABBITMQ_MNESIA_DIR:/var/lib/rabbitmq/mnesia/rabbit@slavery01
RABBITMQ_NODENAME:rabbit@slavery01
[root@slavery01 sbin]#

    说明:

  • sbin/rabbitmqctl本质是shell脚本里通过erl运行程序/opt//opt/rabbitmq-server-3.4.2/ebin/rabbit_control_main.beam这个erlang程序;
  • rabbitmqctl需要erlang语言环境支持,不然无法运行;

 

    2.5 rabbitmq-plugins

#!/bin/sh -e
##  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 GoPivotal, Inc.
##  Copyright (c) 2007-2014 GoPivotal, 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    #调用并执行脚本rabbitmq-env

exec ${ERL_DIR}erl \
    -pa "${RABBITMQ_HOME}/ebin" \
    -noinput \
    -hidden \
    ${RABBITMQ_NAME_TYPE} rabbitmq-plugins$$ \
    -boot "${CLEAN_BOOT_FILE}" \
    -s rabbit_plugins_main \
    -enabled_plugins_file "$RABBITMQ_ENABLED_PLUGINS_FILE" \
    -plugins_dist_dir "$RABBITMQ_PLUGINS_DIR" \
    -nodename $RABB

    这里为了打印rabbitmq-plugins里的变量值,故在rabbitmq-plugins文件里末尾追加如下脚本,如下:

echo "ERL_DIR:"${ERL_DIR}
echo "RABBITMQ_HOME:"${RABBITMQ_HOME}
echo "RABBITMQ_NAME_TYPE:"${RABBITMQ_NAME_TYPE}
echo "CLEAN_BOOT_FILE:"${CLEAN_BOOT_FILE}
echo "RABBITMQ_ENABLED_PLUGINS_FILE:"${RABBITMQ_ENABLED_PLUGINS_FILE}
echo "RABBITMQ_PLUGINS_DIR:"${RABBITMQ_PLUGINS_DIR}
echo "RABB:"${RABB}

    然后执行rabbitmq-plugins得到的结果如下:

[root@slavery01 sbin]# rabbitmq-plugins 
ERL_DIR:
RABBITMQ_HOME:/opt/rabbitmq-server-3.4.2/sbin/..
RABBITMQ_NAME_TYPE:-sname
CLEAN_BOOT_FILE:start_clean
RABBITMQ_ENABLED_PLUGINS_FILE:/etc/rabbitmq/enabled_plugins
RABBITMQ_PLUGINS_DIR:/opt/rabbitmq-server-3.4.2/sbin/../plugins
RABB:
[root@slavery01 sbin]#

   说明:

  • rabbitmq-plugins本质是shell脚本里通过erl运行/opt/rabbitmq-server-3.4.2/ebin/rabbit_plugins.beam这个erlang程序;
  • rabbitmq-plugins需要erlang语言环境支持;
  • rabbitmq-plugins主要用于插件管理;

 

    2.6 rabbitmq-server

#!/bin/sh -e
#shell编程是以"#"为注释,但对"#!/bin/sh"却不是。"#!/bin/sh"是对shell的声明,说明你所用的是那种类型的shell及其路径所在。
##  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 GoPivotal, Inc.
##  Copyright (c) 2007-2014 GoPivotal, 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    #调用并执行脚本rabbitmq-env

#如果相关参数没有配置,那么表达式"x" = "x$参数KEY"永远成立,则设置相关参数的默认值
RABBITMQ_START_RABBIT=
[ "x" = "x$RABBITMQ_ALLOW_INPUT" ] && RABBITMQ_START_RABBIT=" -noinput"
[ "x" = "x$RABBITMQ_NODE_ONLY" ] && RABBITMQ_START_RABBIT="$RABBITMQ_START_RABBIT -s rabbit boot "

#CYGWIN不会记录进程ID
#后台运行模式时候不会生成进程ID文件
case "$(uname -s)" in
  CYGWIN*) # we make no attempt to record the cygwin pid; rabbitmqctl wait
           # will not be able to make sense of it anyway
           ;;
  *)       # When -detached is passed, we don't write the pid, since it'd be the
           # wrong one
           detached=""
           for opt in "$@"; do
               if [ "$opt" = "-detached" ]; then
                   detached="true"
               fi
           done
           if [ $detached ]; then
               echo "Warning: PID file not written; -detached was passed." 1>&2
           else
               mkdir -p $(dirname ${RABBITMQ_PID_FILE});
               echo $$ > ${RABBITMQ_PID_FILE}
           fi
esac

RABBITMQ_EBIN_ROOT="${RABBITMQ_HOME}/ebin"

#告诉bash如果后续任何语句的执行结果不是true则应该退出
set +e

RABBITMQ_CONFIG_FILE=$RABBITMQ_CONFIG_FILE \
RABBITMQ_DIST_PORT=$RABBITMQ_DIST_PORT \
    ${ERL_DIR}erl -pa "$RABBITMQ_EBIN_ROOT" \
    -boot "${CLEAN_BOOT_FILE}" \
    -noinput \
    -hidden \
    -s rabbit_prelaunch \
    ${RABBITMQ_NAME_TYPE} rabbitmqprelaunch$$ \
    -extra "${RABBITMQ_NODENAME}"

PRELAUNCH_RESULT=$?
if [ ${PRELAUNCH_RESULT} = 2 ] ; then
    # dist port is mentioned in config, so do not set it
    true
elif [ ${PRELAUNCH_RESULT} = 0 ] ; then
    # dist port is not mentioned in the config file, we can set it
    RABBITMQ_DIST_ARG="-kernel inet_dist_listen_min ${RABBITMQ_DIST_PORT} -kernel inet_dist_listen_max ${RABBITMQ_DIST_PORT}"
else
    exit ${PRELAUNCH_RESULT}
fi

set -e

RABBITMQ_CONFIG_ARG=
[ -f "${RABBITMQ_CONFIG_FILE}.config" ] && RABBITMQ_CONFIG_ARG="-config ${RABBITMQ_CONFIG_FILE}"

RABBITMQ_LISTEN_ARG=
[ "x" != "x$RABBITMQ_NODE_PORT" ] && [ "x" != "x$RABBITMQ_NODE_IP_ADDRESS" ] && RABBITMQ_LISTEN_ARG="-rabbit tcp_listeners [{\""${RABBITMQ_NODE_IP_ADDRESS}"\","${RABBITMQ_NODE_PORT}"}]"

# we need to turn off path expansion because some of the vars, notably
# RABBITMQ_SERVER_ERL_ARGS, contain terms that look like globs and
# there is no other way of preventing their expansion.
set -f

RABBITMQ_CONFIG_FILE=$RABBITMQ_CONFIG_FILE \
exec ${ERL_DIR}erl \
    -pa ${RABBITMQ_EBIN_ROOT} \
    ${RABBITMQ_START_RABBIT} \
    ${RABBITMQ_NAME_TYPE} ${RABBITMQ_NODENAME} \
    -boot "${SASL_BOOT_FILE}" \
    ${RABBITMQ_CONFIG_ARG} \
    +W w \
    ${RABBITMQ_SERVER_ERL_ARGS} \
    ${RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS} \
    ${RABBITMQ_LISTEN_ARG} \
    -sasl errlog_type error \
    -sasl sasl_error_logger false \
    -rabbit error_logger '{file,"'${RABBITMQ_LOGS}'"}' \
    -rabbit sasl_error_logger '{file,"'${RABBITMQ_SASL_LOGS}'"}' \
    -rabbit enabled_plugins_file "\"$RABBITMQ_ENABLED_PLUGINS_FILE\"" \
    -rabbit plugins_dir "\"$RABBITMQ_PLUGINS_DIR\"" \
    -rabbit plugins_expand_dir "\"$RABBITMQ_PLUGINS_EXPAND_DIR\"" \
    -os_mon start_cpu_sup false \
    -os_mon start_disksup false \
    -os_mon start_memsup false \
    -mnesia dir "\"${RABBITMQ_MNESIA_DIR}\"" \
    ${RABBITMQ_SERVER_START_ARGS} \
    ${RABBITMQ_DIST_ARG} \
    "$@"

    这里为了打印rabbitmq-server里的变量值,故在rabbitmq-server文件里末尾追加如下脚本,如下:

 

 

echo "RABBITMQ_START_RABBIT:"${RABBITMQ_START_RABBIT}
echo "RABBITMQ_ALLOW_INPUT:"${RABBITMQ_ALLOW_INPUT}
echo "RABBITMQ_NODE_ONLY:"${RABBITMQ_NODE_ONLY}
echo "RABBITMQ_PID_FILE:"${RABBITMQ_PID_FILE}
echo "RABBITMQ_HOME:"${RABBITMQ_HOME}
echo "RABBITMQ_EBIN_ROOT:"${RABBITMQ_EBIN_ROOT}
echo "RABBITMQ_CONFIG_FILE:"${RABBITMQ_CONFIG_FILE}
echo "ERL_DIR:"${ERL_DIR}
echo "CLEAN_BOOT_FILE:"${CLEAN_BOOT_FILE}
echo "RABBITMQ_NAME_TYPE:"${RABBITMQ_NAME_TYPE}
echo "RABBITMQ_NODENAME:"${RABBITMQ_NODENAME}
echo "PRELAUNCH_RESULT:"${PRELAUNCH_RESULT}
echo "RABBITMQ_DIST_ARG:"${RABBITMQ_DIST_ARG}
echo "RABBITMQ_DIST_PORT:"${RABBITMQ_DIST_PORT}
echo "RABBITMQ_CONFIG_ARG:"${RABBITMQ_CONFIG_ARG}
echo "RABBITMQ_NODE_PORT:"${RABBITMQ_NODE_PORT}
echo "RABBITMQ_NODE_IP_ADDRESS:"${RABBITMQ_NODE_IP_ADDRESS}
echo "SASL_BOOT_FILE:"${SASL_BOOT_FILE}
echo "RABBITMQ_SERVER_ERL_ARGS:"${RABBITMQ_SERVER_ERL_ARGS}
echo "RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS:"${RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS}
echo "RABBITMQ_LISTEN_ARG:"${RABBITMQ_LISTEN_ARG}
echo "RABBITMQ_LOGS:"${RABBITMQ_LOGS}
echo "RABBITMQ_SASL_LOGS:"${RABBITMQ_SASL_LOGS}
echo "RABBITMQ_ENABLED_PLUGINS_FILE:"${RABBITMQ_ENABLED_PLUGINS_FILE}
echo "RABBITMQ_PLUGINS_DIR:"${RABBITMQ_PLUGINS_DIR}
echo "RABBITMQ_PLUGINS_EXPAND_DIR:"${RABBITMQ_PLUGINS_EXPAND_DIR}
echo "RABBITMQ_MNESIA_DIR:"${RABBITMQ_MNESIA_DIR}
echo "RABBITMQ_SERVER_START_ARGS:"${RABBITMQ_SERVER_START_ARGS}
echo "RABBITMQ_DIST_ARG:"${RABBITMQ_DIST_ARG}

    然后执行rabbitmq-server得到的结果如下:

[root@slavery01 sbin]# rabbitmq-server
RABBITMQ_START_RABBIT: -noinput -s rabbit boot
RABBITMQ_ALLOW_INPUT:
RABBITMQ_NODE_ONLY:
RABBITMQ_PID_FILE:/var/lib/rabbitmq/mnesia/rabbit@slavery01.pid
RABBITMQ_HOME:/opt/rabbitmq-server-3.4.2/sbin/..
RABBITMQ_EBIN_ROOT:/opt/rabbitmq-server-3.4.2/sbin/../ebin
RABBITMQ_CONFIG_FILE:/etc/rabbitmq/rabbitmq
ERL_DIR:
CLEAN_BOOT_FILE:start_clean
RABBITMQ_NAME_TYPE:-sname
RABBITMQ_NODENAME:rabbit@slavery01
PRELAUNCH_RESULT:0
RABBITMQ_DIST_ARG:-kernel inet_dist_listen_min 25672 -kernel inet_dist_listen_max 25672
RABBITMQ_DIST_PORT:25672
RABBITMQ_CONFIG_ARG:
RABBITMQ_NODE_PORT:
RABBITMQ_NODE_IP_ADDRESS:
SASL_BOOT_FILE:start_sasl
RABBITMQ_SERVER_ERL_ARGS:+K true +A30 +P 1048576 -kernel inet_default_connect_options [{nodelay,true}]
RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS:
RABBITMQ_LISTEN_ARG:
RABBITMQ_LOGS:/var/log/rabbitmq/rabbit@slavery01.log
RABBITMQ_SASL_LOGS:/var/log/rabbitmq/rabbit@slavery01-sasl.log
RABBITMQ_ENABLED_PLUGINS_FILE:/etc/rabbitmq/enabled_plugins
RABBITMQ_PLUGINS_DIR:/opt/rabbitmq-server-3.4.2/sbin/../plugins
RABBITMQ_PLUGINS_EXPAND_DIR:/var/lib/rabbitmq/mnesia/rabbit@slavery01-plugins-expand
RABBITMQ_MNESIA_DIR:/var/lib/rabbitmq/mnesia/rabbit@slavery01
RABBITMQ_SERVER_START_ARGS:
RABBITMQ_DIST_ARG:-kernel inet_dist_listen_min 25672 -kernel inet_dist_listen_max 25672
[root@slavery01 sbin]# rabbitmq-server

    说明:

  • rabbitmq-server本质是shell通过erl运行/opt/rabbitmq-server-3.4.2/ebin/rrabbit.beam这个erlang程序;
  • rabbitmq-server需要erlang语言环境支持;

 

3.rabbitmq3.4.2的默认配置文件在哪里?默认插件放在哪里?日志放在哪里?

默认配置项 默认配置项值
默认配置文件目录 /etc/rabbitmq/rabbitmq
默认日志文件目录 /var/log/rabbitmq
默认数据库目录 /var/lib/rabbitmq/mnesia
默认激活插件的目录 /etc/rabbitmq/enabled_plugins
默认插件所在目录 /opt/rabbitmq-3.4.2/plugins
默认rabbitmq相关环境配置文件 /etc/rabbitmq/rabbitmq-env.conf

 

4.为什么有了 rabbitmqctl 还需要 RabbitMQ Management plugin?

  •     原因一:rabbitmqctl需要有Erlang运行环境支持,必须和rabbitmq集群使用相同的.erlang.cookie;
  •     原因二:就算有了一种的条件,那么拿到这个权限就可以操作一切,比较危险;
  •     原因三:不是所有人都喜欢使用rabbitmqctl这种CLI命令,不如界面点击灵活可视化效果好;
  •     原因四:rabbitmqctl难于与其他编程语言集成。

 

5.RabbitMQ Management plugin 提供了哪些功能?

    Web interface        通过鼠标点击就可以查看各种信息,WEB UI提供了在 rabbitmq server 上的可视化操作;

    REST interface      通过HTTP URI实现对各种功能的访问.REST API 提供了一种与其他语言和工具集成的方式.返回的结构均以JSON格式提供,故需要支持JSON解析,需要支持HTTP basic authentication,需要手动构造完整的 HTTP request.支持对返回结果的排序,显示过滤,以及获取历史数据;

    CLI interface          rabbitmqadmin 脚本访问 (Python2.x).CLI interface 优于 REST-based API 的地方是不需要构建 request 中的全部内容,提供了优雅的格式化的输出。rabbitmqadmin 已经实现了对 REST API 的封装,提供了更加简洁的调用接口,能够对 rabbitmq server 进行管理和监控。rabbitmqadmin 提供了 Web UI 上所具有的全部功能,且更易于在脚本中使用。rabbitmqadmin 是一个特化的 HTTP 客户端,但如果你打算在自己的应用程序中调用 rabbitmqadmin ,那么还是建议你采用直接访问 HTTP API 的方式.另外rabbitmqadmin一般会在增加可执行权限后放到/opt/rabbitmq-3.4.2/sbin中.

 

 

 

 

  • rabbitmq3.4.2 shell脚本分析
            
    
    博客分类: 消息队列rabbitmq rabbitmq3.4.2配置 
  • 大小: 37.9 KB
  • rabbitmq3.4.2 shell脚本分析
            
    
    博客分类: 消息队列rabbitmq rabbitmq3.4.2配置 
  • 大小: 10.3 KB