Docker:搭建RabbitMQ集群

时间:2020-12-09 07:09:29

RabbitMQ原理介绍(一)

RabbitMQ安装使用(二)

RabbitMQ添加新用户并支持远程访问(三)

RabbitMQ管理命令rabbitmqctl详解(四)

RabbitMQ两种集群模式配置管理(五)

RabbitMQ应用实例Python版-Hello World

RabbitMQ应用实例Python版-工作队列

RabbitMQ应用实例Python版-消息确认和消息持久化

RabbitMQ应用实例Python版-发布/订阅

RabbitMQ应用实例Python版-路由

Docker搭建RabbitMQ(单机版)

采用bijukunjummen提供的镜像。

 
1
$ git clone https://github.com/bijukunjummen/docker-rabbitmq-cluster.git

启动集群,别忘记安装docker daemon和docker-compose

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ cd docker-rabbitmq-cluster/cluster
$ docker-compose up -d
Pulling rabbit1 (bijukunjummen/rabbitmq-server:latest)...
latest: Pulling from bijukunjummen/rabbitmq-server
8d30e94188e7: Pull complete
4dcc5ee4075c: Pull complete
9554738b08fc: Pull complete
efdc65d45fa8: Pull complete
8511de892111: Pull complete
370e6c0ab215: Pull complete
940500e823fe: Pull complete
b6134aab1a2a: Pull complete
b870c2ab7ca0: Pull complete
56ed51d99ae0: Pull complete
adec15369e34: Pull complete
1f44e442c116: Pull complete
0dcd19841c9f: Pull complete
24b8906417b0: Pull complete
Digest: sha256:a6bbb268de87d09d538152450b77835a27693039ab079785a7b72f595bef18a6
Status: Downloaded newer image for bijukunjummen/rabbitmq-server:latest
Creating cluster_rabbit1_1
Creating cluster_rabbit2_1
Creating cluster_rabbit3_1

默认启动了三个节点

 
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
rabbit1:
  image: bijukunjummen/rabbitmq-server
  hostname: rabbit1
  ports:
    - "5672:5672"
    - "15672:15672"
 
rabbit2:
  image: bijukunjummen/rabbitmq-server
  hostname: rabbit2
  links:
    - rabbit1
  environment:
   - CLUSTERED=true
   - CLUSTER_WITH=rabbit1
   - RAM_NODE=true
  ports:
      - "5673:5672"
      - "15673:15672"
 
rabbit3:
  image: bijukunjummen/rabbitmq-server
  hostname: rabbit3
  links:
    - rabbit1
    - rabbit2
  environment:
   - CLUSTERED=true
   - CLUSTER_WITH=rabbit1
  ports:
        - "5674:5672"

查看容器

 
1
2
3
4
5
$ docker ps
CONTAINER ID        IMAGE                           COMMAND                  CREATED             STATUS              PORTS                                                                                  NAMES
ba5f665bb213        bijukunjummen/rabbitmq-server   "/bin/sh -c /opt/rabb"   10 minutes ago      Up 10 minutes       4369/tcp, 9100-9105/tcp, 15672/tcp, 25672/tcp, 0.0.0.0:5674->5672/tcp                  cluster_rabbit3_1
b9466e206b2b        bijukunjummen/rabbitmq-server   "/bin/sh -c /opt/rabb"   10 minutes ago      Up 10 minutes       4369/tcp, 9100-9105/tcp, 25672/tcp, 0.0.0.0:5673->5672/tcp, 0.0.0.0:15673->15672/tcp   cluster_rabbit2_1
b733201aeadf        bijukunjummen/rabbitmq-server   "/bin/sh -c /opt/rabb"   10 minutes ago      Up 10 minutes       4369/tcp, 0.0.0.0:5672->5672/tcp, 9100-9105/tcp, 0.0.0.0:15672->15672/tcp, 25672/tcp   cluster_rabbit1_1                                                                                            daomonit

访问
http://192.168.99.100:15672,弹出登陆界面。输入guest/guest

Docker:搭建RabbitMQ集群

然后就可以添加用户了。

Docker搭建RabbitMQ集群(多机版)

上面我们使用Docker搭建出了一个RabbitMQ单机集群,这种模式只能用来测试玩玩,无法再生产环境中使用。在生产环境中,需要把3个节点的集群分布到各个主机上面去。这个时候docker-compose就需要做调整了,主要是对外端口和link方式,首先我们说一下RabbitMQ开启后的对外端口。

SELinux和类似机制或许会通过绑定端口的方式阻止RabbitMQ。当这种情况发生时,RabbitMQ会启动失败。请确认以下的端口是可以被打开的:

4369 (epmd)

25672 (Erlang distribution)

5672, 5671 (启用了或者未启用TLS的AMQP 0-9-1)

15672 (如果管理插件被启用)

61613, 61614 (如果STOMP被启用)

1883, 8883 (如果MQTT被启用)

说完端口,下面提供一个三节点的docker-compose文件。

rabbit1(10.106.136.7)

 
1
2
3
4
5
6
7
8
9
10
11
12
rabbit1:
  image: bijukunjummen/rabbitmq-server
  hostname: rabbit1
  ports:
    - "5672:5672"
    - "4369:4369"
    - "1883:1883"
    - "15672:15672"
    - "25672:25672"
  environment:
    - RABBITMQ_DEFAULT_USER=myuser
    - RABBITMQ_DEFAULT_PASS=mypass

rabbit2(10.106.136.8)

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
rabbit2:
  image: bijukunjummen/rabbitmq-server
  hostname: rabbit2
  extra_hosts:
    - "rabbit1:10.106.136.7"
  environment:
   - CLUSTERED=true
   - CLUSTER_WITH=rabbit1
   - RAM_NODE=true
  ports:
      - "5672:5672"
      - "4369:4369"
      - "1883:1883"
      - "15672:15672"
      - "25672:25672"

rabbit3(10.106.136.9)

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
rabbit3:
  image: bijukunjummen/rabbitmq-server
  hostname: rabbit3
  extra_hosts:
    - "rabbit1:10.106.136.7"
    - "rabbit2:10.106.136.8"
  environment:
   - CLUSTERED=true
   - CLUSTER_WITH=rabbit1
   - RAM_NODE=true
  ports:
        - "5672:5672"
        - "4369:4369"
        - "1883:1883"
        - "15672:15672"
        - "25672:25672"

然后依序在三个节点上分别开启rabbitmq,然后随意找个节点打开IP:25672,输入guest用户名和guest密码就可以查看集群了。