简介
RabbitMQ是一款实现了高级消息队列协议(AMQP)的开源消息代理软件(亦称面向消息的中间件)。RabbitMQ服务器是用Erlang语言编写,而群集和故障转移是构建在开放电信平台框架上的。所有主要的语言均有与代理接口通讯的客户端库。由于文档写了很多,下面就精简一下,把单机群集拿出来讲下。
安装依赖
yum install -y epel-release yum install -y erlang socat
源码安装方式:
yum install -y gcc gcc-c++ libtool ncurses-devel wget wget http://zlib.net/zlib-1.2.11.tar.xz tar Jxf zlib-1.2.11.tar.xz -C /usr/local/src/ cd /usr/local/src/zlib-1.2.11/ CFLAGS="-fPIC -O3" ./configure --shared –64 make -j `cat /proc/cpuinfo | grep processor | wc -l`;make install chcon -R -u system_u /usr/local/lib/libz.so* echo '/usr/local/lib' > /etc/ld.so.conf.d/complie.conf cd - wget https://www.openssl.org/source/openssl-1.0.2l.tar.gz tar zxf openssl-1.0.2l.tar.gz -C /usr/local/src/ cd /usr/local/src/openssl-1.0.2l/ CFLAGS="-fPIC -O3" ./config --prefix=/usr/local/openssl shared threads zlib make -j `cat /proc/cpuinfo | grep processor | wc -l`;make install ln -s /usr/local/openssl/lib/lib*.so* /usr/local/lib/ ln -s /usr/local/openssl/lib/pkgconfig/* /usr/local/lib/pkgconfig/ ln -s /usr/local/openssl/include/* /usr/local/include/ echo 'export PATH=/usr/local/openssl/bin:$PATH' > /etc/profile.d/openssl.sh source /etc/profile.d/openssl.sh openssl version cd – ldconfig wget http://erlang.org/download/otp_src_20.0.tar.gz tar zxf otp_src_20.0.tar.gz -C /usr/local/src/ cd /usr/local/src/otp_src_20.0/ ./configure --prefix=/usr/local/erlang --with-ssl=/usr/local/openssl --enable-threads make -j `cat /proc/cpuinfo | grep processor | wc -l`;make install echo 'export PATH=$PATH:/usr/local/erlang/bin' > /etc/profile.d/erl.sh source /etc/profile.d/erl.sh cd - wget http://www.dest-unreach.org/socat/download/socat-1.7.3.2.tar.gz tar zxf socat-1.7.3.2.tar.gz -C /usr/local/src/ cd /usr/local/src/socat-1.7.3.2/ CFLAGS="-fPIC -O3" ./configure make -j `cat /proc/cpuinfo | grep processor | wc -l`;make install ldconfig
安装RabbitMQ
RPM方式:
wget https://github.com/rabbitmq/rabbitmq-server/releases/download/rabbitmq_v3_6_11/rabbitmq-server-3.6.11-1.el7.noarch.rpm rpm -ivh rabbitmq-server-3.6.11-1.el7.noarch.rpm
公用二进制包方式:
wget https://github.com/rabbitmq/rabbitmq-server/releases/download/rabbitmq_v3_6_11/rabbitmq-server-generic-unix-3.6.11.tar.xz tar Jxf rabbitmq-server-generic-unix-3.6.11.tar.xz -C /usr/local/ mv /usr/local/rabbitmq_server-3.6.11/ /usr/local/rabbitmq_server echo 'export PATH=$PATH:/usr/local/rabbitmq_server/sbin/' > /etc/profile.d/rabbitmq.sh source /etc/profile.d/rabbitmq.sh mkdir -p /data/rabbitmq/data groupadd -g 33 rabbitmq useradd -u 33 -g rabbitmq -s /sbin/nologin -d /data/rabbitmq/ -c "RabbitMQ messaging server" -M rabbitmq chown -R rabbitmq:rabbitmq /data/rabbitmq/ chown -R rabbitmq /usr/local/rabbitmq_server/etc/rabbitmq/
建立服务脚本:
vim /usr/lib/systemd/system/rabbitmq-server.service
[Unit] Description=RabbitMQ broker After=syslog.target network.target [Service] Type=notify User=rabbitmq Group=rabbitmq WorkingDirectory=/data/rabbitmq/ ExecStart=/usr/local/rabbitmq_server/sbin/rabbitmq-server ExecStop=/usr/local/rabbitmq_server/sbin/rabbitmqctl stop ExecStop=/bin/sh -c "while ps -p $MAINPID >/dev/null 2>&1; do sleep 1; done" NotifyAccess=all TimeoutStartSec=3600 [Install] WantedBy=multi-user.target
运行
公用二进制包最好先创建配置文件环境变量;默认其配置文件目录及其它相关目录都是以二进制包为根目录而自动生成的,故若安默认启动需保证二进制包目录需有rabbitmq用户权限。这里设置环境变量配置文件,使其数据及日志目录存放于/data/rabbitmq目录下。
vim /usr/local/rabbitmq/etc/rabbitmq/rabbitmq-env.conf
# RabbitMQ env Config File. RABBITMQ_MNESIA_BASE=/data/rabbitmq/data RABBITMQ_LOG_BASE=/data/rabbitmq RABBITMQ_PLUGINS_DIR=/usr/local/rabbitmq_server/plugins
Tips: PLUGINS目录为二进制包中plugins目录的位置。
由于erl命令没在系统默认PATH中,故需修改自带的启动脚本,在前面添加如下项即可:
vim /usr/local/rabbitmq/sbin/rabbitmq-server
source /etc/profile
vim /usr/local/rabbitmq/sbin/rabbitmqctl
source /etc/profile
启动:
systemctl start rabbitmq-server systemctl status rabbitmq-server
查看:
netstat -nlput | grep `ps -ef | awk '/rabbitmq_server/ && $0 !~ "awk" {print $2}'`
开启管理网页插件:
echo '[{rabbit, [{loopback_users, []}]}].' > /usr/local/rabbitmq_server/etc/rabbitmq/rabbitmq.config
启用管理页面插件、添加用户:
rabbitmq-plugins enable rabbitmq_management rabbitmqctl add_user admin admin rabbitmqctl set_user_tags admin administrator
二进制包需以rabbitmq用户身份运行:
su -s /bin/bash - rabbitmq -c "rabbitmq-plugins enable rabbitmq_management" su -s /bin/bash - rabbitmq -c "rabbitmqctl add_user admin admin" su -s /bin/bash - rabbitmq -c "rabbitmqctl set_user_tags admin administrator"
插件启用完毕,即可以看到其会启动新的端口15672:
防火墙开放端口后即可在其它机器上查看:
firewall-cmd --permanent --add-port=5672/tcp firewall-cmd --permanent --add-port=15672/tcp firewall-cmd --permanent --add-port=25672/tcp firewall-cmd --reload
单机群集
某些情况下,在单机上运行一组RabbitMQ节点可能非常有用。这通常用于在台式机或笔记本上进行群集测试而无需为群集启动多个虚拟机。
为了在单机上运行多个RabbitMQ节点,有必要确保节点具有不同的节点名称,数据存储位置,日志文件位置,并绑定到不同的端口,包括插件使用的端口。请参阅配置指南中的RABBITMQ_NODENAME, RABBITMQ_NODE_PORT和RABBITMQ_DIST_PORT,以及文件和目录位置指南中的RABBITMQ_MNESIA_DIR,RABBITMQ_CONFIG_FILE和RABBITMQ_LOG_BASE。
可以通过重复调用rabbitmq-server(Windows上的rabbitmq-server,bat)手动启动同一主机上的多个节点。例如:
vim /etc/host
# RabbitMQ Single Cluster 172.16.220.109 rabbit 172.16.220.109 hare
cd /var/lib/rabbitmq systemctl stop rabbitmq-server rm -rf mnesia/ RABBITMQ_NODE_PORT=5672 RABBITMQ_NODENAME=rabbit rabbitmq-server -detached RABBITMQ_NODE_PORT=5673 RABBITMQ_NODENAME=hare rabbitmq-server -detached
注意,此时第二个节点其实并未成功启动,因为我们开启了管理页面(占用15672端口):
故当RabbitMQ打开了除AMQP之外的其它端口,你需要配置那些端口以不至于冲突。可以通过如下命令完成启动:
ps -ef | awk '/rabbitmq_server/ && $0 !~ "awk" {print $2}' | xargs kill RABBITMQ_NODE_PORT=5672 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15672}]" RABBITMQ_NODENAME=rabbit rabbitmq-server -detached RABBITMQ_NODE_PORT=5673 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15673}]" RABBITMQ_NODENAME=hare rabbitmq-server -detached
这时再查看,其已正常启动:
将hare节点加入到群集rabbit中:
rabbitmqctl -n hare stop_app rabbitmqctl -n hare join_cluster rabbit@rabbit1 rabbitmqctl -n hare start_app
查看群集状态:
rabbitmqctl -n rabbit cluster_status rabbitmqctl -n hare cluster_status
登陆管理界面亦可查看到集群状态: