摘要: 今天教大家利用镜像快熟部署使用RabbitMQ。RabbitMQ是一个开源的AMQP实现,服务器端用Erlang语言编写,支持多种客户端,用于在分布式系统中存储转发消息,在易用性、扩展性、高可用性等方面表现不俗。
RabbitMQ是一个开源的AMQP实现,服务器端用Erlang语言编写,支持多种客户端,如:Python、Ruby、.NET、Java、JMS、C、PHP、ActionScript、XMPP、STOMP等,支持AJAX。用于在分布式系统中存储转发消息,在易用性、扩展性、高可用性等方面表现不俗。
部署方式
在阿里云服务器下部署 RabbitMQ提供两种部署方式:
- RabbitMQ镜像部署
- 手动部署(源码编译安装/YUM安装)
一般推荐镜像部署适合新手使用更加快捷方便,安装包部署以及手动部署适合对Linux命令有基本了解的用户,可以满足用户个性化部署的要求。本教程主要介绍镜像和手工部署的方式。
镜像部署
1. 单击 JAVA环境(CentOS7.2 Nginx Tomcat8 JDK)进入镜像详情页。
2. 单击 **立即购买**,按提示步骤购买 ECS 实例。
3. 登录 ECS 管理控制台:https://ecs.console.aliyun.com/#/home
4. 在左边导航栏里,单击 **实例**,进入 ECS 实例列表页。
5. 选择所购 ECS 实例所在的地域,并找到所购 ECS 实例,在 **IP 地址** 列获取该实例的公网 IP 地址。
6. 在浏览器地址栏中输入公网 IP 地址,下载操作文档。
8. 使用winscp工具将Java代码放入/data/wwwroot/default中。
9. 默认tomcat是以一般www用户运行,将网站代码权限改为www,执行下面命令:
chown -R www.www /data/wwwroot
10. 重启tomcat
11. 在浏览器地址栏中输入公网 IP 地址,验证。
手工部署
系统平台:CentOS 7.3
rabbitmq版本:rabbitmq-server -3.6.9
erlang版本:erlang19.3
JDK版本:JDK1.8.0_121
- 前提准备
创建一般用户rabbitmq,运行rabbitmq
useradd rabbitmq
设置Linux主机名
centos7修改/etc/hostname,centos6修改/etc/sysconfig/network,下面以centos7为例
echo rabbit1 > /etc/hostname
hostname rabbit1
exit #退出重新登录
安装依赖包yum -y install make gcc gcc-c++ m4 ncurses-devel openssl-devel unixODBC-devel
- 源代码下载
wget http://erlang.org/download/otp_src_19.3.tar.gz
wget https://www.rabbitmq.com/releases/rabbitmq-server/v3.6.9/rabbitmq-server-generic-unix-3.6.9.tar.xz
- 安装Erlang
tar xzf otp_src_19.3.tar.gz #解压
cd otp_src_19.3
./configure --prefix=/usr/local/erlang --enable-shared-zlib --with-ssl --enable-threads --enable-smp-support --enable-kernel-poll --enable-hipe --without-javac
make && make install
- 解压RabbitMQ
1. 解压rabbitmq-server-generic-unix-3.6.9.tar.xz
tar xvJf rabbitmq-server-generic-unix-3.6.9.tar.xz
mv rabbitmq_server-3.6.9 /usr/local/rabbitmq
2. rabbitmq环境变量配置sed -i 's@^ERL_DIR=.*@ERL_DIR=/usr/local/erlang/bin/@' /usr/local/rabbitmq/sbin/rabbitmq-defaults
sed -i 's@^LOG_BASE=.*@LOG_BASE=/usr/local/rabbitmq/var/log/rabbitmq@' /usr/local/rabbitmq/sbin/rabbitmq-defaults
mkdir -p /usr/local/rabbitmq/var/{lib,log}/rabbitmq
3. 一般用户(rabbitmq)运行RabbitMQwget http://pkgs.fedoraproject.org/cgit/rpms/rabbitmq-server.git/plain/rabbitmq-script-wrapper
sed -i 's@cd /var/lib/rabbitmq@cd /usr/local/rabbitmq/var/lib/rabbitmq@g' rabbitmq-script-wrapper #更改rabbitmq数据存储目录
sed -i 's@/usr/lib/rabbitmq/bin/@/usr/local/rabbitmq/sbin/@g' rabbitmq-script-wrapper
chmod +x rabbitmq-script-wrapper
cp rabbitmq-script-wrapper /usr/sbin/rabbitmqctl
cp rabbitmq-script-wrapper /usr/sbin/rabbitmq-server
cp rabbitmq-script-wrapper /usr/sbin/rabbitmq-plugins
chown -R rabbitmq.rabbitmq /usr/local/rabbitmq/var
4. rabbitmq日志割接cat >> /etc/logrotate.d/rabbitmq-server << EOF
/usr/local/rabbitmq/var/log/rabbitmq/*.log {
weekly
missingok
rotate 20
compress
delaycompress
notifempty
sharedscripts
postrotate
/sbin/service rabbitmq-server rotate-logs > /dev/null
endscript
}
EOF
5. rabbitmq启动脚本
vi /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
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/erlang/bin
NAME=rabbitmq-server
DAEMON=/usr/sbin/${NAME}
CONTROL=/usr/sbin/rabbitmqctl
DESC=rabbitmq-server
USER=rabbitmq
ROTATE_SUFFIX=
INIT_LOG_DIR=/usr/local/rabbitmq/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}
[ -f /etc/sysconfig/${NAME} ] && . /etc/sysconfig/${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
# RABBIT_NOFILES_LIMIT from /etc/sysconfig/rabbitmq-server is not handled
# automatically
if [ "$RABBITMQ_NOFILES_LIMIT" ]; then
ulimit -n $RABBITMQ_NOFILES_LIMIT
fi
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
保存后,添加执行权限,并设置自启动chmod +x /etc/init.d/rabbitmq-server
chkconfig --add rabbitmq-server
chkconfig rabbitmq-server on
6. 修改rabbitmq.config特别注意默认用户名密码,请自行修改default_user,default_pass,loopback_users
cat > /usr/local/rabbitmq/etc/rabbitmq/rabbitmq.config << EOF
[
{rabbit, [
{tcp_listeners,[{"0.0.0.0",5672}]},
{tcp_listen_options, [binary, {packet,raw},
{reuseaddr,true},
{backlog,128},
{nodelay,true},
{exit_on_close,false},
{keepalive,true}]},
{default_vhost, <<"/">>},
{default_user, <<"guest">>},
{default_pass, <<"guest">>},
{loopback_users, ["guest"]},
{default_permissions, [<<".*">>, <<".*">>, <<".*">>]}
]}
].
EOF
7. 开启rabbitmq managercat > /usr/local/rabbitmq/etc/rabbitmq/enabled_plugins << EOF
[rabbitmq_management].
EOF
8. 启动rabbitmqservice rabbitmq-server start
9. 进入管理页面浏览器访问http://公网IP:15672