创建docker-compose.yml 文件
version: '3'
services:
rabbitmq1:
image: rabbitmq:3.8.3-management
container_name: rabbitmq1
restart: always
hostname: rabbitmq1
ports:
- 15683:15672
- 5683:5672
volumes:
- ./data1:/var/lib/rabbitmq
- ./rabbitmq.sh:/home/rabbitmq.sh
- ./etc/hosts:/etc/hosts
environment:
- RABBITMQ_DEFAULT_USER=root
- RABBITMQ_DEFAULT_PASS=root
- RABBITMQ_ERLANG_COOKIE=CURIOAPPLICATION
networks:
rabbit:
ipv4_address: 192.168.6.2
rabbitmq2:
image: rabbitmq:3.8.3-management
container_name: rabbitmq2
restart: always
hostname: rabbitmq2
ports:
- 15684:15672
- 5684:5672
volumes:
- ./data2:/var/lib/rabbitmq
- ./rabbitmq.sh:/home/rabbitmq.sh
- ./etc/hosts:/etc/hosts
environment:
- RABBITMQ_DEFAULT_USER=root
- RABBITMQ_DEFAULT_PASS=root
- RABBITMQ_ERLANG_COOKIE=CURIOAPPLICATION
networks:
rabbit:
ipv4_address: 192.168.6.3
rabbitmq3:
image: rabbitmq:3.8.3-management
container_name: rabbitmq3
restart: always
hostname: rabbitmq3
ports:
- 15692:15672
- 5692:5672
volumes:
- ./data3:/var/lib/rabbitmq
- ./rabbitmq.sh:/home/rabbitmq.sh
- ./etc/hosts:/etc/hosts
environment:
- RABBITMQ_DEFAULT_USER=root
- RABBITMQ_DEFAULT_PASS=root
- RABBITMQ_ERLANG_COOKIE=CURIOAPPLICATION
networks:
rabbit:
ipv4_address: 192.168.6.4
networks:
rabbit:
external:
name: rabbitmqnet
第二步:创建 rabbitmqnet 子网
docker network create --subnet 192.168.6.1/24 rabbitmqnet
第三步:创建 rabbitmq.sh 脚本
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster rabbitmq@rabbitmq1
rabbitmqctl start_app
第四步:配置docker-compose.yml文件同步目录下的 ./etc/hosts 文件(集群节点间需能互相访问,故每个集群节点的hosts文件应包含集群内所有节点的信息以保证互相解析)
192.168.6.2 rabbitmq1
192.168.6.3 rabbitmq2
192.168.6.4 rabbitmq3
第五步:docker-compose up -d 运行容器
第六步:docker exec -it rabbitmq1 bash 分别进入从节点容器(本文中从节点是 rabbitmq3,rabbitmq2),启动脚本
chmod +777 /home/rabbitmq.sh
./home/rabbitmq.sh
第七步:主从集群已经搭建好,查看集群状态 rabbitmqctl cluster_status
可以通过管控台来查看
第八步:设置策略
rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}'
至此镜像队列集群搭建完成
查看当前策略
rabbitmqctl list_policies
删除策略
rabbitmqctl clear_policy ha-all