Linux 服务器 Java环境部署系列(五):搭建RabbitMQ

引言

  本文主要介绍Linux服务器下,搭建RabbitMQ,及相关配置。

系列文档目录

  Linux 服务器 Java环境部署系列(一):安装JDK

  Linux 服务器 Java环境部署系列(二):安装nginx、mysql、tomcat、redis

  Linux 服务器 Java环境部署系列(三):安装git、maven

  Linux 服务器 Java环境部署系列(四):搭建jenkins环境、部署项目

  Linux 服务器 Java环境部署系列(五):搭建RabbitMQ

准备工作

wget http://erlang.org/download/otp_src_22.0-rc2.tar.gz
  • 下载rabbitmq-server安装包 
wget wget https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.8.2/rabbitmq-server-3.8.2-1.el7.noarch.rpm

安装Erlang

  • 安装依赖类库:
    • yum -y install gcc-c++
    • yum -y install ncurses-devel
    • yum -y install openssl-devel
    • yum -y install unixODBC-devel
  • 创建目录(安装指定目录):mkdir /usr/local/erlang
  • 打开目录 cd otp_src_22.2/
  • 安装至指定目录: ./configure --prefix=/usr/local/erlang --without-Javac
  • make && make install
  • 编译&安装完成以后配置Erlang环境变量:vim  /etc/profile
    • 追加环境变量到文件末尾:export PATH=$PATH:/usr/local/erlang/bin
    • source /etc/profile

安装RabbitMQ

  • 加载源:rpm -ivh --nodeps rabbitmq-server-3.8.2-1.el7.noarch.rpm
  • 如果linux的防火墙开启时,还需执行以下操作
    • firewall-cmd --zone=public --add-port=15672/tcp --permanent
    • firewall-cmd --zone=public --add-port=5672/tcp --permanent
    • firewall-cmd --reload
  • 启动:
    • rabbitmq-plugins enable rabbitmq_management
    • rabbitmq-server
  • 后台启动:rabbitmq-server -detached
  • 创建登录账号、密码:rabbitmqctl add_user [账号] [密码]
  • 授管理员权限:rabbitmqctl set_user_tags [账号] administrator
  • 授权用户到虚拟机(/):rabbitmqctl set_permissions -p / [账号] '.*' '.*' '.*'
  • 配置开机自启动:
    • 添加自启动脚本:
    • 在/etc/init.d目录下新建一个rabbitmq-server文件,内容如下: 
#!/bin/sh
#
# rabbitmq-server RabbitMQ broker
#
# chkconfig: - 80 05
# description: Enable AMQP service provided by RabbitMQ
#
 
### BEGIN INIT INFO
# Provides:          rabbitmq-server
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs $network
# Description:       RabbitMQ broker
# Short-Description: Enable AMQP service provided by RabbitMQ broker
### END INIT INFO
 
# Source function library.
. /etc/init.d/functions
export HOME=/root
PATH=/sbin:/usr/sbin:/bin:/usr/bin
NAME=rabbitmq-server
#DAEMON=/usr/sbin/${NAME}
#CONTROL=/usr/sbin/rabbitmqctl
DAEMON=/server/rabbitmq/sbin/${NAME}
CONTROL=/server/rabbitmq/sbin/rabbitmqctl
DESC=rabbitmq-server
USER=root
ROTATE_SUFFIX=
INIT_LOG_DIR=/var/log/rabbitmq
PID_FILE=/var/run/rabbitmq/pid
 
START_PROG="daemon"
LOCK_FILE=/var/lock/subsys/$NAME
 
test -x $DAEMON || exit 0
test -x $CONTROL || exit 0
 
RETVAL=0
set -e
 
[ -f /etc/default/${NAME} ] && . /etc/default/${NAME}
 
ensure_pid_dir () {
    PID_DIR=`dirname ${PID_FILE}`
    if [ ! -d ${PID_DIR} ] ; then
        mkdir -p ${PID_DIR}
        chown -R ${USER}:${USER} ${PID_DIR}
        chmod 755 ${PID_DIR}
    fi
}
 
remove_pid () {
    rm -f ${PID_FILE}
    rmdir `dirname ${PID_FILE}` || :
}
 
start_rabbitmq () {
    status_rabbitmq quiet
    if [ $RETVAL = 0 ] ; then
        echo RabbitMQ is currently running
    else
        RETVAL=0
        ensure_pid_dir
        set +e
        RABBITMQ_PID_FILE=$PID_FILE $START_PROG $DAEMON \
            > "${INIT_LOG_DIR}/startup_log" \
            2> "${INIT_LOG_DIR}/startup_err" \
            0<&- &
        $CONTROL wait $PID_FILE >/dev/null 2>&1
        RETVAL=$?
        set -e
        case "$RETVAL" in
            0)
                echo SUCCESS
                if [ -n "$LOCK_FILE" ] ; then
                    touch $LOCK_FILE
                fi
                ;;
            *)
                remove_pid
                echo FAILED - check ${INIT_LOG_DIR}/startup_\{log, _err\}
                RETVAL=1
                ;;
        esac
    fi
}
 
stop_rabbitmq () {
    status_rabbitmq quiet
    if [ $RETVAL = 0 ] ; then
        set +e
        $CONTROL stop ${PID_FILE} > ${INIT_LOG_DIR}/shutdown_log 2> ${INIT_LOG_DIR}/shutdown_err
        RETVAL=$?
        set -e
        if [ $RETVAL = 0 ] ; then
            remove_pid
            if [ -n "$LOCK_FILE" ] ; then
                rm -f $LOCK_FILE
            fi
        else
            echo FAILED - check ${INIT_LOG_DIR}/shutdown_log, _err
        fi
    else
        echo RabbitMQ is not running
        RETVAL=0
    fi
}
 
status_rabbitmq() {
    set +e
    if [ "$1" != "quiet" ] ; then
        $CONTROL status 2>&1
    else
        $CONTROL status > /dev/null 2>&1
    fi
    if [ $? != 0 ] ; then
        RETVAL=3
    fi
    set -e
}
 
rotate_logs_rabbitmq() {
    set +e
    $CONTROL rotate_logs ${ROTATE_SUFFIX}
    if [ $? != 0 ] ; then
        RETVAL=1
    fi
    set -e
}
 
restart_running_rabbitmq () {
    status_rabbitmq quiet
    if [ $RETVAL = 0 ] ; then
        restart_rabbitmq
    else
        echo RabbitMQ is not runnning
        RETVAL=0
    fi
}
 
restart_rabbitmq() {
    stop_rabbitmq
    start_rabbitmq
}
 
case "$1" in
    start)
        echo -n "Starting $DESC: "
        start_rabbitmq
        echo "$NAME."
        ;;
    stop)
        echo -n "Stopping $DESC: "
        stop_rabbitmq
        echo "$NAME."
        ;;
    status)
        status_rabbitmq
        ;;
    rotate-logs)
        echo -n "Rotating log files for $DESC: "
        rotate_logs_rabbitmq
        ;;
    force-reload|reload|restart)
        echo -n "Restarting $DESC: "
        restart_rabbitmq
        echo "$NAME."
        ;;
    try-restart)
        echo -n "Restarting $DESC: "
        restart_running_rabbitmq
        echo "$NAME."
        ;;
    *)
        echo "Usage: $0 {start|stop|status|rotate-logs|restart|condrestart|try-restart|reload|force-reload}" >&2
        RETVAL=1
        ;;
esac
 
exit $RETVAL
View Code
    •  根据实际情况修改上面内容中
# 增加了Erlang环境路径
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/erlang/bin

# 服务脚本
DAEMON=/server/rabbitmq/sbin/${NAME}

CONTROL=/server/rabbitmq/sbin/rabbitmqctl

DESC=rabbitmq-server

USER=root

# 日志目录
INIT_LOG_DIR=/data/soft/rabbitmq_server-3.7.9/var/log/rabbitmq     

# pid文件,查找命令命令:find / -name rabbit*.pid                                       
PID_FILE=/var/lib/rabbitmq/mnesia/rabbit@linux-dev.pid
View Code
      • 设置执行权限,命令:chmod a+x /etc/init.d/rabbitmq-server
    添加开机启动项:
    • 添加命令:chkconfig --add /etc/init.d/rabbitmq-server
    • 查看启动项,命令:chkconfig --list
    • 需要设置rabbitmq-server启动命令:chkconfig rabbitmq-server on
    • 关闭命令为:chkconfig rabbitmq-server off
    • 删除命令为:chkconfig --del rabbitmq-server
  • 执行命令reboot,重启服务器即可验证nginx开机启动

安装 RabbitMQ延迟队列插件

  在正常项目开发中,我们常常会遇到延迟消费的需求;所以我们在安装完RabbitMQ服务后,还需要安装对应延迟队列的插件,以方便满足业务需求。

  • 下载插件,注意要下载至你rabbitmq服务器的plugins目录下,例如:/usr/lib/rabbitmq/plugins
wget https://github.com/rabbitmq/rabbitmq-delayed-message-exchange/releases/download/v3.8.0/rabbitmq_delayed_message_exchange-3.8.0.ez
  • 解压后得到文件:rabbitmq_delayed_message_exchange-20171215-3.8.0.ez
  • 拷贝文件到指定目录:
cp rabbitmq_delayed_message_exchange-3.8.0.ez /usr/lib/rabbitmq/lib/rabbitmq_server-3.8.2/plugins/
  • 启用插件:rabbitmq-plugins enable rabbitmq_delayed_message_exchang

posted @ 2020-01-14 15:46  JaminHuang  阅读(592)  评论(0编辑  收藏  举报