🏷 基础环境
虚拟机:node1 / node2 / node3
集群:docker swarm,node1 为 manager 节点,node2 / node3 为 worker 节点
集群搭建可参考:Docker Swarm 入门:单机创建 Swarm 集群
🏷 集群部署方式一( rabbitmqctl
)
参考资料: Clustering Transcript with rabbitmqctl
1、创建 rabbitmq 持久化存储路径
# 【所有节点】
mkdir -p ~/rabbitmq/data
2、在 node1
上新建部署文件 ~/rabbitmq/stack.yml
version: '3.7'
services:
rabbit1:
image: rabbitmq:3
networks:
- rabbit_network
volumes:
- ~/rabbitmq/data:/var/lib/rabbitmq
environment:
- RABBITMQ_ERLANG_COOKIE=anoyi_cluster_cookie
- RABBITMQ_DEFAULT_USER=anoyi
- RABBITMQ_DEFAULT_PASS=Passw0rd
hostname: rabbit1
deploy:
replicas: 1
placement:
constraints:
- node.hostname==node1
rabbit2:
image: rabbitmq:3
networks:
- rabbit_network
volumes:
- ~/rabbitmq/data:/var/lib/rabbitmq
environment:
- RABBITMQ_ERLANG_COOKIE=anoyi_cluster_cookie
- RABBITMQ_DEFAULT_USER=anoyi
- RABBITMQ_DEFAULT_PASS=Passw0rd
hostname: rabbit2
deploy:
replicas: 1
placement:
constraints:
- node.hostname==node2
rabbit3:
image: rabbitmq:3
networks:
- rabbit_network
volumes:
- ~/rabbitmq/data:/var/lib/rabbitmq
environment:
- RABBITMQ_ERLANG_COOKIE=anoyi_cluster_cookie
- RABBITMQ_DEFAULT_USER=anoyi
- RABBITMQ_DEFAULT_PASS=Passw0rd
hostname: rabbit3
deploy:
replicas: 1
placement:
constraints:
- node.hostname==node3
networks:
rabbit_network:
driver: overlay
attachable: true
3、启动 rabbitmq 服务
# 【node1】部署 stack
docker stack deploy -c ~/rabbitmq/stack.yml rabbitmq
# 【node1】查看服务状态
docker service ls
直到出现如下列表:
ID NAME MODE REPLICAS IMAGE PORTS
j72ifjskqug5 rabbitmq_rabbit1 replicated 1/1 rabbitmq:3
ihs9ec74mtng rabbitmq_rabbit2 replicated 1/1 rabbitmq:3
sb996gg4jtl2 rabbitmq_rabbit3 replicated 1/1 rabbitmq:3
4、构建 rabbitmq 集群
将 rabbit@rabbit2
和 rabbit@rabbit3
加入集群:
#【node2】rabbit@rabbit2 加入集群
docker exec -it $(docker ps | grep "rabbit" | awk '{ print $1 }') rabbitmqctl stop_app
docker exec -it $(docker ps | grep "rabbit" | awk '{ print $1 }') rabbitmqctl reset
docker exec -it $(docker ps | grep "rabbit" | awk '{ print $1 }') rabbitmqctl join_cluster rabbit@rabbit1
docker exec -it $(docker ps | grep "rabbit" | awk '{ print $1 }') rabbitmqctl start_app
#【node3】rabbit@rabbit3 加入集群
docker exec -it $(docker ps | grep "rabbit" | awk '{ print $1 }') rabbitmqctl stop_app
docker exec -it $(docker ps | grep "rabbit" | awk '{ print $1 }') rabbitmqctl reset
docker exec -it $(docker ps | grep "rabbit" | awk '{ print $1 }') rabbitmqctl join_cluster rabbit@rabbit1
docker exec -it $(docker ps | grep "rabbit" | awk '{ print $1 }') rabbitmqctl start_app
5、查看集群状态
# 【任意节点】
docker exec -it $(docker ps | grep "rabbit" | awk '{ print $1 }') rabbitmqctl cluster_status
🏷 集群部署方式二( Config File Peer Discovery Backend
)
参考资料: How to Configure Peer Discovery
1、创建 rabbitmq 持久化存储路径
# 【所有节点】
mkdir -p ~/rabbitmq/data
2、在 node1
上创建 rabbitmq 配置文件 ~/rabbitmq/rabbitmq.conf
loopback_users.guest = false
listeners.tcp.default = 5672
hipe_compile = false
default_user = anoyi
default_pass = Passw0rd
cluster_formation.peer_discovery_backend = rabbit_peer_discovery_classic_config
cluster_formation.classic_config.nodes.1 = rabbit@rabbit1
cluster_formation.classic_config.nodes.2 = rabbit@rabbit2
cluster_formation.classic_config.nodes.3 = rabbit@rabbit3
3、在 node1
上创建 docker config
docker config create rabbitmq-config ~/rabbitmq/rabbitmq.conf
4、在 node1
上新建部署文件 ~/rabbitmq/stack.yml
version: '3.7'
services:
rabbit1:
image: rabbitmq:3
networks:
- rabbit_network
volumes:
- ~/rabbitmq/data:/var/lib/rabbitmq
configs:
- source: rabbitmq-config
target: /etc/rabbitmq/rabbitmq.conf
environment:
- RABBITMQ_ERLANG_COOKIE=anoyi_cluster_cookie
hostname: rabbit1
deploy:
replicas: 1
placement:
constraints:
- node.hostname==node1
rabbit2:
image: rabbitmq:3
networks:
- rabbit_network
volumes:
- ~/rabbitmq/data:/var/lib/rabbitmq
configs:
- source: rabbitmq-config
target: /etc/rabbitmq/rabbitmq.conf
environment:
- RABBITMQ_ERLANG_COOKIE=anoyi_cluster_cookie
hostname: rabbit2
deploy:
replicas: 1
placement:
constraints:
- node.hostname==node2
rabbit3:
image: rabbitmq:3
networks:
- rabbit_network
volumes:
- ~/rabbitmq/data:/var/lib/rabbitmq
configs:
- source: rabbitmq-config
target: /etc/rabbitmq/rabbitmq.conf
environment:
- RABBITMQ_ERLANG_COOKIE=anoyi_cluster_cookie
hostname: rabbit3
deploy:
replicas: 1
placement:
constraints:
- node.hostname==node3
networks:
rabbit_network:
driver: overlay
attachable: true
configs:
rabbitmq-config:
external: true
5、启动 rabbitmq 集群
# 【node1】部署集群
docker stack deploy -c ~/rabbitmq/stack.yml rabbitmq
🏷 番外补充
1、Spring Boot 应用连接 RabbitMQ 集群配置
spring.rabbitmq.addresses=node1:5672,node2:5672,node3:5672
spring.rabbitmq.username=anoyi
spring.rabbitmq.password=Passw0rd
注意,容器需要暴露端口 5672 供外部访问