上文参考: rabbitmq集群-普通模式
它和普通集群最大的区别在于 Queue 数据和原数据不再是单独存储在一台机器上,而是同时存储在多台机器上。也就是说每个 RabbitMQ 实例都有一份镜像数据(副本数据)。每次写入消息的时候都会自动把数据同步到多台实例上去,这样一旦其中一台机器发生故障,其他机器还有一份副本数据可以继续提供服务,也就实现了高可用。
所谓的镜像集群模式并不需要额外搭建,只需要我们将队列配置为镜像队列即可。
这个配置可以通过网页配置,也可以通过命令行配置,我们分别来看。
配置页面,我们可以通过配置页面配置不同的policy
各参数含义如下:
Name: policy 的名称。
Pattern: queue 的匹配模式(正则表达式)。
Definition:镜像定义,主要有三个参数:ha-mode, ha-params, ha-sync-mode。
add结束,我们可以在其他的ui管理界面比如http://localhost:15674/#/policies看到同步的policy
配置结束后,我们做个测试,测试参考rabbitmq集群-普通模式中的3.2 基于3.1,停机publish发布的节点-rabbit1,其他节点会丢失该节点信息 在做测试,我们发现节点rabbit2,rabbit3依然存在(截图没有截rabbit3的)
我们停止了rabbit1节点
启动consumer,message被消费了一次
命令行的配置格式如下:
rabbitmqctl set_policy [-p vhost] [--priority priority] [--apply-to apply-to] {name} {pattern} {definition}
举一个简单的配置案例
rabbitmqctl set_policy -p / --apply-to queues my_queue_mirror "^" '{"ha-mode":"all","ha-sync-mode":"automatic"}'
在高并发下,集群当然是更好的选择,如果数据量很大的话,比如处理数据达到了几千万甚至上亿,集群也很被推荐,但是,集群并不意味着就是比单体更好的,我们都知道,集群也有如下缺点
参考 Using RabbitMQ in Cluster
Sending 100k messages to the single instance took about 10 seconds, while sending them to the first instance from cluster took about 54 seconds. There is also a huge difference in processing messages on the receiver side. Receiving messages from single standalone queue took about 43 seconds while receiving then from the clustered HA queue took about 132 seconds.
所以,是否集群也是看业务需求。