从本章开始,我们一起在Docker环境实战RabbitMQ环境部署和对应的Java开发,当前是《Docker下RabbitMQ四部曲》系列的第一篇,整个系列由以下四篇文章组成:
- 第一篇,即本章,我们用最快的方式体验RabbitMQ单机环境下生产和消费消息,然后再体验RabbitMQ集群环境下生产和消费消息;
- 第二篇,详细说明如何制作RabbitMQ的docker镜像文件,这个镜像支持集群,也能通过参数来配置内存节点、高可用等特性;
- 第三篇,开发基于SpringBoot的web应用,实现生产和消息RabbitMQ消息的功能;
- 第四篇,实战高可用,集群环境中有RabbitMQ宕机,验证此时消息的生产和消费是否正常;
环境信息
本次实战的环境信息如下:
- 操作系统:Ubuntu 16.04.3 LTS
- docker版本:17.03.2-ce, build f5ec1e2
- docker-compose版本:1.18.0, build 8dd22a9
- RabbitMQ版本:3.7.5-rc.1
- RabbitMQ环境的Erlang版本:19.3.6.5
本章概要
本章的实战步骤简述如下:
- 部署单机版RabbitMQ、生产消息的web应用、消费消息的web应用;
- 检查RabbitMQ的基本情况、生产和消费消息;
- 部署集群版RabbitMQ、生产消息的web应用、消费消息的web应用;
- 检查RabbitMQ的基本情况、生产和消费消息;
部署单机版RabbitMQ
-
创建docker-compose.yml文件,内容如下:
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 1rabbitmq:
2 image: bolingcavalry/rabbitmq-server:0.0.3
3 hostname: rabbitmq
4 ports:
5 - "15672:15672"
6 environment:
7 - RABBITMQ_DEFAULT_USER=admin
8 - RABBITMQ_DEFAULT_PASS=888888
9producer:
10 image: bolingcavalry/rabbitmqproducer:0.0.2-SNAPSHOT
11 hostname: producer
12 links:
13 - rabbitmq:rabbitmqhost
14 ports:
15 - "18080:8080"
16 environment:
17 - mq.rabbit.address=rabbitmqhost:5672
18 - mq.rabbit.username=admin
19 - mq.rabbit.password=888888
20consumer:
21 image: bolingcavalry/rabbitmqconsumer:0.0.5-SNAPSHOT
22 hostname: consumer
23 links:
24 - rabbitmq:rabbitmqhost
25 environment:
26 - mq.rabbit.address=rabbitmqhost:5672
27 - mq.rabbit.username=admin
28 - mq.rabbit.password=888888
29 - mq.rabbit.queue.name=consumer.queue
30
31
- 在docker-compose.yml文件所在目录执行命令
docker-compose up -d,即可下载镜像并创建容器,下载过程比较耗时请耐心等待,容器一共三个:RabbitMQ、消息生产者、消息消费者;
-
容器创建成功后,网络架构如下图所示:
-
为了方便后续体验,将三个容器信息列表如下:
single_rabbitmq_1
RabbitMQ服务器
172.17.0.2
single_producer_1
生产消息的web应用
172.17.0.4
single_consumer_1
消费消息的web应用
172.17.0.3
体验单机版RabbitMQ
单机版的RabbitMQ环境已经部署完毕,接下来我们来体验一番:
- 假设运行docker的电脑IP地址为
192.168.119.155;
- 在浏览器输入
http://192.168.119.155:18080/send/aaa/bbb,这个请求会使single_producer_1容器向RabbitMQ发送消息,如下图:
- 在docker机器上输入命令
docker logs -f single_consumer_1,可以看到容器的日志,如下所示,
receive message : hello, aaa, bbb就是容器从RabbitMQ收到的消息:
1
2
3
4
5
6
7
8 12018-05-12 10:40:23.851 INFO 1 --- [cTaskExecutor-2] o.s.a.r.c.CachingConnectionFactory : Attempting to connect to: [rabbitmqhost:5672]
22018-05-12 10:40:23.878 WARN 1 --- [172.17.0.2:5672] c.r.c.impl.ForgivingExceptionHandler : An unexpected connection driver error occured (Exception message: Socket closed)
32018-05-12 10:40:28.890 INFO 1 --- [cTaskExecutor-2] o.s.a.r.l.SimpleMessageListenerContainer : Restarting Consumer@25023b5b: tags=[{}], channel=null, acknowledgeMode=AUTO local queue size=0
42018-05-12 10:40:28.892 INFO 1 --- [cTaskExecutor-3] o.s.a.r.c.CachingConnectionFactory : Attempting to connect to: [rabbitmqhost:5672]
52018-05-12 10:40:28.900 INFO 1 --- [cTaskExecutor-3] o.s.a.r.c.CachingConnectionFactory : Created new connection: connectionFactory#29f69090:4/SimpleConnection@1fff76dd [delegate=amqp://admin@172.17.0.2:5672/, localPort= 36948]
62018-05-12 11:40:57.048 INFO 1 --- [cTaskExecutor-3] c.b.r.receiver.FanoutReceiver : receive message : hello, aaa, bbb, 2018-05-12 11:40:56
7
8
- 在浏览器访问
http://192.168.119.155:15672,会打开RabbitMQ的管理页面,需要登录,如下图,输入用户名
admin,密码
888888:
-
登录成功后,点击“Connections”的Tab页,如下图,目前single_producer_1和single_consumer_1容器都连接到了RabbitMQ:
-
如下图红框所示,在“Exchanges”的Tab页,可以看到一个fanout型的交换机,名为
bolingcavalryFanoutExchange:
- 如下图红框所示,在“Queues”的Tab页,可以看到一个队列,名为
consumer.queue:
至此,单机版的RabbitMQ我们就体验完毕了,接下来开始体验集群版的RabbitMQ;
清空单机版RabbitMQ的所有容器
在刚才创建的docker-compose.yml文件所在目录下执行命令
docker-compose down,即可将刚才创建的三个容器彻底清除,如下所示:
1
2
3
4
5
6
7
8
9 1root@maven:/usr/local/work/github/blog_demos/rabbitmq_docker_files/single# docker-compose down
2Stopping single_producer_1 ... done
3Stopping single_consumer_1 ... done
4Stopping single_rabbitmq_1 ... done
5Removing single_producer_1 ... done
6Removing single_consumer_1 ... done
7Removing single_rabbitmq_1 ... done
8
9
部署集群版RabbitMQ
-
创建集群版对应的docker-compose.yml文件,内容如下:
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
65
66
67
68
69
70
71
72
73
74
75 1version: '2'
2services:
3 rabbit1:
4 image: bolingcavalry/rabbitmq-server:0.0.3
5 hostname: rabbit1
6 ports:
7 - "15672:15672"
8 environment:
9 - RABBITMQ_DEFAULT_USER=admin
10 - RABBITMQ_DEFAULT_PASS=888888
11 rabbit2:
12 image: bolingcavalry/rabbitmq-server:0.0.3
13 hostname: rabbit2
14 depends_on:
15 - rabbit1
16 links:
17 - rabbit1
18 environment:
19 - CLUSTERED=true
20 - CLUSTER_WITH=rabbit1
21 - RAM_NODE=true
22 ports:
23 - "15673:15672"
24 rabbit3:
25 image: bolingcavalry/rabbitmq-server:0.0.3
26 hostname: rabbit3
27 depends_on:
28 - rabbit2
29 links:
30 - rabbit1
31 - rabbit2
32 environment:
33 - CLUSTERED=true
34 - CLUSTER_WITH=rabbit1
35 ports:
36 - "15675:15672"
37 producer:
38 image: bolingcavalry/rabbitmqproducer:0.0.2-SNAPSHOT
39 hostname: producer
40 depends_on:
41 - rabbit3
42 links:
43 - rabbit1:rabbitmqhost
44 ports:
45 - "18080:8080"
46 environment:
47 - mq.rabbit.address=rabbitmqhost:5672
48 - mq.rabbit.username=admin
49 - mq.rabbit.password=888888
50 consumer1:
51 image: bolingcavalry/rabbitmqconsumer:0.0.5-SNAPSHOT
52 hostname: consumer1
53 depends_on:
54 - producer
55 links:
56 - rabbit2:rabbitmqhost
57 environment:
58 - mq.rabbit.address=rabbitmqhost:5672
59 - mq.rabbit.username=admin
60 - mq.rabbit.password=888888
61 - mq.rabbit.queue.name=consumer1.queue
62 consumer2:
63 image: bolingcavalry/rabbitmqconsumer:0.0.5-SNAPSHOT
64 hostname: consumer2
65 depends_on:
66 - consumer1
67 links:
68 - rabbit3:rabbitmqhost
69 environment:
70 - mq.rabbit.address=rabbitmqhost:5672
71 - mq.rabbit.username=admin
72 - mq.rabbit.password=888888
73 - mq.rabbit.queue.name=consumer2.queue
74
75
- 在docker-compose.yml文件所在目录执行命令
docker-compose up -d,即可创建容器,容器一共六个:三个RabbitMQ、一个消息生产者、两个消息消费者;
-
容器创建成功后,网络架构如下图所示:
-
为了方便后续体验,将三个容器信息列表如下:
cluster_rabbit1_1
一号RabbitMQ服务器
172.17.0.2
cluster_rabbit2_1
二号RabbitMQ服务器
172.17.0.3
cluster_rabbit3_1
三号RabbitMQ服务器
172.17.0.6
cluster_producer_1
生产消息的web应用
172.17.0.4
cluster_consumer1_1
一号消费消息的web应用
172.17.0.5
cluster_consumer2_1
二号消费消息的web应用
172.17.0.7
体验集群版RabbitMQ
集群版的RabbitMQ环境已经部署完毕,我们来体验一番:
- 假设运行docker的电脑IP地址为
192.168.119.155;
- 在浏览器输入
http://192.168.119.155:18080/send/aaa/bbb,这个请求会使single_producer_1容器向RabbitMQ发送消息,如下图:
- 在docker机器上输入命令
docker logs -f cluster_consumer1_1,可以看到容器cluster_consumer1_1的日志,如下所示,
receive message : hello, aaa, bbb就是容器从RabbitMQ收到的消息:
1
2
3
4
5 12018-05-12 11:59:17.112 INFO 1 --- [cTaskExecutor-4] o.s.a.r.c.CachingConnectionFactory : Attempting to connect to: [rabbitmqhost:5672]
22018-05-12 11:59:17.172 INFO 1 --- [cTaskExecutor-4] o.s.a.r.c.CachingConnectionFactory : Created new connection: connectionFactory#55040f2f:6/SimpleConnection@723a07 [delegate=amqp://admin@172.17.0.3:5672/, localPort= 48294]
32018-05-12 12:11:12.187 INFO 1 --- [cTaskExecutor-4] c.b.r.receiver.FanoutReceiver : receive message : hello, aaa, bbb, 2018-05-12 12:11:12
4
5
- 接下来看另一个消费消息的容器,在docker机器上输入命令
docker logs -f cluster_consumer2_1,可以看到容器cluster_consumer2_1的日志,如下所示,也受到一条消息,这证明fanout模式的交换机中的消息已成功被两个消费者消费:
1
2
3
4
5 12018-05-12 11:59:12.199 INFO 1 --- [cTaskExecutor-3] o.s.a.r.c.CachingConnectionFactory : Attempting to connect to: [rabbitmqhost:5672]
22018-05-12 11:59:12.263 INFO 1 --- [cTaskExecutor-3] o.s.a.r.c.CachingConnectionFactory : Created new connection: connectionFactory#568bf312:4/SimpleConnection@11a59930 [delegate=amqp://admin@172.17.0.6:5672/, localPort= 55972]
32018-05-12 12:11:12.187 INFO 1 --- [cTaskExecutor-3] c.b.r.receiver.FanoutReceiver : receive message : hello, aaa, bbb, 2018-05-12 12:11:12
4
5
- 在浏览器访问
http://192.168.119.155:15672,会打开RabbitMQ的管理页面,需要登录,如下图,输入用户名
admin,密码
888888:
-
登录成功后,在“Overview”的Tab页,如下图红框所示,可见有三个RabbitMQ server,注意绿框中的信息,一号和三号RabbitMQ都是磁盘型,二号RabbitMQ是内存型的:
-
点击“Connections”的Tab页,如下图,所有生产者和消费者容器都连接到了RabbitMQ:
上图中表格表明了以下连接关系:
生产者连接一号RabbitMQ、一号消费者连接二号RabbitMQ、二号消费者连接三号RabbitMQ;
- 如下图红框所示,在“Exchanges”的Tab页,可以看到一个fanout型的交换机,名为
bolingcavalryFanoutExchange:
- 如下图红框所示,在“Queues”的Tab页,可以看到两个队列,分别存在于二号RabbitMQ和三号RabbitMQ:
至此,集群版的RabbitMQ我们就体验完毕了,本章旨在让读者您用最少的操作来完成整个体验过程,接下来的文章中,我们会一起实战这些简单操作背后的技术细节;