build environment
host | IP | CPU name |
---|---|---|
MQ01 | 192.168.0.107 | rabbitmq01 |
MQ02 | 192.168.0.86 | rabbitmq02 |
Install MQ
Download MQ
wget wget https://packagecloud.io/rabbitmq/rabbitmq-server/packages/el/7/rabbitmq-server-3.8.16-1.el7.noarch.rpm
wget https://packagecloud.io/rabbitmq/erlang/packages/el/7/erlang-23.2.7-1.el7.x86_64.rpm
Install MQ
yum localinstall -y erlang-23.2.7-1.el7.x86_64.rpm
yum localinstall -y rabbitmq-server-3.8.16-1.el7.noarch.rpm
Write master file
touch /etc/rabbitmq/rabbitmq.conf
chow .rabbitmq /etc/rabbitmq/ -R
Vim /etc/rabbitmq/rabbitmq.conf
#禁止来宾访问
loopback_users.guest = false
#监听端口
listeners.tcp.default = 5672
#默认用户名和密码
default_pass = admin
default_user = admin
#WEB管理端端口
management.tcp.port = 15672
management.tcp.ip = 0.0.0.0
#WEB默认访问路径(建议不配置)
management.path_prefix=mq
Modify data, log storage location
mkdir /data/rabbitmq/{
logs/data}
chown rabitmq:rabbitmq /data/rabbitmq -R
vim /etc/rabbitmq/rabbitmq-env.conf
#添加如下两行内容
RABBITMQ_MNESIA_BASE=/data/rabbitmq/data
RABBITMQ_LOG_BASE=/data/rabbitmq/logs
Start MQ
systemctl stop firewalld
systemctl start rabbitmq-server
Set hostname connection
MQ01:
hostnamectl set-hostname rabbitmq01
bash
echo "192.168.0.86 rabbitmq02" >>/etc/hosts
echo "192.168.0.107 rabbitmq01" >>/etc/hosts
ping rabbitmq02
MQ02:
hostnamectl set-hostname rabbitmq02
bash
echo "192.168.0.86 rabbitmq02" >>/etc/hosts
echo "192.168.0.107 rabbitmq01" >>/etc/hosts
ping rabbitmq01
set.erlang.cookie
MQ01:
echo "Weihu2023@" >/var/lib/rabbitmq/.erlang.cookie
systemctl restart rabbitmq-server
MQ02:
echo "Weihu2023@" >/var/lib/rabbitmq/.erlang.cookie
systemctl restart rabbitmq-server
Build a cluster
Node joins the cluster
MQ02:
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster rabbit@rabbitmq01
rabbitmqctl start_app
By default, RabbitMQ starts as a disk node. If you want to join as a memory node, you can add the --ram parameter.
If you want to modify the node type, you can use the command:
rabbitmqctl change_cluster_node_type disc(ram)
Note that since RAM nodes only store internal database tables in memory, these data must be synchronized from other nodes when the RAM node starts, so a cluster must contain at least one disk node.
set cluster name
rabbitmqctl set_cluster_name rabbitmq_test_cluster
At this point, the ordinary cluster has been built. If you need to set up a mirrored cluster, you need to continue to perform the following operations
Set up a mirrored cluster
- command line settings
rabbitmqctl set_policy "queue_policy_test" "^" '{"ha-mode":"all","ha-sync-mode":"automatic"}'
view rules
rabbitmqctl list_policies
- Management interface settings
1. Create rules
各参数含义如下:
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: exactly 模式需要用到的参数,
ha-sync-mode: 进行队列中消息的同步方式,有效值为 automatic 和 manual。
priority 为可选参数,表示 policy 的优先级。
2. Check the rules
3. Verify the rules (create a queue on a random node and check whether it is synchronized to two nodes)
-
Before creating a mirror policy
-
After creating the mirror policy
Cluster Common Commands
View cluster status
rabbitmqctl cluster_status