集群搭建
1.新建compose.yaml文件,将下列代码复制到compose.yaml中。文件中声明了三个mq实例mq1、mq2、mq3, 用户名:test,密码:123321,端口如下
主机名 | 控制台端口 | AMQP端口 |
mq1 | 15672 | 5672 |
mq2 | 15673 | 5673 |
mq3 | 15674 | 5674 |
services:
mq1:
image: rabbitmq:3-management
container_name: mq1
volumes:
- ./mq1/rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf
- ./mq1/.erlang.cookie:/var/lib/rabbitmq/.erlang.cookie
environment:
- RABBITMQ_DEFAULT_USER=test
- RABBITMQ_DEFAULT_PASS=123321
hostname: mq1
ports:
- 15672:15672
- 5672:5672
mq2:
image: rabbitmq:3-management
container_name: mq2
volumes:
- ./mq2/rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf
- ./mq2/.erlang.cookie:/var/lib/rabbitmq/.erlang.cookie
environment:
- RABBITMQ_DEFAULT_USER=test
- RABBITMQ_DEFAULT_PASS=123321
hostname: mq2
ports:
- 15673:15672
- 5673:5672
mq3:
image: rabbitmq:3-management
container_name: mq3
volumes:
- ./mq3/rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf
- ./mq3/.erlang.cookie:/var/lib/rabbitmq/.erlang.cookie
environment:
- RABBITMQ_DEFAULT_USER=test
- RABBITMQ_DEFAULT_PASS=123321
hostname: mq3
ports:
- 15674:15672
- 5674:5672
2.新建install.sh将下面脚本复制到文件中 。该脚本会新建mq1、mq2、mq3三个文件夹,并分别在每个文件中下创建rabbitmq.conf和.erlang.cookie文件,并用docker compose命令部署mq集群,会以后台方式启动。
#!/bin/bash
i=1
while [[ $i -le 3 ]]
do
name=mq${i}
rm -rf ./${name}
mkdir ./${name}
touch ./${name}/rabbitmq.conf
echo "loopback_users.guest = false" >> ./${name}/rabbitmq.conf
echo "listeners.tcp.default = 5672" >> ./${name}/rabbitmq.conf
echo "cluster_formation.peer_discovery_backend = rabbit_peer_discovery_classic_config" >> ./${name}/rabbitmq.conf
echo "cluster_formation.classic_config.nodes.1 = rabbit@mq1" >> ./${name}/rabbitmq.conf
echo "cluster_formation.classic_config.nodes.2 = rabbit@mq2" >> ./${name}/rabbitmq.conf
echo "cluster_formation.classic_config.nodes.3 = rabbit@mq3" >> ./${name}/rabbitmq.conf
touch ./${name}/.erlang.cookie
echo KIXIIWYWZXVQXKRJEDUN > ./${name}/.erlang.cookie
chmod 600 ./${name}/.erlang.cookie
let "i+=1"
done
docker compose down
docker compose up -d
3.将compose.yaml与install.sh放到同一个文件夹下,并执行下面代码。可以用docker compose logs -f 查看日志,也可以用docker logs -f mq1 (mq1是主机名)分别查看每一台启动日志
sudo chmod +x install.sh && sudo ./install.sh