服务器A的IP服务器B的IP
192.168.56.110192.168.56.111
  • 创建持久化文件夹
    1
    2
    3
    4
    #在192.168.56.110上执行命令
    mkdir -p /home/rabbitmq/cluster/rabbitmq01
    #在192.168.56.111上执行命令
    mkdir -p /home/rabbitmq/cluster/rabbitmq02
  • 启动RabbitMQ
    1
    2
    3
    4
    #在192.168.56.110上执行
    docker run -d --hostname rabbitmq01 --name rabbitmq01 --add-host=rabbitmq01:192.168.56.110 --add-host=rabbitmq02:192.168.56.111 -v /home/rabbitmq/cluster/rabbitmq01:/var/lib/rabbitmq --privileged=true -p 15672:15672 -p 5672:5672 -p 5671:5671 -p 4369:4369 -p 25672:25672 -e RABBITMQ_ERLANG_COOKIE='rabbitmqCookie' rabbitmq:3.8.14-management
    #在192.168.56.111上执行
    docker run -d --hostname rabbitmq02 --name rabbitmq02 --add-host=rabbitmq01:192.168.56.110 --add-host=rabbitmq02:192.168.56.111 -v /home/rabbitmq/cluster/rabbitmq02:/var/lib/rabbitmq --privileged=true -p 15672:15672 -p 5672:5672 -p 5671:5671 -p 4369:4369 -p 25672:25672 -e RABBITMQ_ERLANG_COOKIE='rabbitmqCookie' rabbitmq:3.8.14-management
  • 目前这两个节点没什么关系,接下来让他们成为一个集群
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    #在192.168.56.110上执行
    docker exec -it rabbitmq01 bash

    rabbitmqctl stop_app
    rabbitmqctl reset
    rabbitmqctl start_app
    exit

    #在192.168.56.111上执行
    docker exec -it rabbitmq02 bash

    rabbitmqctl stop_app
    rabbitmqctl reset
    #ram是内存节点根据需要选择,不加就是磁盘节点
    rabbitmqctl join_cluster --ram rabbit@rabbitmq01
    rabbitmqctl start_app
    exit
  • 目前为止,我们的集群为普通集群,要做成镜像集群的话
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    CMD
    docker exec -it rabbitmq01 bash
    rabbitmqctl set_policy -p / "ha" "^" '{"ha-mode":"all","ha-sync-mode":"automatic"}'
    ## 可用rabbitmqctl list_policies -p /; --查看vhost / 下的所有策略
    ## 在 cluster中任意节点启用策略,策略会自动同步到集群节点
    ## 策略模式all即复制到所有节点,包含新增节点,策略正则表达式为“^表示所有匹配所


    参数
    ha-mode:策略键
    1.all 队列镜像在群集中的所有节点上。当新节点添加到群集时,队列将镜像到该节点
    2.exactly 集群中的队列实例数。
    3.nodes 队列镜像到节点名称中列出的节点。

    ha-sync-mode:队列同步
    1.manual手动<默认模式>.新的队列镜像将不会收到现有的消息,它只会接收新的消息。
    2.automatic自动同步.当一个新镜像加入时,队列会自动同步。队列同步是一个阻塞操作。
1
界面操作

我们进入web界面是可以看到:

  • 负载均衡
    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
    在192.168.56.110上创建nginx的conf文件,/home/rabbitmq/cluster/nginx/nginx-rabbitmq01.conf

    user nginx;
    worker_processes 1;
    error_log /var/log/nginx/error.log warn;
    pid /var/run/nginx.pid;
    events {
    worker_connections 1024;
    }
    http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';
    access_log /var/log/nginx/access.log main;
    sendfile on;
    #tcp_nopush on;
    keepalive_timeout 65;
    #gzip on;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    client_max_body_size 10m;
    client_body_buffer_size 128k;
    proxy_connect_timeout 5s;
    proxy_send_timeout 5s;
    proxy_read_timeout 5s;
    proxy_buffer_size 4k;
    proxy_buffers 4 32k;
    proxy_busy_buffers_size 64k;
    proxy_temp_file_write_size 64k;
    #rabbitmq管理界面
    upstream rabbitManage {
    server 192.168.56.110:15672;
    server 192.168.56.111:15672;
    }
    server {
    listen 15672;
    server_name 192.168.56.110;
    location / {
    proxy_pass http://rabbitManage;
    index index.html index.htm;
    }

    }
    }
    # rabbitmq通信
    stream{
    upstream rabbitTcp{
    server 192.168.56.110:5672;
    server 192.168.56.111:5672;
    }
    server {
    listen 5673;
    proxy_pass rabbitTcp;
    }
    }
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
在192.168.56.111上创建nginx的conf文件,/home/rabbitmq/cluster/nginx/nginx-rabbitmq02.conf


user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 5s;
proxy_send_timeout 5s;
proxy_read_timeout 5s;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
#rabbitmq管理界面
upstream rabbitManage {
server 192.168.56.110:15672;
server 192.168.56.111:15672;
}
server {
listen 15672;
server_name 192.168.56.111;
location / {
proxy_pass http://rabbitManage;
index index.html index.htm;
}

}
}
# rabbitmq通信
stream{
upstream rabbitTcp{
server 192.168.56.110:5672;
server 192.168.56.111:5672;
}

server {
listen 5673;
proxy_pass rabbitTcp;
}
}
  • 最后启动nginx(nginx -s reload)
    1
    2
    3
    4
    #在192.168.56.110上执行
    docker run -it -d --name nginxRmq -v /home/rabbitmq/cluster/nginx/nginx-rabbitmq01.conf:/etc/nginx/nginx.conf --privileged --net=host nginx
    #在192.168.56.111上执行
    docker run -it -d --name nginxRmq -v /home/rabbitmq/cluster/nginx/nginx-rabbitmq02.conf:/etc/nginx/nginx.conf --privileged --net=host nginx