• RabbitMQ学习(三)-集群架构


    一、普通集群(副本集群)

    1.1、简介

    官方文档: https://www.rabbitmq.com/clustering.html

    All data/state required for the operation of a RabbitMQ broker is replicated across all nodes. An exception to this are message queues, which by default reside on one node, though they are visible and reachable from all nodes. To replicate queues across nodes in a cluster --摘自官网

    默认情况下:RabbitMQ代理操作所需的所有数据/状态都将跨所有节点复制。这方面的一个例外是消息队列,默认情况下,消息队列位于一个节点上,尽管它们可以从所有节点看到和访问

    1.2、架构图

    image

    缺点: 默认情况下,RabbitMQ集群中队列的内容仅位于单个节点上(即声明该队列的节点,也就是主节点)。创建队列时,只会在某一个节点上创建队列,其它节点上并不含有队列,而只是含有创建节点的元数据(包括队列信息,绑定关系等)。如果队列所在的节点故障,则队列就崩溃了

    1.3、集群搭建

    1)集群规划

    主机名 ip 角色
    rabbitmq1 10.0.0.11 master
    rabbitmq2 10.0.0.12 repl01
    rabbitmq310.0.0.13repl03

    2)配置映射关系

    [root@rabbitmq1 ~]# cat /etc/hosts
    127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
    
    10.0.0.11 rabbitmq1
    10.0.0.12 rabbitmq2
    10.0.0.13 rabbitmq3

    3)三台安装rabbitmq,以一台为例

    文档: https://www.cnblogs.com/hujinzhong/p/13523705.html

    [root@rabbitmq1 ~]# ll
    total 34388
    -rw-r--r-- 1 root root 19782364 Aug 18 15:43 erlang-22.3-1.el7.x86_64.rpm
    -rw-r--r-- 1 root root 15429219 Aug 18 15:46 rabbitmq-server-3.8.6-1.el7.noarch.rpm
    [root@rabbitmq1 ~]# rpm -ivh erlang-22.3-1.el7.x86_64.rpm 
    [root@rabbitmq1 ~]# yum install socat -y
    [root@rabbitmq1 ~]# rpm -ivh rabbitmq-server-3.8.6-1.el7.noarch.rpm
    
    #配置(模板下载地址:https://raw.githubusercontent.com/rabbitmq/rabbitmq-server/v3.8.6/docs/rabbitmq.conf.example)
    [root@rabbitmq1 ~]# vim /etc/rabbitmq/rabbitmq.conf
    ## Uncomment the following line if you want to allow access to the
    ## guest user from anywhere on the network.
    loopback_users.guest = false   #打开注释
    
    #启动rabbitmq中的插件管理
    [root@rabbitmq1 ~]# rabbitmq-plugins enable rabbitmq_management
    
    #启动
    [root@rabbitmq1 ~]# systemctl start rabbitmq-server
    [root@rabbitmq1 ~]# systemctl enable rabbitmq-server
    
    #web访问
    http://10.0.0.11:15672
    

    4)同步cookie文件

    可以将master上的/var/lib/rabbitmq/.erlang.cookie同步至其他节点

    [root@rabbitmq1 ~]# ll /var/lib/rabbitmq/.erlang.cookie
    -r-------- 1 rabbitmq rabbitmq 20 Aug 19 00:00 /var/lib/rabbitmq/.erlang.cookie
    [root@rabbitmq1 ~]# scp /var/lib/rabbitmq/.erlang.cookie root@10.0.0.12:/var/lib/rabbitmq/
    [root@rabbitmq1 ~]# scp /var/lib/rabbitmq/.erlang.cookie root@10.0.0.13:/var/lib/rabbitmq/
    
    #查看
    [root@rabbitmq1 ~]# cat /var/lib/rabbitmq/.erlang.cookie
    XARNAMUJEWELISZXCTRJ

    5)重启所有节点

    [root@rabbitmq1 ~]# systemctl restart rabbitmq-server
    #或者停止rabbitmq后,使用rabbitmq-server -detached(官方建议)
    

    6)在node2和node3执行加入集群命令

    #rabbitmq2上
    [root@rabbitmq2 ~]# rabbitmqctl stop_app
    Stopping rabbit application on node rabbit@rabbitmq2 ...
    [root@rabbitmq2 ~]# rabbitmqctl join_cluster rabbit@rabbitmq1
    Clustering node rabbit@rabbitmq2 with rabbit@rabbitmq1
    [root@rabbitmq2 ~]# rabbitmqctl start_app
    Starting node rabbit@rabbitmq2 ...
    
    #同理rabbitmq3
    

    7)查看集群状态,任意节点执行

    [root@rabbitmq1 ~]# rabbitmqctl cluster_status
    Cluster status of node rabbit@rabbitmq1 ...
    Basics
    
    Cluster name: rabbit@rabbitmq1
    
    Disk Nodes
    
    rabbit@rabbitmq1
    rabbit@rabbitmq2
    rabbit@rabbitmq3
    
    Running Nodes
    
    rabbit@rabbitmq1
    rabbit@rabbitmq2
    rabbit@rabbitmq3
    
    Versions
    
    rabbit@rabbitmq1: RabbitMQ 3.8.6 on Erlang 22.3
    rabbit@rabbitmq2: RabbitMQ 3.8.6 on Erlang 22.3
    rabbit@rabbitmq3: RabbitMQ 3.8.6 on Erlang 22.3
    
    Alarms
    
    (none)
    
    Network Partitions
    
    (none)
    
    Listeners
    
    Node: rabbit@rabbitmq1, interface: [::], port: 15672, protocol: http, purpose: HTTP API
    Node: rabbit@rabbitmq1, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
    Node: rabbit@rabbitmq1, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0
    Node: rabbit@rabbitmq2, interface: [::], port: 15672, protocol: http, purpose: HTTP API
    Node: rabbit@rabbitmq2, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
    Node: rabbit@rabbitmq2, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0
    Node: rabbit@rabbitmq3, interface: [::], port: 15672, protocol: http, purpose: HTTP API
    Node: rabbit@rabbitmq3, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
    Node: rabbit@rabbitmq3, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0
    
    Feature flags
    
    Flag: drop_unroutable_metric, state: disabled
    Flag: empty_basic_get_metric, state: disabled
    Flag: implicit_default_bindings, state: enabled
    Flag: quorum_queue, state: enabled
    Flag: virtual_host_metadata, state: enabled
    [root@rabbitmq1 ~]# rabbitmqctl cluster_status
    Cluster status of node rabbit@rabbitmq1 ...
    Basics
    
    Cluster name: rabbit@rabbitmq1
    
    Disk Nodes
    
    rabbit@rabbitmq1
    rabbit@rabbitmq2
    rabbit@rabbitmq3
    
    Running Nodes
    
    rabbit@rabbitmq1
    rabbit@rabbitmq2
    rabbit@rabbitmq3
    
    Versions
    
    rabbit@rabbitmq1: RabbitMQ 3.8.6 on Erlang 22.3
    rabbit@rabbitmq2: RabbitMQ 3.8.6 on Erlang 22.3
    rabbit@rabbitmq3: RabbitMQ 3.8.6 on Erlang 22.3
    
    Alarms
    
    (none)
    
    Network Partitions
    
    (none)
    
    Listeners
    
    Node: rabbit@rabbitmq1, interface: [::], port: 15672, protocol: http, purpose: HTTP API
    Node: rabbit@rabbitmq1, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
    Node: rabbit@rabbitmq1, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0
    Node: rabbit@rabbitmq2, interface: [::], port: 15672, protocol: http, purpose: HTTP API
    Node: rabbit@rabbitmq2, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
    Node: rabbit@rabbitmq2, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0
    Node: rabbit@rabbitmq3, interface: [::], port: 15672, protocol: http, purpose: HTTP API
    Node: rabbit@rabbitmq3, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
    Node: rabbit@rabbitmq3, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0
    
    Feature flags
    
    Flag: drop_unroutable_metric, state: disabled
    Flag: empty_basic_get_metric, state: disabled
    Flag: implicit_default_bindings, state: enabled
    Flag: quorum_queue, state: enabled
    Flag: virtual_host_metadata, state: enabled

    8)登录管理界面,展示如下状态

    image

    9)测试master上创建队列,其他节点查看

    image

    查看从节点队列状态:同步过来了

    image

    10)测试master节点宕机后,节点状态

    image

    image

    二、镜像集群

    2.1、介绍

    This guide covers mirroring (queue contents replication) of classic queues --摘自官网

    By default, contents of a queue within a RabbitMQ cluster are located on a single node (the node on which the queue was declared). This is in contrast to exchanges and bindings, which can always be considered to be on all nodes. Queues can optionally be made mirrored across multiple nodes. --摘自官网

    镜像队列机制就是将队列在三个节点之间设置主从关系,消息会在三个节点之间进行自动同步,且如果其中一个节点不可用,并不会导致消息丢失或服务不可用的情况,提升MQ集群的整体高可用性

    2.2、架构图

    image

    2.3、集群配置

    镜像集群在副本集群基础上做如下配置即可

    [root@rabbitmq1 ~]# rabbitmqctl set_policy ha-all '^' '{"ha-mode":"all","ha-sync-mode":"automatic"}' 
    Setting policy "ha-all" for pattern "^" to "{"ha-mode":"all","ha-sync-mode":"automatic"}" with priority "0" for vhost "/" ...

    查看集群状态:

    image

    image

    相关说明:

    # 0.策略说明
    	rabbitmqctl set_policy [-p <vhost>] [--priority <priority>] [--apply-to <apply-to>] <name> <pattern>  <definition>
    	-p Vhost: 可选参数,针对指定vhost下的queue进行设置
    	Name:     policy的名称
    	Pattern: queue的匹配模式(正则表达式)
    	Definition:镜像定义,包括三个部分ha-mode, ha-params, ha-sync-mode
               		ha-mode:指明镜像队列的模式,有效值为 all/exactly/nodes
                            all:表示在集群中所有的节点上进行镜像
                            exactly:表示在指定个数的节点上进行镜像,节点的个数由ha-params指定
                            nodes:表示在指定的节点上进行镜像,节点名称通过ha-params指定
                	 ha-params:ha-mode模式需要用到的参数
                    ha-sync-mode:进行队列中消息的同步方式,有效值为automatic和manual
                    priority:可选参数,policy的优先级
                    
                     
    # 1.查看当前策略
    	rabbitmqctl list_policies
    
    # 2.添加策略
    	rabbitmqctl set_policy ha-all '^hello' '{"ha-mode":"all","ha-sync-mode":"automatic"}' 
    	说明:策略正则表达式为 “^” 表示所有匹配所有队列名称  ^hello:匹配hello开头队列
    
    # 3.删除策略
    	rabbitmqctl clear_policy ha-all
    
    # 4.测试集群,当master宕机能自动切换,不影响业务

    2.4、docker单机镜像集群

    文档:https://www.jianshu.com/p/ad0257624224

  • 相关阅读:
    java截取字符串
    Integer
    Sql语句常用关键字
    mybatis三种传值方式
    mybatis中的#和$的区别
    374. Guess Number Higher or Lower
    278. First Bad Version
    69. Sqrt(x)
    35. Search Insert Position
    167. Two Sum II
  • 原文地址:https://www.cnblogs.com/hujinzhong/p/13530030.html
Copyright © 2020-2023  润新知