0


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

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

重点:

  1. 关键技术点 1. 节点间相互验证,.erlang.cookie2. nodename的唯一性3. port冲突与配置4. Management UI plugin安装5. Queue replicas6. 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
        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

# 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

# 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

[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

# 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

# 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

[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

# 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

# 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

[rabbitmq_management].

Springboot连接与编程

application.yml

spring:
  rabbitmq:
    # Connect rabbitmq cluster
    addresses: localhost:5672,localhost:5673,localhost:5674
    username: admin
    password: admin

Queue and AnonymousQueue

Queue

        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

AnonymousQueue aq = new AnonymousQueue(new Base64UrlNamingStrategy("queue-prefix-"));

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

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");
        }

    }
}

本文转载自: https://blog.csdn.net/b5wang/article/details/136229195
版权归原作者 B5WANG 所有, 如有侵权,请联系我们删除。

“微服务架构师封神之路13-RabbitMQ集群与高可用|RabbitMQ clustering and HA”的评论:

还没有评论