RabbitMQ集群部署

本文使用单机部署rabbitmq、haproxy。rabbitmq集群开启镜像队列,haproxy实现负载均衡。

为了快速部署,使用镜像体积较小的alpine版。

准备

docker-compose.yaml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
version: "3.8"

x-services:
  default: &default
    networks:
      default-network:
    logging:
      options:
        max-size: "10m"
        max-file: "3"
    restart: always

networks:
  default-network:
    driver: bridge

services:
  rabbitmq1:
    <<: *default
    container_name: rabbitmq1
    hostname: my-rabbitmq1
    ports:
      - "5672:5672"
      - "15672:15672"
    environment:
      - RABBITMQ_ERLANG_COOKIE="MyCookie"
    volumes:
      - /etc/localtime:/etc/localtime:ro
    image: rabbitmq:3.9.20-management-alpine

  rabbitmq2:
    <<: *default
    container_name: rabbitmq2
    hostname: my-rabbitmq2
    ports:
      - "5673:5672"
      - "15673:15672"
    environment:
      - RABBITMQ_ERLANG_COOKIE="MyCookie"
    volumes:
      - /etc/localtime:/etc/localtime:ro
    image: rabbitmq:3.9.20-management-alpine

  rabbitmq3:
    <<: *default
    container_name: rabbitmq3
    hostname: my-rabbitmq3
    ports:
      - "5674:5672"
      - "15674:15672"
    environment:
      - RABBITMQ_ERLANG_COOKIE="MyCookie"
    volumes:
      - /etc/localtime:/etc/localtime:ro
    image: rabbitmq:3.9.20-management-alpine

  haproxy:
    <<: *default
    container_name: haproxy
    network_mode: host
    volumes:
      - ./haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg
      - /etc/localtime:/etc/localtime:ro
    image: haproxy:2.6.1-alpine

haproxy.cfg

global
  log 127.0.0.1 local0 info
  maxconn 10240
  daemon

defaults
  log global
  mode http
  timeout connect 5000
  timeout client 5000
  timeout server 5000
  timeout check 2000

listen admin_stats
    bind                *:9090
    mode                http
    stats               show-node haproxy
    stats               refresh 5s
    stats auth          admin:admin
    stats uri           /stats
    stats admin if TRUE

listen haproxy
  bind *:35672
  option tcplog
  mode tcp
  balance roundrobin
  server rabbit1 127.0.0.1:5672 check inter 5000 rise 2 fall 2
  server rabbit2 127.0.0.1:5673 check inter 5000 rise 2 fall 2
  server rabbit3 127.0.0.1:5674 check inter 5000 rise 2 fall 2

部署

启动容器

1
docker-compose up -d

rabbitmq集群配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# 进入rabbitmq1容器
docker exec -it rabbitmq1 sh
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl start_app
exit
# 进入rabbitmq2容器
docker exec -it rabbitmq2 sh
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster --ram rabbitmq1@my-rabbitmq1
rabbitmqctl start_app
exit
# 进入rabbitmq3容器
docker exec -it rabbitmq3 sh
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster --ram rabbitmq1@my-rabbitmq1
rabbitmqctl start_app
exit

rabbitmq集群开启镜像队列

1
2
# 进入任意rabbitmq容器操作
rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}'

查看集群状态

1
2
# 进入任意rabbitmq容器操作
rabbitmqctl cluster_status

haproxy 配置
rabbit镜像 https://registry.hub.docker.com/_/rabbitmq
haproxy镜像 https://registry.hub.docker.com/_/haproxy

updatedupdated2024-10-282024-10-28