Spring Boot 简单使用 RabbitMQ 消息队列

SpringBootRabbitMQ配置与消息处理示例

在分布式系统中,消息队列(MQ)是一个非常重要的工具,常用于解耦、异步处理等。本文将展示如何在 Spring Boot 项目中集成和使用 RabbitMQ,简洁明了地演示消息的发送、接收以及监听。


1. 配置 RabbitMQ 📦

首先,我们需要创建一个配置类来配置 RabbitMQ。通过 enable 变量判断是否启用 RabbitMQ 配置。

package com.lili.office.config;

import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Configuration
public class RabbitmqConfig {

    @Value("${spring.rabbitmq.host:}")
    private String rabbitmqHost;

    public static boolean enable = false;

    @PostConstruct
    public void init() {
        enable = StringUtils.isNotEmpty(rabbitmqHost);
    }

    public final static String QUEUE_NAME = "ly-jamiechyi_test";

    @Bean
    public Queue queue() {
        return new Queue(QUEUE_NAME);
    }
}

解释:

  • @Configuration 注解标记该类为配置类。

  • 使用 @Value 获取配置文件中的 RabbitMQ 主机地址。

  • @PostConstruct 方法在容器初始化后执行,用来判断是否启用 RabbitMQ 配置。

2. 添加 RabbitMQ 依赖 ⚙️

pom.xml 中添加 RabbitMQ 相关的依赖:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

3. 配置 RabbitMQ 参数 🔑

application.yml 文件中配置 RabbitMQ 的连接信息:

  rabbitmq:
    host: 101.48.100.70
    port: 5672
    username: admin
    password: admin
    listener:
      simple:
        acknowledge-mode: none
    virtual-host: /  

解释:

  • 配置 RabbitMQ 的主机、端口、用户名、密码等。

  • 设置消息确认模式为 none,即手动确认模式。

4. 发送消息 🚀

接下来,我们使用 AmqpTemplate 来发送消息:

    @Autowired
    private AmqpTemplate rabbitTemplate;
    @GetMapping("/test1")
    public void test1(){
        if (RabbitmqConfig.enable){
            // 发送一条消息
            rabbitTemplate.convertAndSend(RabbitmqConfig.QUEUE_NAME,"2");
        }
    }

解释:

  • convertAndSend 方法用于发送消息,参数包括队列名和消息内容。

  • 通过 RabbitmqConfig.enable 判断是否启用 RabbitMQ 配置。


5. 主动消费消息 🕹️

如果需要主动消费消息,可以使用 receiveAndConvert 方法:

    @GetMapping("/test2")
    public void test2(){
        if (RabbitmqConfig.enable){
            // 接收一条消息
            Object o = rabbitTemplate.receiveAndConvert(RabbitmqConfig.QUEUE_NAME);
            System.out.println(o);
            // 其他逻辑
        }
    }

解释:

  • receiveAndConvert 方法接收消息并进行类型转换。

  • 适合用于需要主动获取消息的场景。


6. 使用监听器自动消费消息 🔔

除了主动消费消息外,我们还可以使用 @RabbitListener 注解来监听消息队列,实现自动消费消息:

@Component
@ConditionalOnProperty(name = "spring.rabbitmq.host")
public class MessageConsumer {

    @RabbitListener(queues = RabbitmqConfig.QUEUE_NAME)
    public void receiveMessage(String message) {
        System.out.println("Received message: " + message);
        // 在这里编写处理消息的逻辑
    }
}

解释:

  • @RabbitListener 注解监听指定队列的消息。

  • 当消息到达队列时,receiveMessage 方法会被自动调用。


总结 📚

本文介绍了如何在 Spring Boot 中集成 RabbitMQ,展示了配置、发送消息、主动消费和自动监听消息的基本用法。通过这种方式,我们能够实现系统间的解耦和异步处理,大大提高系统的灵活性和扩展性。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jamie Chyi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值