从本章开始,我们一起在Docker环境实战RabbitMQ环境部署和对应的Java开发,当前是《Docker下RabbitMQ四部曲》系列的第一篇,整个系列由以下四篇文章组成:
- 第一篇,即本章,我们用最快的方式体验RabbitMQ单机环境下生产和消费消息,然后再体验RabbitMQ集群环境下生产和消费消息;
- 第二篇,详细说明如何制作RabbitMQ的docker镜像文件,这个镜像支持集群,也能通过参数来配置内存节点、高可用等特性;
- 第三篇,开发基于SpringBoot的web应用,实现生产和消息RabbitMQ消息的功能;
- 第四篇,实战高可用,集群环境中有RabbitMQ宕机,验证此时消息的生产和消费是否正常;
原文地址:https://blog.csdn.net/boling_cavalry/article/details/80212878
环境信息
本次实战的环境信息如下:
- 操作系统: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文件,内容如下:
rabbitmq:
image: bolingcavalry/rabbitmq-server:0.0.3
hostname: rabbitmq
ports:
- "15672:15672"
environment:
- RABBITMQ_DEFAULT_USER=admin
- RABBITMQ_DEFAULT_PASS=888888
producer:
image: bolingcavalry/rabbitmqproducer:0.0.2-SNAPSHOT
hostname: producer
links:
- rabbitmq:rabbitmqhost
ports:
- "18080:8080"
environment:
- mq.rabbit.address=rabbitmqhost:5672
- mq.rabbit.username=admin
- mq.rabbit.password=888888
consumer:
image: bolingcavalry/rabbitmqconsumer:0.0.5-SNAPSHOT
hostname: consumer
links:
- rabbitmq:rabbitmqhost
environment:
- mq.rabbit.address=rabbitmqhost:5672
- mq.rabbit.username=admin
- mq.rabbit.password=888888
- mq.rabbit.queue.name=consumer.queue
- 在docker-compose.yml文件所在目录执行命令docker-compose up -d,即可下载镜像并创建容器,下载过程比较耗时请耐心等待,容器一共三个:RabbitMQ、消息生产者、消息消费者;
- 容器创建成功后,网络架构如下图所示:
- 为了方便后续体验,将三个容器信息列表如下:
名称 | 作用 | IP地址 |
---|---|---|
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收到的消息:
2018-05-12 10:40:23.851 INFO 1 --- [cTaskExecutor-2] o.s.a.r.c.CachingConnectionFactory : Attempting to connect to: [rabbitmqhost:5672]
2018-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)
2018-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
2018-05-12 10:40:28.892 INFO 1 --- [cTaskExecutor-3] o.s.a.r.c.CachingConnectionFactory : Attempting to connect to: [rabbitmqhost:5672]
2018-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]
2018-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
- 在浏览器访问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,即可将刚才创建的三个容器彻底清除,如下所示:
代码语言:javascript复制root@maven:/usr/local/work/github/blog_demos/rabbitmq_docker_files/single# docker-compose down
Stopping single_producer_1 ... done
Stopping single_consumer_1 ... done
Stopping single_rabbitmq_1 ... done
Removing single_producer_1 ... done
Removing single_consumer_1 ... done
Removing single_rabbitmq_1 ... done
部署集群版RabbitMQ
- 创建集群版对应的docker-compose.yml文件,内容如下:
version: '2'
services:
rabbit1:
image: bolingcavalry/rabbitmq-server:0.0.3
hostname: rabbit1
ports:
- "15672:15672"
environment:
- RABBITMQ_DEFAULT_USER=admin
- RABBITMQ_DEFAULT_PASS=888888
rabbit2:
image: bolingcavalry/rabbitmq-server:0.0.3
hostname: rabbit2
depends_on:
- rabbit1
links:
- rabbit1
environment:
- CLUSTERED=true
- CLUSTER_WITH=rabbit1
- RAM_NODE=true
ports:
- "15673:15672"
rabbit3:
image: bolingcavalry/rabbitmq-server:0.0.3
hostname: rabbit3
depends_on:
- rabbit2
links:
- rabbit1
- rabbit2
environment:
- CLUSTERED=true
- CLUSTER_WITH=rabbit1
ports:
- "15675:15672"
producer:
image: bolingcavalry/rabbitmqproducer:0.0.2-SNAPSHOT
hostname: producer
depends_on:
- rabbit3
links:
- rabbit1:rabbitmqhost
ports:
- "18080:8080"
environment:
- mq.rabbit.address=rabbitmqhost:5672
- mq.rabbit.username=admin
- mq.rabbit.password=888888
consumer1:
image: bolingcavalry/rabbitmqconsumer:0.0.5-SNAPSHOT
hostname: consumer1
depends_on:
- producer
links:
- rabbit2:rabbitmqhost
environment:
- mq.rabbit.address=rabbitmqhost:5672
- mq.rabbit.username=admin
- mq.rabbit.password=888888
- mq.rabbit.queue.name=consumer1.queue
consumer2:
image: bolingcavalry/rabbitmqconsumer:0.0.5-SNAPSHOT
hostname: consumer2
depends_on:
- consumer1
links:
- rabbit3:rabbitmqhost
environment:
- mq.rabbit.address=rabbitmqhost:5672
- mq.rabbit.username=admin
- mq.rabbit.password=888888
- mq.rabbit.queue.name=consumer2.queue
- 在docker-compose.yml文件所在目录执行命令docker-compose up -d,即可创建容器,容器一共六个:三个RabbitMQ、一个消息生产者、两个消息消费者;
- 容器创建成功后,网络架构如下图所示:
- 为了方便后续体验,将三个容器信息列表如下:
名称 | 作用 | IP地址 |
---|---|---|
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收到的消息:
2018-05-12 11:59:17.112 INFO 1 --- [cTaskExecutor-4] o.s.a.r.c.CachingConnectionFactory : Attempting to connect to: [rabbitmqhost:5672]
2018-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]
2018-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
- 接下来看另一个消费消息的容器,在docker机器上输入命令docker logs -f cluster_consumer2_1,可以看到容器cluster_consumer2_1的日志,如下所示,也受到一条消息,这证明fanout模式的交换机中的消息已成功被两个消费者消费:
2018-05-12 11:59:12.199 INFO 1 --- [cTaskExecutor-3] o.s.a.r.c.CachingConnectionFactory : Attempting to connect to: [rabbitmqhost:5672]
2018-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]
2018-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
- 在浏览器访问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我们就体验完毕了,本章旨在让读者您用最少的操作来完成整个体验过程,接下来的文章中,我们会一起实战这些简单操作背后的技术细节;