Docker运行RabbitMQ并使用SpringAMQP操作

文章目录

一、RabbitMQ运行

拉取docker镜像

bash 复制代码
docker pull rabbitmq:3-management

基础运行命令

bash 复制代码
docker run \
	-e RABBITMQ_DEFAULT_USER=rabbitmq \
	-e RABBITMQ_DEFAULT_PASS=rabbitmq \
	--name rabbitmq \
	-p 15672:15672 \
	-p 5672:5672 \
	-d \
	rabbitmq:3-management

15672是网页后台管理系统,5672是给服务用的。

官方入门教程可以看这里RabbitMQ Tutorials --- RabbitMQ

二、整合SpringAMQP

1. 引入依赖

xml 复制代码
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
yml 复制代码
spring:
  rabbitmq:
    host: localhost # rabbitMQ的ip地址
    port: 5672 # rabbitMQ服务端口
    username: rabbitmq
    password: rabbitmq

三、测试

这边采用常用的消费者-生产者模型,使用默认的Direct类型exchange。不懂的可以先继续学习rabbitmq再来实践。

1. 消费者

在消费者服务随便新建一个listener

java 复制代码
@Slf4j
@Component
public class SpringRabbitListener {

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "simple.queue"),
            exchange = @Exchange(name = "simple.exchange"),
            key = "simple"
    ))
    public void listenSimpleQueue(String msg) {
        log.info("消费者接收到simple.queue的消息:【" + msg + "】");
    }
}

2. 生产者

在生产者服务的Test模块新建一个测试

java 复制代码
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringAmqpTest {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    public void testSendMessage2SimpleQueue() throws InterruptedException {
        String message = "hello, spring amqp!";
        rabbitTemplate.convertAndSend("simple.exchange", "simple", message);
    }
}

3. 运行

先启动消费者。

登录http://localhost:15672/,可以看到simple.exchangesimple.queue已被创建。

然后启动测试testSendMessage2SimpleQueue,出现类似以下日志,消息发送成功。

txt 复制代码
12:17:43:771  INFO 21064 --- [           main] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: [localhost:5672]
12:17:43:808  INFO 21064 --- [           main] o.s.a.r.c.CachingConnectionFactory       : Created new connection: rabbitConnectionFactory#50f40653:0/SimpleConnection@536d97f8 [delegate=amqp://[email protected]:5672/, localPort= 59641]

消费者出现类似以下日志,收到消息。

txt 复制代码
12:17:31:074  INFO 8924 --- [           main] o.s.a.r.c.CachingConnectionFactory       : Created new connection: rabbitConnectionFactory#2a27cb34:0/SimpleConnection@671facee [delegate=amqp://[email protected]:5672/, localPort= 59634]
12:17:31:141  INFO 8924 --- [           main] cn.itcast.mq.ConsumerApplication         : Started ConsumerApplication in 1.011 seconds (JVM running for 1.462)
12:17:43:848  INFO 8924 --- [ntContainer#0-1] c.i.mq.listener.SpringRabbitListener     : 消费者接收到simple.queue的消息:【hello, spring amqp!】
相关推荐
SunTecTec3 小时前
Flink Docker Application Mode 命令解析 - 修改命令以启用 Web UI
大数据·前端·docker·flink
好记忆不如烂笔头abc3 小时前
HTTPSConnectionPool(host=‘files.pythonhosted.org‘, port=443): Read timed out.
docker
内网渗透5 小时前
OpenWrt 与 Docker:打造轻量级容器化应用平台技术分享
linux·docker·容器·openwrt·软路由
qq_339282236 小时前
docker打开滚动日志
运维·docker·容器
Hoking6 小时前
SpringBoot应用原生或docker镜像容器集成Skywalking
docker·容器·skywalking
Kendra9197 小时前
Docker 容器虚拟化技术和自动化部署
docker·容器·自动化
ITCharge8 小时前
Docker 万字教程:从入门到掌握
后端·docker·容器
智想天开8 小时前
13.组合模式:思考与解读
docker·设计模式·容器·组合模式
马大胡子9 小时前
Greenbone(绿骨)开源GVM容器docker部署和汉化介绍
linux·网络安全·docker
AdaTina10 小时前
Docker的分解分析
运维·docker·容器