Linux RabbitMQ单机与集群部署

1、下载rabbitMQ软件【这里以通用二进制包为准】

wget https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.8.6/rabbitmq-server-generic-unix-3.8.6.tar.xz

 2、RabbitMQ 对Erlang版本要求

到这里查看
https://www.rabbitmq.com/which-erlang.html

因为这里演示的版本是3.8.6,所以Erlang版本:21.3~23.X

3、下载Erlang

https://github.com/erlang/otp/archive/OTP-23.0.tar.gz

 4、Erlang安装

# 安装Erlang
~]# yum install mesa-libGL-devel mesa-libGLU-devel freeglut-devel gcc glibc-devel make ncurses-devel openssl-devel autoconf xmlto libtool automake -y
~]# tar xvf otp-OTP-23.0.tar.gz
~]# cd otp-OTP-23.0
otp-OTP-23.0]# ./otp_build autoconf
~]# ./otp_build autoconf && ./configure --prefix=/usr/local/erlang_23.0 && make && make install

# 配置Erlang环境变量
~]# vi /etc/profile
PATH=$PATH:/usr/local/erlang_23.0/bin/
~]# . /etc/profile

 5、安装RabbitMQ

# 解压rabbitMQ
~]# tar xvf rabbitmq-server-generic-unix-3.8.6.tar.xz -C /usr/local/

# 配置环境变量
~]# vi /etc/profile
PATH=$PATH:/usr/local/erlang_23.0/bin/:/usr/local/rabbitmq_server-3.8.6/sbin/
~]# . /etc/profile

# 命令启动rabbitMQ服务
~]# rabbitmq-server start    
Configuring logger redirection

  ##  ##      RabbitMQ 3.8.6
  ##  ##
  ##########  Copyright (c) 2007-2020 VMware, Inc. or its affiliates.
  ######  ##
  ##########  Licensed under the MPL 2.0. Website: https://rabbitmq.com

  Doc guides: https://rabbitmq.com/documentation.html
  Support:    https://rabbitmq.com/contact.html
  Tutorials:  https://rabbitmq.com/getstarted.html
  Monitoring: https://rabbitmq.com/monitoring.html

  Logs: /usr/local/rabbitmq_server-3.8.6/var/log/rabbitmq/rabbit@localhost.log
        /usr/local/rabbitmq_server-3.8.6/var/log/rabbitmq/rabbit@localhost_upgrade.log

  Config file(s): (none)

  Starting broker... completed with 0 plugins.
  
# 检查端口是否开启成功  
~]# netstat -tunlp | grep beam
tcp        0      0 0.0.0.0:25672           0.0.0.0:*               LISTEN      70943/beam.smp      
tcp6       0      0 :::5672                 :::*                    LISTEN      70943/beam.smp 

6、启用RabbitMQ Web管理的插件

 ~]# rabbitmq-plugins enable rabbitmq_management
Enabling plugins on node rabbit@localhost:
rabbitmq_management
The following plugins have been configured:
  rabbitmq_management
  rabbitmq_management_agent
  rabbitmq_web_dispatch
Applying plugin configuration to rabbit@localhost...
The following plugins have been enabled:
  rabbitmq_management
  rabbitmq_management_agent
  rabbitmq_web_dispatch

set 3 plugins.
Offline change; changes will take effect at broker restart.

 7、编写rabbitMQ启动脚本

~]# mkdir /usr/local/rabbitmq_server-3.8.6/var/run
~]# mkdir /usr/local/rabbitmq_server-3.8.6/script
~]# vi /usr/local/rabbitmq_server-3.8.6/script/rabbitmq
~]# chmod 755 /usr/local/rabbitmq_server-3.8.6/script/rabbitmq
#!/bin/sh
# chkconfig: 2345 64 36
#
# rabbitmq-server RabbitMQ broker
#
# 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

MQ_HOME=/usr/local/rabbitmq_server-3.8.6
PATH=$PATH:/usr/local/erlang_23.0/bin/:$MQ_HOME/sbin/

NAME=rabbitmq-server

DAEMON=$MQ_HOME/sbin/rabbitmq-server
CONTROL=$MQ_HOME/sbin/rabbitmqctl

DESC=rabbitmq-server
USER=rabbitmq
ROTATE_SUFFIX=
INIT_LOG_DIR=/usr/local/rabbitmq_server-3.8.6/var/log/
PID_FILE=/usr/local/rabbitmq_server-3.8.6/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
rabbitmq

 8、利用脚本启动服务

~]# /usr/local/rabbitmq_server-3.8.6/script/rabbitmq start
~]# /usr/local/rabbitmq_server-3.8.6/script/rabbitmq stop

 9、配置Web管理用户

# 创建用户并且给用户设置管理员权限
~]# rabbitmqctl add_user admin admin
~]# rabbitmqctl set_user_tags admin administrator
~]# rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"  

~]# mkdir /etc/rabbitmq
~]# vi /usr/local/rabbitmq_server-3.8.6/etc/rabbitmq/rabbitmq.config 
[
{rabbit, [{tcp_listeners, [{"192.168.2.129",5672},{"127.0.0.1",5672}]}, {loopback_users, ["admin"]}]}
].
~]# /usr/local/rabbitmq_server-3.8.6/script/rabbitmq stop
~]# /usr/local/rabbitmq_server-3.8.6/script/rabbitmq start

 

 

10、登陆Web界面

 11、上面配置演示的是单机版,下面进行集群配置

 12、主机列表配置如下:

192.168.2.129 mq1 
192.168.2.229 mq2 

[root@mq1 ~]# cat /etc/hosts
192.168.2.129 mq1 
192.168.2.229 mq2 

[root@mq2 ~]# cat /etc/hosts
192.168.2.129 mq1 
192.168.2.229 mq2

 13、集群配置

# 选择mq1主机同步~/.erlang.cookie


#复制.erlang.cookie先停止服务
mq2 ~]# /usr/local/rabbitmq_server-3.8.6/script/rabbitmq stop

#找一台MQ主机作做集群参照节点,把参照节点.erlang.cookie同步其他节点上,保持.erlang.cookie数据一致就好
mq1 ~]# scp ~/.erlang.cookie root@mq2:~/.erlang.cookie

mq1 ~]# cat .erlang.cookie
PXYCFDSEHXSKBOKPGHAV

mq2 ~]# cat .erlang.cookie
PXYCFDSEHXSKBOKPGHAV

#检查集群状态
mq1 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@mq1 ...
Basics
...

mq2 ~]# rabbitmqctl cluster_status             
Cluster status of node rabbit@mq2 ...
Basics
...

#停止应用服务
mq1 ~]# rabbitmqctl stop_app

#将mq2增加到mq1集群中
mq1 ~]# rabbitmqctl join_cluster --ram rabbit@mq2
Clustering node rabbit@mq1 with rabbit@mq2

#再开启应用
mq1 ~]# rabbitmqctl start_app
Starting node rabbit@mq1 ...

 

 

 14、访问Web界面查看

 15、集群的移除

#先停止要删除的节点的服务
mq2 ~]# /usr/local/rabbitmq_server-3.8.6/script/rabbitmq stop
Stopping rabbitmq-server: 

#mq1节点删除集群中mq2
mq1 ~]# rabbitmqctl -n rabbit@mq1  forget_cluster_node rabbit@mq2
Removing node rabbit@mq2 from the cluster


#mq2节点恢复单机模式
mq2 ~]# rm -f .erlang.cookie
~]# rabbitmqctl -n rabbit@mq2  forget_cluster_node rabbit@mq1 
Removing node rabbit@mq1 from the cluster
~]# rabbitmqctl stop_app
Stopping rabbit application on node rabbit@mq2 ...
~]# rabbitmqctl reset
Resetting node rabbit@mq2 ...
~]# rabbitmqctl start_app
Starting node rabbit@mq2 ...

#恢复单机后,用户需要重新创建

 16、负载均衡

略,可以选择Haproxy,Nginx,LVS

 

posted @ 2020-08-10 17:46  小粉优化大师  阅读(639)  评论(0编辑  收藏  举报