RabbitMQ之Linux下环境搭建
Posted zengnansheng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RabbitMQ之Linux下环境搭建相关的知识,希望对你有一定的参考价值。
软件环境:
CentOS6.5
otp_src_18.0.tar.gz
rabbitmq-server-generic-unix-3.6.0.tar.xz
安装编译工具
yum -y install make gcc gcc-c++ kernel-devel m4 ncurses-devel openssl-devel
安装Erlang
下载并上传otp_src_18.0.tar.gz到/usr/local目录
解压
cd /usr/local
tar zxvf otp_src_18.0.tar.gz
cd otp_src_18.0
配置 ‘--prefix‘指定的安装目录参数
./configure --prefix=/usr/local/erlang --with-ssl -enable-threads -enable-smmp-support -enable-kernel-poll --enable-hipe --without-javac
上述参数说明:
--prefix 指定安装目录
--enable-smp-support启用对称多处理支持(Symmetric Multi-Processing对称多处理结构的简称)
--enable-threads启用异步线程支持
--enable-sctp启用流控制协议支持(Stream Control Transmission Protocol,流控制传输协议)
--enable-kernel-poll启用Linux内核poll
--enable-hipe启用高性能Erlang
--with-ssl使用SSL包
安装
make && make install
追加erlang环境变量
vim /etc/profile
#在文件末尾添加下面代码
ERLANG_HOME等于上一步‘--prefix‘指定的目录
export ERLANG_HOME=/usr/local/erlang
export PATH=$ERLANG_HOME/bin:$PATH
#使环境变量生效
source /etc/profile
#输入erl命令显示出版本信息则表示安装成功
erl
安装RabbitMQ
下载并上传rabbitmq-server-generic-unix-3.6.0.tar.xz到/usr/local目录
解压rabbitmq,官方给的包是xz压缩包,所以需要使用xz命令
xz -d rabbitmq-server-generic-unix-3.6.0.tar.xz
xz解压后得到.tar包,再用tar命令解压得到rabbitmq_server-3.6.0文件夹
tar -xvf rabbitmq-server-generic-unix-3.6.0.tar
重命名
mv rabbitmq_server-3.6.0 rabbitmq
开启管理页面插件
cd /usr/local/rabbitmq/sbin/
./rabbitmq-plugins enable rabbitmq_management
./rabbitmq-server start 启动服务 该命令ctrl+c后会关闭服务
./rabbitmq-server -detached 在后台启动服务
./rabbitmq-server stop 停止服务
./rabbitmq-server status查看服务状态
./rabbitmq-server etc 查看哪些命令可以使用
执行
./rabbitmq-server -detached
增加一个用户
cd /usr/local/rabbitmq/sbin/
./rabbitmqctl add_user zns 123456
设置用户角色
./rabbitmqctl set_user_tags zns administrator
设置权限
./rabbitmqctl set_permissions -p "/" zns ".*" ".*" ".*"
浏览器输入地址:http://服务器IP地址:15672/ 即可以登录管理后台界面
安装完成之后如果机器有iptables,需要开放端口
vim /etc/sysconfig/iptables
-A INPUT -p tcp -m state --state NEW -m tcp --dport 15672 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 25672 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 5672 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 4369 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 5671 -j ACCEPT
设置开机自动启动
新建一个rabbitmq启动脚本
vim /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=/usr/local/rabbitmq/sbin/${NAME} CONTROL=/usr/local/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
根据实际情况修改下面属性值
DAEMON
CONTROL
DESC
USER
脚本运行需要保证 上面 INIT_LOG_DIR 定义的日志目录是存在的,没有的话可以新建对应目录
chmod +x /etc/init.d/rabbitmq-server
chkconfig --add rabbitmq-server
chkconfig rabbitmq-server on
重启服务器,会自动启动rabbitmq服务,再次访问http://ip:15672 可以成功打开管理界面
以上是关于RabbitMQ之Linux下环境搭建的主要内容,如果未能解决你的问题,请参考以下文章
Linux 服务器 Java环境部署系列:搭建RabbitMQ