微服务架构师封神之路13-RabbitMQ集群与高可用|RabbitMQ clustering and HA

目录

几个关键技术点

节点间相互验证,.erlang.cookie

nodename的唯一性

port冲突与配置

[Management UI plugin安装](#Management UI plugin安装)

Queue(message)replicas

[Queue leader strategy](#Queue leader strategy)

配置文件详细

[Node 1](#Node 1)

[Installation path](#Installation path)

.erlang.cookie

rabbitmq-env.conf

rabbitmq.conf

enabled_plugins

[Node 2](#Node 2)

[Installation path](#Installation path)

.erlang.cookie

rabbitmq-env.conf

rabbitmq.conf

enabled_plugins

[Node 3](#Node 3)

[Installation path](#Installation path)

.erlang.cookie

rabbitmq-env.conf

rabbitmq.conf

enabled_plugins

Springboot连接与编程

application.yml

[Queue and AnonymousQueue](#Queue and AnonymousQueue)

Queue

AnonymousQueue


本文利用一台Linux示范三节点RabbitMQ集群。RabbitMQ版本:3.12.11.

重点:

  1. 关键技术点
    1. 节点间相互验证,.erlang.cookie
    2. nodename的唯一性
    3. port冲突与配置
    4. Management UI plugin安装
    5. Queue replicas
    6. Queue leader strategy
  2. 配置文件详细
  3. Springboot连接与编程

几个关键技术点

节点间相互验证,.erlang.cookie

.erlang.cookie是erlang框架实现分布式的必要文件。三节点集群需要每个节点的.erlang.cookie文件相同。文件的位置:$home/.erlang.cookie.

erlang安装之后文件就已经存在,如果使用三台不同的机器,需要将其中一台的文件拷贝三份。用一台机器做示范,我们不需要再做额外的动作。

nodename的唯一性

RabbitMQ的nodename有固定的格式<prefix>@<hostname>。hostname可以通过操作系统的hostname命令获得。prefix可以自己决定。但是集群中的nodename必须是唯一的。

如果是部署在不同的机器上,所有的hostname需要明确配置在/etc/hosts文件中。

nodename配置在rabbitmq-env.conf文件中。

port冲突与配置

部署在同一台机器上,需要解决三个端口的冲突。

  1. amqp通信端口5672 - 相关配置文件rabbitmq.conf or rabbitmq-env.conf
  2. management UI端口15672 - 相关配置文件rabbitmq.conf
  3. 内部管理Command Line Interface端口25672 - 相关配置文件rabbitmq-env.conf

Management UI plugin安装

信息要放在enabled_plugin文件中。这个文件要手工添加,插件在RabbitMQ第一次启动的时候初始化。

Queue(message)replicas

高可用需要同一条消息要同时复制到三个节点上面,这样应用连接的当前节点下线后,还可以从其它另外两个节点读取还未消费完的消息。没有replica,虽然Rabbitmq依然可用,但会有消息丢失的情况。

要实现消息node间复制,需要创建Queue的时候, queue type设置成Quorum。

两种方式:

  • 通过管理界面创建Queue
  • Java应用在启动的时候配置Queue
java 复制代码
        Map QUEUE_ARGS = new HashMap<String,Object>();    
        QUEUE_ARGS.put("x-queue-type", "quorum");

        new Queue("queue_name", true, false, false, QUEUE_ARGS);

对于匿名Queue不能配置这个属性,当一个node down以后,应用会在新的节点重新创建临时Queue。可能产生message丢失的情况。

Queue leader strategy

在rabbitmq.conf中配置,两种client-local(by default)和balanced.

配置文件详细

Download rabbitmq package, rabbitmq-server-generic-unix-3.12.11.tar.xz. 解压后拷贝三份,作为三个node。

下面的文件除了.erlang.cookie都需要手动添加。

Node 1

Installation path

/opt/rabbitmq/rabbitmq-node-1

.erlang.cookie

$home/.erlang.cookie

rabbitmq-env.conf

File path: /opt/rabbitmq/rabbitmq-node-1/etc/rabbitmq/rabbitmq-env.conf

java 复制代码
# Example rabbitmq-env.conf file entries. Note that the variables
# do not have the RABBITMQ_ prefix.
#
# Overrides node name
RABBITMQ_NODENAME=rabbitmq-node-1@DESKTOP-B5WANG

# Specifies new style config file location
RABBITMQ_CONFIG_FILE=/opt/rabbitmq/rabbitmq-node-1/etc/rabbitmq/rabbitmq.conf

# Specifies advanced config file location
# RABBITMQ_ADVANCED_CONFIG_FILE=/etc/rabbitmq/advanced.config

# CLI port
RABBITMQ_DIST_PORT=25672

rabbitmq.conf

File path: /opt/rabbitmq/rabbitmq-node-1/etc/rabbitmq/rabbitmq.conf

java 复制代码
# port
listeners.tcp.default = 5672
# listeners.tcp.1 = 0.0.0.0:5672

# default user
default_vhost = /
default_user = admin
default_pass = admin
default_permissions.configure = .*
default_permissions.read = .*
default_permissions.write = .*
default_user_tags.administrator = true
default_user_tags.management = true
default_user_tags.custom_tag = true

# management plugin
management.tcp.port = 15672
management.tcp.ip   = 0.0.0.0

# cluster
cluster_name = rabbitmq-cluster
cluster_keepalive_interval = 10000
cluster_formation.peer_discovery_backend = classic_config
cluster_formation.classic_config.nodes.1 = rabbitmq-node-1@DESKTOP-B5WANG
cluster_formation.classic_config.nodes.2 = rabbitmq-node-2@DESKTOP-B5WANG
cluster_formation.classic_config.nodes.3 = rabbitmq-node-3@DESKTOP-B5WANG

# queue location strategy
queue_master_locator = client-local

enabled_plugins

File path: /opt/rabbitmq/rabbitmq-node-1/etc/rabbitmq/enabled_plugins

java 复制代码
[rabbitmq_management].

Node 2

Installation path

/opt/rabbitmq/rabbitmq-node-2

.erlang.cookie

$home/.erlang.cookie

rabbitmq-env.conf

File path: /opt/rabbitmq/rabbitmq-node-2/etc/rabbitmq/rabbitmq-env.conf

java 复制代码
# Example rabbitmq-env.conf file entries. Note that the variables
# do not have the RABBITMQ_ prefix.
#
# Overrides node name
RABBITMQ_NODENAME=rabbitmq-node-2@DESKTOP-B5WANG

# Specifies new style config file location
RABBITMQ_CONFIG_FILE=/opt/rabbitmq/rabbitmq-node-2/etc/rabbitmq/rabbitmq.conf

# Specifies advanced config file location
# RABBITMQ_ADVANCED_CONFIG_FILE=/etc/rabbitmq/advanced.config

# CLI port
RABBITMQ_DIST_PORT=25673

rabbitmq.conf

File path: /opt/rabbitmq/rabbitmq-node-2/etc/rabbitmq/rabbitmq.conf

java 复制代码
# port
listeners.tcp.default = 5673
# listeners.tcp.1 = 0.0.0.0:5673

# default user
default_vhost = /
default_user = admin
default_pass = admin
default_permissions.configure = .*
default_permissions.read = .*
default_permissions.write = .*
default_user_tags.administrator = true
default_user_tags.management = true
default_user_tags.custom_tag = true

# management plugin
management.tcp.port = 15673
management.tcp.ip   = 0.0.0.0

# cluster
cluster_name = rabbitmq-cluster
cluster_keepalive_interval = 10000
cluster_formation.peer_discovery_backend = classic_config
cluster_formation.classic_config.nodes.1 = rabbitmq-node-1@DESKTOP-B5WANG
cluster_formation.classic_config.nodes.2 = rabbitmq-node-2@DESKTOP-B5WANG
cluster_formation.classic_config.nodes.3 = rabbitmq-node-3@DESKTOP-B5WANG

# queue location strategy
queue_master_locator = client-local

enabled_plugins

File path: /opt/rabbitmq/rabbitmq-node-2/etc/rabbitmq/enabled_plugins

java 复制代码
[rabbitmq_management].

Node 3

Installation path

/opt/rabbitmq/rabbitmq-node-3

.erlang.cookie

$home/.erlang.cookie

rabbitmq-env.conf

File path: /opt/rabbitmq/rabbitmq-node-3/etc/rabbitmq/rabbitmq-env.conf

java 复制代码
# Example rabbitmq-env.conf file entries. Note that the variables
# do not have the RABBITMQ_ prefix.
#
# Overrides node name
RABBITMQ_NODENAME=rabbitmq-node-3@DESKTOP-B5WANG

# Specifies new style config file location
RABBITMQ_CONFIG_FILE=/opt/rabbitmq/rabbitmq-node-3/etc/rabbitmq/rabbitmq.conf

# Specifies advanced config file location
# RABBITMQ_ADVANCED_CONFIG_FILE=/etc/rabbitmq/advanced.config

# CLI port
RABBITMQ_DIST_PORT=25674

rabbitmq.conf

File path: /opt/rabbitmq/rabbitmq-node-3/etc/rabbitmq/rabbitmq.conf

java 复制代码
# port
listeners.tcp.default = 5674
# listeners.tcp.1 = 0.0.0.0:5674

# default user
default_vhost = /
default_user = admin
default_pass = admin
default_permissions.configure = .*
default_permissions.read = .*
default_permissions.write = .*
default_user_tags.administrator = true
default_user_tags.management = true
default_user_tags.custom_tag = true

# management plugin
management.tcp.port = 15674
management.tcp.ip   = 0.0.0.0

# cluster
cluster_name = rabbitmq-cluster
cluster_keepalive_interval = 10000
cluster_formation.peer_discovery_backend = classic_config
cluster_formation.classic_config.nodes.1 = rabbitmq-node-1@DESKTOP-B5WANG
cluster_formation.classic_config.nodes.2 = rabbitmq-node-2@DESKTOP-B5WANG
cluster_formation.classic_config.nodes.3 = rabbitmq-node-3@DESKTOP-B5WANG

# queue location strategy
queue_master_locator = client-local

enabled_plugins

File path: /opt/rabbitmq/rabbitmq-node-3/etc/rabbitmq/enabled_plugins

java 复制代码
[rabbitmq_management].

Springboot连接与编程

application.yml

java 复制代码
spring:
  rabbitmq:
    # Connect rabbitmq cluster
    addresses: localhost:5672,localhost:5673,localhost:5674
    username: admin
    password: admin

Queue and AnonymousQueue

Queue

java 复制代码
        Map arg = new HashMap<String,Object>();
        arg.put("x-queue-type", "quorum");
        arg.put("x-queue-leader-locator", "client-local");
        new Queue(QUEUE_NAME_TEXT_MESSAGE, true, false, false, arg);

AnonymousQueue

java 复制代码
AnonymousQueue aq = new AnonymousQueue(new Base64UrlNamingStrategy("queue-prefix-"));

AnonymousQueue本质上还是Queue,只是属性不同。所以不需要配置quorum,否则应用会报错。

java 复制代码
public class AnonymousQueue extends Queue {
    public AnonymousQueue() {
        this((Map)null);
    }

    public AnonymousQueue(Map<String, Object> arguments) {
        this(Base64UrlNamingStrategy.DEFAULT, arguments);
    }

    public AnonymousQueue(NamingStrategy namingStrategy) {
        this(namingStrategy, (Map)null);
    }

    public AnonymousQueue(NamingStrategy namingStrategy, Map<String, Object> arguments) {
        super(namingStrategy.generateName(), false, true, true, arguments);
        if (!this.getArguments().containsKey("x-queue-master-locator")) {
            this.setLeaderLocator("client-local");
        }

    }
}
相关推荐
周末不下雨几秒前
win11+ubuntu22.04双系统 | 联想 24 y7000p | ubuntu 22.04 | 把ubuntu系统装到1T的移动固态硬盘上!!!
linux·运维·ubuntu
BestandW1shEs16 分钟前
彻底理解消息队列的作用及如何选择
java·kafka·rabbitmq·rocketmq
哎呦喂-ll41 分钟前
Linux进阶:环境变量
linux
Rverdoser42 分钟前
Linux环境开启MongoDB的安全认证
linux·安全·mongodb
PigeonGuan1 小时前
【jupyter】linux服务器怎么使用jupyter
linux·ide·jupyter
猿java1 小时前
什么是 Hystrix?它的工作原理是什么?
java·微服务·面试
东华果汁哥1 小时前
【linux 免密登录】快速设置kafka01、kafka02、kafka03 三台机器免密登录
linux·运维·服务器
咖喱鱼蛋2 小时前
Ubuntu安装Electron环境
linux·ubuntu·electron
ac.char2 小时前
在 Ubuntu 系统上安装 npm 环境以及 nvm(Node Version Manager)
linux·ubuntu·npm