RabbitMq集群搭建

## 资源
| 服务器IP | 功能 |
| —– | —— |
| 192.168.200.98 | node1 |
| 192.168.200.106 | node2 |
> hosts 指定主机名(根据实际情况)

192.168.200.98 zybtest-200-98
192.168.200.106 zybtest-200-106

## 安装erlang
> RabbitMq 依赖erlang (2台主机)
bash
curl -s https://packagecloud.io/install/repositories/rabbitmq/erlang/script.rpm.sh | sudo bash
sudo yum install erlang-21.1.1-1.el7.centos.x86_64

## 安装 RabbitMq (2台主机)
> 下载地址: http://www.rabbitmq.com/install-rpm.html#downloads
bash
rpm -ivh rabbitmq-server-3.7.8-1.el7.noarch.rpm

## 启动配置

systemctl start rabbitmq-server.service
systemctl status rabbitmq-server.service
systemctl enable rabbitmq-server.service
rabbitmqctl add_user admin sendifno.2018
rabbitmqctl set_user_tags admin administrator
rabbitmqctl set_permissions -p / admin “.*” “.*” “.*”

rabbitmq-plugins enable rabbitmq_management #启动web管理界面

## 集群配置
> 同步cookie文件,保持2台主机一致

> rabbitmq是依据erlang的分布式特性(RabbitMQ底层是通过Erlang架构来实现的,所以rabbitmqctl会启动Erlang节点,并基于Erlang节点来使用Erlang系统连接RabbitMQ节点,在连接过程中需要正确的Erlang Cookie和节点名称,Erlang节点通过交换Erlang Cookie以获得认证)来实现的,所以部署rabbitmq分布式集群时要先安装erlang,并把其中一个服务的cookie复制到另外的节点。


scp /var/lib/rabbitmq/.erlang.cookie 192.168.200.106:/var/lib/rabbitmq/
#node1配置
rabbitmqctl stop
rabbitmq-server -detached


> 加入集群(node2)

rabbitmqctl stop
rabbitmq-server -detached
rabbitmqctl stop_app
rabbitmqctl join_cluster rabbit@zybtest-200-98
rabbitmqctl start_app
rabbitmqctl cluster_status
rabbitmqctl list_users

## 其他
`
从做磁盘节点加入
rabbitmqctl join_cluster rabbit@rabbit43
从做内存节点加入
rabbitmqctl join_cluster -ram rabbit@rabbit43
配置镜像队列(设置镜像队列策略)
rabbitmqctl set_policy ha-all “^” ‘{“ha-mode”:”all”}’

如果需要移除集群节点,执行下面命令:

rabbitmqctl forget_cluster_node rabbit@node2(具体节点)

5、修改集群名称(任意一个节点操作,默认为master node名称)

rabbitmqctl set_cluster_name rabbitmq_cluster1

6、查看集群状态(任意一个节点操作)

rabbitmqctl cluster_status

7、设置镜像队列策略(任意一个节点操作)

rabbitmqctl set_policy ha-all “^” ‘{“ha-mode”:”all”}’

将所有队列设置为镜像队列,即队列会被复制到各个节点,各个节点状态一致。
此时看rabbitmq管控台,Nodes会有三个节点:

Leave a Reply

Your email address will not be published. Required fields are marked *