简介
RabbitMQ 是实现了高级消息队列协议(AMQP)的开源消息代理软件。RabbitMQ 服务器是用 Erlang 语言编写的,所以下面要安装 RabbitMQ 需要安装 Erlang。
以下环境都是在centos7 1804版本上测试,其它版本为测试
一.安装Erlang
1.1 准备
sudo yum update
sudo yum install epel-release
sudo yum install gcc gcc-c++ glibc-devel make ncurses-devel openssl-devel autoconf java-1.8.0-openjdk-devel git wget wxBase.x86_64
1.2 安装Erlang
wget http://packages.erlang-solutions.com/erlang-solutions-1.0-1.noarch.rpm
sudo rpm -Uvh erlang-solutions-1.0-1.noarch.rpm
sudo yum install erlang
- 验证是否安装成功,输入命令:
erl
二.安装RabbitMQ
2.1 下载安装
wget https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.7.8/rabbitmq-server-3.7.8-1.el7.noarch.rpm
sudo yum install rabbitmq-server-3.7.8-1.el7.noarch.rpm
2.2 配置
-
开机自启动:
sudo chkconfig rabbitmq-server on
-
开启远程访问:
cd /etc/rabbitmq cp /usr/share/doc/rabbitmq-server-3.4.1/rabbitmq.config.example /etc/rabbitmq/ mv rabbitmq.config.example rabbitmq.config vi /etc/rabbitmq/rabbitmq.config #将{loopback_user,[]}注释去掉,再删除后面逗号
-
开启web界面管理工具:
rabbitmq-plugins enable rabbitmq_management
2.3重启
service rabbitmq-server restart
在浏览器输入ip:15672
会看见webui界面(此操作需要先关闭防火墙)
三.集群
3.1集群环境配置
- 修改host文件
如两台机器ip为:
192.168.50.139
192.168.50.51
分别在两台机器hosts文件中加入:
192.168.50.139 rabbit1
192.168.50.51 rabbit2
- scp
.erlang.cookie
文件
集群中每个节点的cookie需要相同,官方文档说明如下:
RabbitMQ nodes and CLI tools (e.g. rabbitmqctl) use a cookie to determine whether they are allowed to communicate with each
other. For two nodes to be able to communicate they must have the same shared secret called the Erlang cookie. The cookie is
just a string of alphanumeric characters up to 255 characters in size. It is usually stored in a local file. The file must be
only accessible to the owner (e.g. have UNIX permissions of 600 or similar). Every cluster node must have the same cookie.
在rabbit1机器上:
cd /var/lib/rabbitmq
sudo chmod 777 .erlang.cookie
在rabbit2机器上:
cd /var/lib/rabbitmq
sudo rm -rf .erlang.cookie
scp qiyuesuo@192.168.50.139:/var/lib/rabbitmq/.erlang.cookie ./
sudo chmod 400 .erlang.cookie
sudo chown rabbitmq:rabbitmq .erlang.cookie
最后恢复rabbit1机器上的文件权限
sudo chmod 400 /var/lib/rabbitmq/.erlang.cookie
3.2 加入集群
在rabbit02机器上:
sudo rabbitmqctl stop_app
sudo rabbitmqctl join_cluster rabbit@rabbit01
sudo rabbitmqctl stop_app
两台机器上输入sudo rabbitmqctl cluster_status
出现以下内容则成功,访问管理页面时会出现两个节点
Cluster status of node rabbit@rabbit01 ...
[{nodes,[{disc,[rabbit@rabbit01,rabbit@rabbit02]}]},
{running_nodes,[rabbit@rabbit02,rabbit@rabbit01]},
{cluster_name,<<"rabbit@rabbit01">>},
{partitions,[]},
{alarms,[{rabbit@rabbit02,[]},{rabbit@rabbit01,[]}]}]
参考
安装rabbitmq:http://www.rabbitmq.com/install-rpm.html
集群配置:http://www.rabbitmq.com/clustering.html