RabbitMQ “普通模式” 集群
1、集群主机
我这里因为和其他消息队列服务公用主机,这里共计4台虚拟机,分别是:rabbitmq-1、rabbitmq-2、rabbitmq-3、rabbitmq-4;
序号 | IP | 角色 | 架构模式 |
---|---|---|---|
1 | 192.168.3.73 | broker | rabbitmq-1 |
2 | 192.168.3.74 | broker | rabbitmq-2 |
3 | 192.168.3.75 | broker | rabbitmq-3 |
4 | 192.168.3.93 | broker | rabbitmq-4 |
首先给每个主机都设置 hosts
echo '192.168.3.73 rabbitmq-1' >>/etc/hosts;
echo '192.168.3.74 rabbitmq-2' >>/etc/hosts;
echo '192.168.3.75 rabbitmq-3' >>/etc/hosts;
echo '192.168.3.93 rabbitmq-4' >>/etc/hosts;
首先给每个主机单独设置 hostname
# 在 192.168.3.73 上
echo 'rabbitmq-1' >>/etc/hostname;
# 在 192.168.3.74 上
echo 'rabbitmq-2' >>/etc/hostname;
# 在 192.168.3.75 上
echo 'rabbitmq-3' >>/etc/hostname;
# 在 192.168.3.93 上
echo 'rabbitmq-4' >>/etc/hostname;
查看hosts是否设置成功
[root@localhost src]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.3.73 rabbitmq-1
192.168.3.74 rabbitmq-2
192.168.3.75 rabbitmq-3
192.168.3.93 rabbitmq-4
2、安装 RabbitMQ(三台机器同时操作)
注意:参考前面 单节点安装说明 ,在每台主机上安装RabbitMQ服务;三台主机上安装的 RabbitMQ 都保证都可以正常启动, 才可以进行以下操作
三台主机安装完成后,访问地址:http://每台主机IP:15672
- http://192.168.3.73:15672
- http://192.168.3.74:15672
- http://192.168.3.75:15672
- http://192.168.3.93:15672
admin/admin
3、配置集群
3.1、防火墙开放 epmd 端口 4369,启动 RabbitMQ 服务。
[root@A ~]# systemctl stop firewalld
[root@A ~]# systemctl start rabbitmq-server
或者:
[root@A ~]# firewall-cmd --zone=public --add-port=4369/tcp --permanent
[root@A ~]# firewall-cmd --reload
[root@A ~]# systemctl start rabbitmq-server
3.2、设置 Erlang Cookie
设置不同节点间同一认证的 Erlang Cookie,这里将 rabbitmq-1 的 Cookie 传给另外两台。
[root@rabbitmq-1 ~]# scp /var/lib/rabbitmq/.erlang.cookie root@rabbitmq-2:/var/lib/rabbitmq/
[root@rabbitmq-1 ~]# scp /var/lib/rabbitmq/.erlang.cookie root@rabbitmq-3:/var/lib/rabbitmq/
[root@rabbitmq-1 ~]# scp /var/lib/rabbitmq/.erlang.cookie root@rabbitmq-4:/var/lib/rabbitmq/
注意:
Cookie 在所有节点上必须完全一样,同步时一定要注意。 erlang 是通过主机名来连接服务,必须保证各个主机名之间可以 ping 通。可以通过编辑/etc/hosts
来手工添加主机名和 IP 对应关系。如果主机名 ping 不通,rabbitmq 服务启动会失败
3.3、启动节点(三台机器同时操作)
# 重启节点(三台机器同时操作)
#[root@rabbitmq-1 ~]# rabbitmq-server -detached
[root@rabbitmq-1 ~]# systemctl restart rabbitmq-server
# 查看单节点的集群状态。
[root@rabbitmq-1 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@rabbitmq-1 ...
---
[root@rabbitmq-2 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@rabbitmq-2 ...
---
[root@rabbitmq-3 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@rabbitmq-3 ...
---
[root@rabbitmq-4 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@rabbitmq-4 ...
---
3.4、加入集群
依次将 rabbitmq-2、rabbitmq-3、rabbitmq-4 加入到 rabbitmq-1 中
# 重启rabbitmq服务
[root@rabbitmq-2 ~]# systemctl restart rabbitmq-server
# 停止rabbitmq服务
[root@rabbitmq-2 ~]# rabbitmqctl stop_app
Stopping rabbit application on node rabbit@rabbitmq-2 ...
[root@rabbitmq-2 ~]# rabbitmqctl join_cluster rabbit@rabbitmq-1
Clustering node rabbit@rabbitmq-2 with rabbit@rabbitmq-1
# 开启rabbitmq服务
[root@rabbitmq-2 ~]# rabbitmqctl start_app
Starting node rabbit@rabbitmq-2 ...
[root@rabbitmq-3 ~]# systemctl restart rabbitmq-server
[root@rabbitmq-3 ~]# rabbitmqctl stop_app
Stopping rabbit application on node rabbit@rabbitmq-3 ...
[root@rabbitmq-3 ~]# rabbitmqctl join_cluster rabbit@rabbitmq-1
Clustering node rabbit@rabbitmq-3 with rabbit@rabbitmq-1
[root@rabbitmq-3 ~]# rabbitmqctl start_app
Starting node rabbit@rabbitmq-3 ...
[root@rabbitmq-4 ~]# systemctl restart rabbitmq-server
[root@rabbitmq-4 ~]# rabbitmqctl stop_app
Stopping rabbit application on node rabbit@rabbitmq-4 ...
[root@rabbitmq-4 ~]# rabbitmqctl join_cluster rabbit@rabbitmq-1
Clustering node rabbit@rabbitmq-4 with rabbit@rabbitmq-1
[root@rabbitmq-4 ~]# rabbitmqctl start_app
Starting node rabbit@rabbitmq-4 ...
3.5、查看集群
# 检查集群
[root@rabbitmq-1 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@rabbitmq-1 ...
Basics
Cluster name: rabbit@rabbitmq-1
Disk Nodes
rabbit@rabbitmq-1
rabbit@rabbitmq-2
rabbit@rabbitmq-3
rabbit@rabbitmq-4
Running Nodes
rabbit@rabbitmq-1
rabbit@rabbitmq-2
rabbit@rabbitmq-3
rabbit@rabbitmq-4
Versions
rabbit@rabbitmq-1: RabbitMQ 3.10.0 on Erlang 23.3.4.11
rabbit@rabbitmq-2: RabbitMQ 3.10.0 on Erlang 23.3.4.11
rabbit@rabbitmq-3: RabbitMQ 3.10.0 on Erlang 23.3.4.11
rabbit@rabbitmq-4: RabbitMQ 3.10.0 on Erlang 23.3.4.11
Maintenance status
Node: rabbit@rabbitmq-1, status: not under maintenance
Node: rabbit@rabbitmq-2, status: not under maintenance
Node: rabbit@rabbitmq-3, status: not under maintenance
Node: rabbit@rabbitmq-4, status: not under maintenance
Alarms
(none)
Network Partitions
(none)
Listeners
Node: rabbit@rabbitmq-1, interface: [::], port: 15672, protocol: http, purpose: HTTP API
Node: rabbit@rabbitmq-1, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
Node: rabbit@rabbitmq-1, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0
Node: rabbit@rabbitmq-2, interface: [::], port: 15672, protocol: http, purpose: HTTP API
Node: rabbit@rabbitmq-2, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
Node: rabbit@rabbitmq-2, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0
Node: rabbit@rabbitmq-3, interface: [::], port: 15672, protocol: http, purpose: HTTP API
Node: rabbit@rabbitmq-3, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
Node: rabbit@rabbitmq-3, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0
Node: rabbit@rabbitmq-4, interface: [::], port: 15672, protocol: http, purpose: HTTP API
Node: rabbit@rabbitmq-4, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
Node: rabbit@rabbitmq-4, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0
Feature flags
Flag: classic_mirrored_queue_version, state: enabled
Flag: drop_unroutable_metric, state: enabled
Flag: empty_basic_get_metric, state: enabled
Flag: implicit_default_bindings, state: enabled
Flag: maintenance_mode_status, state: enabled
Flag: quorum_queue, state: enabled
Flag: stream_queue, state: enabled
Flag: user_limits, state: enabled
Flag: virtual_host_metadata, state: enabled
如果要为集群增加新节点时,我们可以按照上面的步骤将新节点添加到集群。
4、故障测试
4.1、停止一个节点
在集群中如果要停止一个节点执行命令
rabbitmqctl stop_app
或rabbitmqctl stop
或者可以kill
掉节点的进程不影响其他节点运行(注意: 如果只有一个磁盘节点,如果干掉磁盘节点后消息数据会丢失)。
# 停止 rabbitmq-3
[root@rabbitmq-3 ~]# rabbitmqctl stop_app
Stopping rabbit application on node rabbit@rabbitmq-3 ...
# 查看集群状态
[root@rabbitmq-1 ~]# rabbitmqctl cluster_status
4.2、重置一个节点
刚刚将 rabbitmq-3 给停止了,如果再次启动node3,那么还会加入到节点中来。
[root@rabbitmq-3 ~]# rabbitmqctl start_app
Starting node rabbit@rabbitmq-3 ...
# 查看集群
[root@rabbitmq-1 ~]# rabbitmqctl cluster_status
那么,如果才能将一个节点重置,与集群彻底脱离呢。
[root@rabbitmq-3 ~]# rabbitmqctl stop_app
Stopping rabbit application on node rabbit@rabbitmq-3 ...
[root@rabbitmq-3 ~]# rabbitmqctl reset
Resetting node rabbit@rabbitmq-3 ...
[root@rabbitmq-3 ~]# rabbitmqctl start_app
Starting node rabbit@rabbitmq-3 ...
然后再查看一下分离之后的集群状态
# rabbitmq-1 集群
[root@rabbitmq-1 ~]# rabbitmqctl cluster_status
# rabbitmq-3 集群
[root@rabbitmq-3 ~]# rabbitmqctl cluster_status
4.3、设置内存节点
上边两个节点加入集群的时候,默认的是作为磁盘节点,如果想要创建内存节点,只需在创建的时候多加一个
--ram
参数即可。
这里重新加入的时候 如果中间启动过,那么就要关闭后再次配置一下cookie
# 中间我是保持reset的状态的哈
[root@rabbitmq-1 ~]# scp /var/lib/rabbitmq/.erlang.cookie root@rabbitmq-3:/var/lib/rabbitmq/
[root@rabbitmq-3 ~]# rabbitmqctl stop_app
Stopping rabbit application on node rabbit@rabbitmq-3 ...
[root@rabbitmq-3 ~]# rabbitmqctl join_cluster rabbit@rabbitmq-1 --ram
Clustering node rabbit@rabbitmq-3 with rabbit@rabbitmq-1
[root@rabbitmq-3 ~]# rabbitmqctl start_app
Starting node rabbit@rabbitmq-3 ...
# 查看集群中的RAM服务器
[root@rabbitmq-1 ~]# rabbitmqctl cluster_status
4.4、更改节点属性
先将刚刚的 rabbitmq-3 改回到磁盘节点:
[root@rabbitmq-3 ~]# rabbitmqctl stop_app
Stopping rabbit application on node rabbit@rabbitmq-3 ...
[root@rabbitmq-3 ~]# rabbitmqctl change_cluster_node_type disc
[root@rabbitmq-3 ~]# rabbitmqctl start_app
Starting node rabbit@rabbitmq-3 ...
# 查看集群属性
[root@rabbitmq-1 ~]# rabbitmqctl cluster_status
# 然后再把 rabbitmq-2 变更为内存节点:
[root@rabbitmq-2 ~]# rabbitmqctl stop_app
[root@rabbitmq-2 ~]# rabbitmqctl change_cluster_node_type ram
# 中间可能会有错误但是可以忽略
[root@rabbitmq-2 ~]# rabbitmqctl start_app
Starting node rabbit@rabbitmq-2 ...
# 再次查看集群
[root@rabbitmq-1 ~]# rabbitmqctl cluster_status
登录后台
打开网页管理页面查看 nodes
最后编辑:Jeebiz 更新时间:2024-11-01 10:06