从零开始搭建高可用RabbitMQ镜像模式集群

本文详细介绍了如何从零开始搭建RabbitMQ镜像模式集群,包括集群节点间的文件同步、节点加入集群、配置镜像队列策略,确保高可用性。接着讲解了负载均衡器HaProxy的安装与配置,以及Keepalived的安装与高可用原理,实现了VIP的自动故障切换。
摘要由CSDN通过智能技术生成

RabbitMQ集群模式搭建

集群架构图:
在这里插入图片描述

准备工作

准备三台安装了RabbitMQ的节点, 我本地三台虚拟机地址如下 :

IP hostname
192.168.72.138:5672 rabbit138
192.168.72.139:5672 rabbit139
192.168.72.140:5672 rabbit140

选取任意一个节点作为master节点, 进行文件同步, 我这里选择138作为master节点

  • 将138主机/var/lib/rabbitmq/.erlang.cookie文件同步到139,140主机对应目录下
  • .erlang.cookie权限是400, 可能无法操作, 可以将权限改成777去复制, 我这里虚拟机是克隆出来的, 所以同步的这一步就省略了

组成集群

  1. 停止三个节点的MQ服务 : rabbitmqctl stop

  2. 在三个节点执行命令 : rabbitmq-server -detached

  3. slave加入集群操作(重新加入集群也是同样的操作, 以最开始的主节点作为加入节点)

    修改三台主机hosts:

    vi /etc/hosts, 添加如下三行:

    192.168.72.139 rabbit139
    192.168.72.138 rabbit138
    192.168.72.140 rabbit140

    在139,140主机上执行如下命令:

    rabbitmqctl stop_app

    rabbitmqctl join_cluster --ram rabbit@rabbit138

    rabbitmqctl start_app

    ram表示以内存方式加入集群, 不写默认以磁盘方式加入集群

    移除集群节点命令:

    将要移除的节点先停止, 使用命令rabbitmqctl stop_app, 然后在其他节点执行如下命令:

    rabbitmqctl forget_cluster_node rabbit@rabbit139

  4. 修改集群名称 : rabbitmqctl set_cluster_name rabbit-qiyexue

  5. 查看集群状态 : rabbitmqctl cluster_status

  6. 管控台界面, 任意一台主机的即可 : http://192.168.72.140:15672

配置镜像队列(设置镜像队列策略)

  • rabbitmqctl set_policy ha-all “^” ‘{“ha-mode”:“all”}’
  • 将所有队列设置为镜像队列,即队列会被复制到各个节点,各个节点状态一致,RabbitMQ高可用集群就已经搭建好了,我们可以重启服务,查看其队列是否在从节点同步。

表达式参数简介 :
pattern 是匹配队列名称的正则表达式 , 进行区分哪些队列使用哪些策略
definition 其实就是一些arguments, 支持如下参数:
1. ha-mode:One of all, exactly or nodes (the latter currently not supported by web UI).
2. ha-params:Absent if ha-mode is all, a number if ha-mode is exactly, or an array of strings if ha-mode is nodes.
3. ha-sync-mode:One of manual or automatic. *
4. federation-upstream-set:A string; only if the federation plugin is enabled.

	**ha-mode** 的参数:
	
	| ha-mode | ha-params    | Result                                                       |
	| ------- | ------------ | ------------------------------------------------------------ |
	| all     | (absent)     | Queue is mirrored across all nodes in the cluster. When a new node is added to the cluster, the queue will be mirrored to that node. |
	| exactly | *count*      | Queue is mirrored to *count* nodes in the cluster. If there are less than *count* nodes in the cluster, the queue is mirrored to all nodes. If there are more than *count*nodes in the cluster, and a node containing a mirror goes down, then a new mirror will not be created on another node. (This is to prevent queues migrating across a cluster as it is brought down.) |
	| nodes   | *node names* | Queue is mirrored to the nodes listed in *node names*. If any of those node names are not a part of the cluster, this does not constitute an error. If none of the nodes in the list are online at the time when the queue is declared then the queue will be created on the node that the declaring client is connected to. |

至此, RabbitMQ镜像模式集群就已经搭建完成了

集群配置参数详解

配置文件如下:

{
   application, rabbit,           %% -*- erlang -*-
 [{
   description, "RabbitMQ"},
  {
   id, "RabbitMQ"},
  {
   vsn, "3.6.5"},
  {
   modules, ['background_gc','delegate','delegate_sup','dtree','file_handle_cache','file_handle_cache_stats','gatherer','gm','lqueue','mirrored_supervisor_sups','mnesia_sync','mochinum','pg2_fixed','pg_local','rabbit','rabbit_access_control','rabbit_alarm','rabbit_amqqueue_process','rabbit_amqqueue_sup','rabbit_amqqueue_sup_sup','rabbit_auth_mechanism_amqplain','rabbit_auth_mechanism_cr_demo','rabbit_auth_mechanism_plain','rabbit_autoheal','rabbit_binding','rabbit_boot_steps','rabbit_channel_sup','rabbit_channel_sup_sup','rabbit_cli','rabbit_client_sup','rabbit_connection_helper_sup','rabbit_connection_sup','rabbit_control_main','rabbit_ctl_usage','rabbit_dead_letter','rabbit_diagnostics','rabbit_direct','rabbit_disk_monitor','rabbit_epmd_monitor','rabbit_error_logger','rabbit_error_logger_file_h','rabbit_exchange','rabbit_exchange_parameters','rabbit_exchange_type_direct','rabbit_exchange_type_fanout','rabbit_exchange_type_headers','rabbit_exchange_type_invalid','rabbit_exchange_type_topic','rabbit_file','rabbit_framing','rabbit_guid','rabbit_hipe','rabbit_limiter','rabbit_log','rabbit_memory_monitor','rabbit_mirror_queue_coordinator','rabbit_mirror_queue_master','rabbit_mirror_queue_misc','rabbit_mirror_queue_mode','rabbit_mirror_queue_mode_all','rabbit_mirror_queue_mode_exactly','rabbit_mirror_queue_mode_nodes','rabbit_mirror_queue_slave','rabbit_mirror_queue_sync','rabbit_mnesia','rabbit_mnesia_rename','rabbit_msg_file','rabbit_msg_store','rabbit_msg_store_ets_index','rabbit_msg_store_gc','rabbit_node_monitor','rabbit_parameter_validation','rabbit_password','rabbit_password_hashing_md5','rabbit_password_hashing_sha256','rabbit_password_hashing_sha512','rabbit_plugins','rabbit_plugins_main'<
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值