RabbitMQ其他操作
我们关闭rabbit@server-1和rabbit@server-3,并检查每一步中的集群状态
[root@server-1 rabbitmq]# rabbitmqctl stop_app Stopping node 'rabbit@server-1' ... ...done. [root@server-2 ~]# rabbitmqctl cluster_status Cluster status of node 'rabbit@server-2' ... [{nodes,[{disc,['rabbit@server-1']}, {ram,['rabbit@server-3','rabbit@server-2']}]}, {running_nodes,['rabbit@server-3','rabbit@server-2']}, {cluster_name,<<"rabbit@server-1">>}, {partitions,[]}] ...done. [root@server-3 ~]# rabbitmqctl cluster_status Cluster status of node 'rabbit@server-3' ... [{nodes,[{disc,['rabbit@server-1']}, {ram,['rabbit@server-3','rabbit@server-2']}]}, {running_nodes,['rabbit@server-2','rabbit@server-3']}, {cluster_name,<<"rabbit@server-1">>}, {partitions,[]}] ...done.
[root@server-3 ~]# rabbitmqctl stop_app Stopping node 'rabbit@server-3' ... ...done. [root@server-3 ~]# rabbitmqctl cluster_status Cluster status of node 'rabbit@server-3' ... [{nodes,[{disc,['rabbit@server-1']}, {ram,['rabbit@server-2','rabbit@server-3']}]}] ...done. [root@server-1 rabbitmq]# rabbitmqctl cluster_status Cluster status of node 'rabbit@server-1' ... [{nodes,[{disc,['rabbit@server-1']}, {ram,['rabbit@server-2','rabbit@server-3']}]}] ...done.
[root@server-3 ~]# rabbitmqctl start_app Starting node 'rabbit@server-3' ... ...done. [root@server-3 ~]# rabbitmqctl start_app Starting node 'rabbit@server-3' ... ...done. [root@server-3 ~]# rabbitmqctl cluster_status Cluster status of node 'rabbit@server-3' ... [{nodes,[{disc,['rabbit@server-1']}, {ram,['rabbit@server-3','rabbit@server-2']}]}, {running_nodes,['rabbit@server-2','rabbit@server-1','rabbit@server-3']}, {cluster_name,<<"rabbit@server-1">>}, {partitions,[]}] ...done. ...done. [root@server-2 ~]# rabbitmqctl cluster_status Cluster status of node 'rabbit@server-2' ... [{nodes,[{disc,['rabbit@server-1']}, {ram,['rabbit@server-3','rabbit@server-2']}]}, {running_nodes,['rabbit@server-3','rabbit@server-1','rabbit@server-2']}, {cluster_name,<<"rabbit@server-1">>}, {partitions,[]}] ...done. [root@server-1 rabbitmq]# rabbitmqctl cluster_status Cluster status of node 'rabbit@server-1' ... [{nodes,[{disc,['rabbit@server-1']}, {ram,['rabbit@server-3','rabbit@server-2']}]}, {running_nodes,['rabbit@server-3','rabbit@server-2','rabbit@server-1']}, {cluster_name,<<"rabbit@server-1">>}, {partitions,[]}] ...done.
一些重要的警告:
当整个集群关闭时,最后一个关闭的节点必须是第一个要联机的节点。
如果要脱机的最后一个节点无法恢复,可以使用forget_cluster_node命令将其从群集中删除
如果所有集群节点同时停止并且不受控制(例如断电),则可能会留下所有节点都认为其他节点在其后停止的情况。在这种情况下,您可以在一个节点上使用force_boot命令使其再次可引导
集群移除节点
[root@server-3 ~]# rabbitmqctl stop_app Stopping node 'rabbit@server-3' ... ...done. [root@server-3 ~]# rabbitmqctl reset Resetting node 'rabbit@server-3' ... ...done. [root@server-3 ~]# rabbitmqctl start_app Starting node 'rabbit@server-3' ... ...done.
在节点上 运行cluster_status命令确认rabbit@server-3现在不再是集群的一部分并独立运行
[root@server-3 ~]# rabbitmqctl cluster_status Cluster status of node 'rabbit@server-3' ... [{nodes,[{disc,['rabbit@server-3']}]}, {running_nodes,['rabbit@server-3']}, {cluster_name,<<"rabbit@server-3">>}, {partitions,[]}] ...done.
[root@server-1 rabbitmq]# rabbitmqctl cluster_status Cluster status of node 'rabbit@server-1' ... [{nodes,[{disc,['rabbit@server-1']},{ram,['rabbit@server-2']}]}, {running_nodes,['rabbit@server-2','rabbit@server-1']}, {cluster_name,<<"rabbit@server-1">>}, {partitions,[]}] ...done. [root@server-2 ~]# rabbitmqctl cluster_status Cluster status of node 'rabbit@server-2' ... [{nodes,[{disc,['rabbit@server-1']},{ram,['rabbit@server-2']}]}, {running_nodes,['rabbit@server-1','rabbit@server-2']}, {cluster_name,<<"rabbit@server-1">>}, {partitions,[]}] ...done.
我们也可以远程删除节点,例如,在处理无响应的节点时,这很有用
比如:我们在节点rabbit@server-2上把rabbit@server-1从集群中移除
[root@server-1 rabbitmq]# rabbitmqctl stop_app Stopping node 'rabbit@server-1' ... ...done. [root@server-2 ~]# rabbitmqctl forget_cluster_node rabbit@server-1 Removing node 'rabbit@server-1' from cluster ... ...done.
请注意,server-1仍然认为它与server-2集群 ,并试图启动它将导致错误。我们需要重新设置才能重新启动。
[root@server-1 ~]# rabbitmqctl reset #必须要重置 Resetting node rabbit@rabbit1 ... [root@server-1 ~]# rabbitmqctl start_app Starting node rabbit@rabbit1 ... completed with 0 plugins.
server-2是内存节点,server-1是磁盘节点,rabbitmqctl reset时会报错,需要把server-2也转成磁盘节点才可以。
从客户端连接到群集
客户端可以正常连接到群集中的任何节点。如果该节点出现故障,并且集群的其余部分仍然存在,那么客户端应该注意到已关闭的连接,并且应该能够重新连接到群集的一些幸存的成员。通常,将节点主机名或IP地址烧入客户端应用程序是不可取的:这会引入不灵活性,并且如果集群配置发生更改或集群中节点数发生更改,则需要编辑,重新编译和重新部署客户端应用程序。相反,我们推荐一个更抽象的方法:这可能是一个动态的DNS服务,它具有非常短的TTL配置,或者一个普通的TCP负载均衡器,或者用起搏器或类似技术实现的某种移动IP。一般来说
具有RAM节点的集群
RAM节点只将其元数据保存在内存中。由于RAM节点不必像光盘节点那样写入光盘,它们可以更好地执行。但是请注意,由于永久队列数据总是存储在磁盘上,因此性能改进将仅影响资源管理(例如添加/删除队列,交换或虚拟主机),但不会影响发布速度或消耗速度
RAM节点是高级用例; 设置你的第一个群集时,你应该不使用它们。您应该有足够的光盘节点来处理您的冗余要求,然后在需要时添加额外的RAM节点进行缩放
只包含RAM节点的集群是脆弱的; 如果群集停止,您将无法再次启动, 并将丢失所有数据。RabbitMQ将阻止在许多情况下创建RAM节点的群集,但是它不能完全阻止它
这里的例子仅仅为了简单起见,显示了具有一个光盘和一个RAM节点的集群; 这样的集群是一个糟糕的设计选择
创建RAM节点
我们可以在首次加入集群时将节点声明为RAM节点。像之前一样,我们使用rabbitmqctl join_cluster来完成此 操作,但传递 --ram标志
更改节点类型
我们可以将节点的类型从ram更改为disc,反之亦然。假设我们想要颠倒rabbit @ rabbit2和rabbit @ rabbit1的类型 ,将前者从ram节点转换为disc节点,将后者从disc节点转换为ram节点。要做到这一点,我们可以使用 change_cluster_node_type命令。该节点必须先停止
[root@server-1 rabbitmq]# rabbitmqctl stop_app Stopping node 'rabbit@server-1' ... ...done. [root@server-1 rabbitmq]# rabbitmqctl change_cluster_node_type ram Turning 'rabbit@server-1' into a ram node ... ...done. [root@server-1 rabbitmq]# rabbitmqctl start_app Starting node 'rabbit@server-1' ... ...done. [root@server-1 rabbitmq]# rabbitmqctl cluster_status Cluster status of node 'rabbit@server-1' ... [{nodes,[{disc,['rabbit@server-2']}, {ram,['rabbit@server-3','rabbit@server-1']}]}, {running_nodes,['rabbit@server-3','rabbit@server-1']}, {cluster_name,<<"rabbit@server-1">>}, {partitions,[]}] ...done.
[root@server-2 ~]# rabbitmqctl stop_app Stopping node 'rabbit@server-2' ... ...done. [root@server-2 ~]# rabbitmqctl change_cluster_node_type disc Turning 'rabbit@server-2' into a disc node ... ...done. [root@server-2 ~]# rabbitmqctl start_app