准备3台服务器安装配置镜像集群
IP1:192.168.0.1
IP2:192.168.0.2
IP3:192.168.0.3
yum install -y lrzsz telnet nc iftop unzip yum-utils epel-release
systemctl stop firewalld.service && systemctl disable firewalld.service
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
vi /etc/docker/daemon.json
{
"data-root": "/data/docker"
}
systemctl enable docker.service
systemctl enable containerd.service
systemctl start docker
一、启动集群容器
docker run -d --name rabbitmq-node1 --hostname rabbitmq-node1 \
--add-host rabbitmq-node2:192.168.0.2 \
--add-host rabbitmq-node3:192.168.0.3 \
-p "1883:1883" \
-p "4369:4369" \
-p "5672:5672" \
-p "15672:15672" \
-p "25672:25672" \
-p "35672:35672" \
-e RABBITMQ_DEFAULT_USER=admin \
-e RABBITMQ_DEFAULT_PASS=123456 \
-e RABBITMQ_ERLANG_COOKIE="crazy" \
-v /data/rabbitMQ/rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf \
-v /data/rabbitMQ/data:/var/lib/rabbitmq \
--restart=always \
rabbitmq:3.11.9-management
vi /data/rabbitMQ/rabbitmq.conf
## On first start RabbitMQ will create a vhost and a user. These
## config items control what gets created.
## Relevant doc guide: https://rabbitmq.com/access-control.html
##
default_vhost = /
default_user = admin
default_pass = 123456
mqtt.listeners.tcp.default = 1883
vm_memory_high_watermark.absolute = 4GB
docker run -d --name rabbitmq-node2 --hostname rabbitmq-node2 \
--add-host rabbitmq-node1:192.168.0.1 \
--add-host rabbitmq-node3:192.168.0.3 \
-p "1883:1883" \
-p "4369:4369" \
-p "5672:5672" \
-p "15672:15672" \
-p "25672:25672" \
-p "35672:35672" \
-e RABBITMQ_DEFAULT_USER=admin \
-e RABBITMQ_DEFAULT_PASS=123456 \
-e RABBITMQ_ERLANG_COOKIE="crazy" \
-v /data/rabbitMQ/rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf \
-v /data/rabbitMQ/data:/var/lib/rabbitmq \
--restart=always \
rabbitmq:3.11.9-management
docker run -d --name rabbitmq-node3 --hostname rabbitmq-node3 \
--add-host rabbitmq-node1:192.168.0.1 \
--add-host rabbitmq-node2:192.168.0.2 \
-p "1883:1883" \
-p "4369:4369" \
-p "5672:5672" \
-p "15672:15672" \
-p "25672:25672" \
-p "35672:35672" \
-e RABBITMQ_DEFAULT_USER=admin \
-e RABBITMQ_DEFAULT_PASS=123456 \
-e RABBITMQ_ERLANG_COOKIE="crazy" \
-v /data/rabbitMQ/rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf \
-v /data/rabbitMQ/data:/var/lib/rabbitmq \
--restart=always \
rabbitmq:3.11.9-management
在rabbitmq-node2节点执行加入集群:
docker exec -it rabbitmq-node2 /bin/bash
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster --ram rabbit@rabbitmq-node1
rabbitmqctl start_app
rabbitmqctl cluster_status
在rabbitmq-node3节点执行加入集群:
docker exec -it rabbitmq-node3 /bin/bash
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster --ram rabbit@rabbitmq-node1
rabbitmqctl start_app
rabbitmqctl cluster_status
二、配置镜像队列
方式1: 命令行方式
在其中任意一个节点上执行
rabbitmqctl set_policy ct-ha "^" '{"ha-mode":"all","ha-sync-mode":"automatic"}'
方式2: web界面方式
将所有队列设置为镜像队列,并且队列会被复制到所有节点,各个节点状态一致。到此为止RabbitMQ高可用集群就已经搭建完成,然后重启服务测试一下其队列是否在从节点同步。
三、安装插件 rabbitmq_delayed_message_exchange、rabbitmq_mqtt
首先从rabbitmq官网下载插件rabbitmq_delayed_message_exchange-3.11.1.ez
docker cp /root/src/rabbitmq_delayed_message_exchange-3.11.1.ez rabbitmq-node1:/plugins/
rabbitmq-plugins enable rabbitmq_delayed_message_exchange
rabbitmq-plugins enable rabbitmq_mqtt
rabbitmq-plugins list -e
如有多节点,将其他节点执行同样操作即可
四、配置用户权限
默认admin用户也只有登录权限,需要授予读写权限
rabbitmqctl list_users
rabbitmqctl set_permissions -p / admin '.*' '.*' '.*'
rabbitmqctl list_user_permissions admin
常用命令
#删除用户guest
rabbitmqctl delete_user guest
#将rabbitmq-node2踢出集群
rabbitmqctl stop_app
rabbitmqctl forget_cluster_node rabbit@rabbitmq-node2
更改节点存储类型,disc表示磁盘,ram表示内存
rabbitmqctl change_cluster_node_type ram
注意避坑
集群节点安装延时插件rabbitmq_delayed_message_exchange 报错:
root@rabbitmq-node3:/# rabbitmq-plugins enable rabbitmq_delayed_message_exchange
RABBITMQ_ERLANG_COOKIE env variable support is deprecated and will be REMOVED in a future version. Use the $HOME/.erlang.cookie file or the --erlang-cookie switch instead.
Enabling plugins on node rabbit@rabbitmq-node3:
rabbitmq_delayed_message_exchange
The following plugins have been configured:
rabbitmq_delayed_message_exchange
rabbitmq_management
rabbitmq_management_agent
rabbitmq_mqtt
rabbitmq_prometheus
rabbitmq_web_dispatch
Applying plugin configuration to rabbit@rabbitmq-node3...
Stack trace:
** (CaseClauseError) no case clause matching: {:error, {:timeout_waiting_for_tables, [:"rabbit@rabbitmq-node1", :"rabbit@rabbitmq-node3", :"rabbit@rabbitmq-node2"], [:"rabbit_delayed_messagerabbit@rabbitmq-node3", :"rabbit_delayed_messagerabbit@rabbitmq-node3_index"]}}
(rabbitmqctl 3.11.0) lib/rabbitmq/cli/plugins/plugins_helpers.ex:112: RabbitMQ.CLI.Plugins.Helpers.update_enabled_plugins/4
(rabbitmqctl 3.11.0) lib/rabbitmq/cli/plugins/commands/enable_command.ex:127: anonymous fn/6 in RabbitMQ.CLI.Plugins.Commands.EnableCommand.do_run/2
(elixir 1.14.2) lib/stream.ex:1527: anonymous fn/2 in Stream.iterate/2
(elixir 1.14.2) lib/stream.ex:1740: Stream.do_unfold/4
(elixir 1.14.2) lib/stream.ex:1811: Enumerable.Stream.do_each/4
(elixir 1.14.2) lib/stream.ex:1051: Stream.do_transform_inner_enum/7
(elixir 1.14.2) lib/stream.ex:1811: Enumerable.Stream.do_each/4
(elixir 1.14.2) lib/enum.ex:2514: Enum.reduce_while/3
{:case_clause, {:error, {:timeout_waiting_for_tables, [:"rabbit@rabbitmq-node1", :"rabbit@rabbitmq-node3", :"rabbit@rabbitmq-node2"], [:"rabbit_delayed_messagerabbit@rabbitmq-node3", :"rabbit_delayed_messagerabbit@rabbitmq-node3_index"]}}}
解决方法:将集群节点退出集群然后以磁盘节点重新加入集群即可。内存节点无法正常安装延时插件。