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://rabbitmq@127.0.0.1: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://rabbitmq@127.0.0.1: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!】
相关推荐
Bug退退退1238 小时前
RabbitMQ 高级特性之死信队列
java·分布式·spring·rabbitmq
cui_hao_nan9 小时前
Docker后端部署
运维·docker·容器
大苏打seven10 小时前
Docker学习笔记:Docker网络
笔记·学习·docker
小张是铁粉10 小时前
docker在Linux的安装遇到的问题
linux·docker·容器
没有名字的小羊12 小时前
8.Docker镜像讲解
运维·docker·容器·tomcat
企鹅侠客14 小时前
实践篇:14-构建 Node.js 应用程序镜像
docker·node.js·dockerfile
做一个AC梦15 小时前
Docker安装失败:Docker Desktop installation failed
运维·docker·容器
Shan120515 小时前
浅谈Docker Kicks in的应用
运维·docker·容器
Li&&Tao15 小时前
docker 常用命令
docker·容器·eureka
Jiude17 小时前
MinIO 社区版被故意阉割,Web管理功能全面移除。我来试试国产RustFS
后端·docker·架构