Centos7 生产环境安装 RabbitMQ (单机)

相关站点

系统环境

1
2
CentOS Linux release 7.6.1810 (Core)
Linux 3.10.0-957.5.1.el7.x86_64 #1 SMP Fri Feb 1 14:54:57 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

更改系统的最大打开文件描述符数

创建 Rabbitmq 用户和用户组

1
2
3
4
5
6
7
8
# 切换root用户
$ sudo -i

# 创建rabbitmq用户组
# groupadd rabbitmq

# 创建rabbitmq用户(不允许远程登录)
# useradd -g rabbitmq rabbitmq -s /bin/false

编译安装 Erlang

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 安装依赖
# yum install -y make autoconf gcc gcc-c++ glibc-devel kernel-devel m4 ncurses-devel openssl-devel unixODBC unixODBC-devel libtool libtool-ltdl-devel unzip

# 创建下载目录
# mkdir -p /home/rabbitmq/software

# 下载
# cd /home/rabbitmq/software
# wget http://erlang.org/download/otp_src_22.0.tar.gz

# 解压
# tar -xvf otp_src_22.0.tar.gz

# 删除下载文件
# rm -f otp_src_22.0.tar.gz

# 创建安装目录
# mkdir -p /usr/local/erlang-22.0

# 进入解压目录
# cd otp_src_22.0

# 配置
# ./otp_build autoconf
# ./configure --prefix=/usr/local/erlang-22.0 --without-javac

# 编译安装
# make && make install

# 创建软链接
# ln -sf /usr/local/erlang-22.0/bin/erl /usr/bin/erl

# 配置环境变量
# vim /etc/profile
export ERLANG_HOME=/usr/local/erlang-22.0
export PATH=$PATH:$ERLANG_HOME/bin

# 使环境变量生效
# source /etc/profile

二进制安装 RabbitMQ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 安装依赖
# yum install -y xmlto python-simplejson

# 下载
# wget https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.7.15/rabbitmq-server-generic-unix-3.7.15.tar.xz

# 解压
# xz -d rabbitmq-server-generic-unix-3.7.15.tar.xz
# tar -xvf rabbitmq-server-generic-unix-3.7.15.tar

# 删除下载文件
# rm -f rabbitmq-server-generic-unix-3.7.15.tar

# 移动解压目录
# mv rabbitmq_server-3.7.15 /usr/local/rabbitmq-3.7.15

# 文件授权
# chown -R rabbitmq:rabbitmq /usr/local/rabbitmq-3.7.15

# 配置环境变量
# vim /etc/profile
export RabbitMQ_HOME=/usr/local/rabbitmq-3.7.15
export PATH=$PATH:$RabbitMQ_HOME/sbin

# 使环境变量生效
# source /etc/profile

RabbitMQ 基础配置、插件安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 创建默认的日志目录与数据目录
# mkdir -p /usr/local/rabbitmq-3.7.15/var/log/rabbitmq
# mkdir -p /usr/local/rabbitmq-3.7.15/var/lib/rabbitmq/mnesia

# 创建默认的配置文件
# touch /usr/local/rabbitmq-3.7.15/etc/rabbitmq/rabbitmq.config
# echo "[]." > /usr/local/rabbitmq-3.7.15/etc/rabbitmq/rabbitmq.config

# 创建默认的环境变量文件
# touch /usr/local/rabbitmq-3.7.15/etc/rabbitmq/rabbitmq-env.conf
# echo "CONF_ENV_FILE=/usr/local/rabbitmq-3.7.15/etc/rabbitmq/rabbitmq-env.conf" >> /usr/local/rabbitmq-3.7.15/sbin/rabbitmq-defaults

# 启用Web控制台管理插件,浏览器可以通过url地址(http://127.0.0.1:15672)访问Web管理界面
# 默认的登录账号和密码都是guest,由于账号guest具有所有的操作权限,并且又是默认账号,出于系统安全的考虑,RabbitMQ默认限制了guest只能通过localhost登录使用
# rabbitmq-plugins enable rabbitmq_management

# 安装延迟队列插件
# cd /usr/local/rabbitmq/plugins
# wget https://dl.bintray.com/rabbitmq/community-plugins/3.7.x/rabbitmq_delayed_message_exchange/rabbitmq_delayed_message_exchange-20171201-3.7.x.zip
# unzip rabbitmq_delayed_message_exchange-20171201-3.7.x.zip
# rm -f rabbitmq_delayed_message_exchange-20171201-3.7.x.zip

# 启用延迟队列插件
# rabbitmq-plugins enable rabbitmq_delayed_message_exchange

# 查看所有已安装的插件
# rabbitmq-plugins list

# 文件授权
# chown -R rabbitmq:rabbitmq /usr/local/rabbitmq-3.7.15

RabbitMQ 创建虚拟主机与超级管理员用户,设置角色权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 上面提到过出于系统安全考虑,rabbitmq默认限制了guest只能通过localhost登录使用,因此需要手动创建管理员帐号,并更改guest用户默认的密码

# 前台启动RabbitMQ服务(默认会打印出日志文件和配置文件的路径)
# rabbitmq-server

# 或者后台启动RabbitMQ服务
# rabbitmq-server -detached

# 创建虚拟主机(相当于mysql的数据库概念)
# rabbitmqctl add_vhost /

# 更改guest用户默认的密码
# rabbitmqctl change_password guest yourPassword

# 创建超级管理员用户
# rabbitmqctl add_user admin yourPassword

# 赋予administrator角色给超级管理员用户
# rabbitmqctl set_user_tags admin administrator

# 赋予超级管理员用户权限
# rabbitmqctl set_permissions -p / admin '.*' '.*' '.*'

# 彻底关闭后台启动的RabbitMQ服务
# ./rabbitmqctl stop

配置防火墙

1
2
3
4
5
6
7
8
9
10
11
# 配置防火墙永久开放rabbitmq的端口,其中5672是客户端通信端口,15672是web管理界面的端口,25672是server间内部通信端口,4369是erlang发现端口
# firewall-cmd --zone=public --permanent --add-port=5672/tcp
# firewall-cmd --zone=public --permanent --add-port=15672/tcp
# firewall-cmd --zone=public --permanent --add-port=25672/tcp
# firewall-cmd --zone=public --permanent --add-port=4369/tcp

# 保存防火墙配置
# firewall-cmd --reload

# 查看防火墙已开放的端口
# firewall-cmd --list-ports

开机自启动 RabbitMQ

1
2
3
4
5
6
7
8
9
10
11
# 创建服务自启动脚本
# touch /etc/init.d/rabbitmq-server

# 更改服务自启动脚本,写入后面给出的脚本内容即可,如果是在其他系统环境里安装,只需修改脚本里的USER、MQ_HOME
# vim /etc/init.d/rabbitmq-server

# 服务自启动脚本授权
# chmod u+x /etc/init.d/rabbitmq-server

# 开机自启动
# chkconfig rabbitmq-server on

管理 RabbitMQ 服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 关闭服务
# systemctl stop rabbitmq-server

# 启动服务
# systemctl start rabbitmq-server

# 重启服务
# systemctl restart rabbitmq-server

# 查看服务状态
# systemctl status rabbitmq-server

# 查看运行状态
# rabbitmqctl status

# 循环日志文件
# rabbitmqctl rotate_logs[suffix]

# 重置(前提是rabbitmq已停止),从它属于的任何集群中移除,从管理数据库中移除所有数据,例如配置过的用户和虚拟宿主, 删除所有持久化的消息。
# rabbitmqctl reset

# 强制重置(前提是rabbitmq已停止),force_reset命令和reset的区别是无条件重置节点,不管当前管理数据库状态以及集群的配置。如果数据库或者集群配置发生错误才使用这个最后的手段。
# rabbitmqctl force_reset

RabbitMQ 服务自启动脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/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=/home/rabbitmq
PATH=/sbin:/usr/sbin:/bin:/usr/bin

USER=rabbitmq
NAME=rabbitmq-server
MQ_HOME=/usr/local/rabbitmq-3.7.15
DAEMON=${MQ_HOME}/sbin/${NAME}
CONTROL=${MQ_HOME}/sbin/rabbitmqctl

ROTATE_SUFFIX=
DESC=rabbitmq-server

MAX_OPEN_FILES=1048576
ulimit -n $MAX_OPEN_FILES

START_PROG="daemon"
PID_FILE=/var/run/rabbitmq/pid
LOCK_FILE=/var/lock/subsys/$NAME
INIT_LOG_DIR=${MQ_HOME}/var/log/rabbitmq

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

配置概述

1
2
3
4
5
6
安装目录:/usr/local/rabbitmq-3.7.15
日志目录:/usr/local/rabbitmq-3.7.15/var/log/rabbitmq
数据目录:/usr/local/rabbitmq-3.7.15/var/lib/rabbitmq/mnesia
配置文件:/usr/local/rabbitmq-3.7.15/etc/rabbitmq/rabbitmq.config
环境变量配置文件:/usr/local/rabbitmq-3.7.15/etc/rabbitmq/rabbitmq-env.conf
自启动脚本:/etc/init.d/rabbitmq-server