Spring Boot 集成 RabbitMQ 的详细步骤

以下是在 Spring Boot 中集成 RabbitMQ 的详细步骤,包括配置、消息发送和接收的完整流程:


1. 添加依赖

pom.xml 中添加 RabbitMQ 和 Spring Boot 的依赖:

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

2. 配置 RabbitMQ 连接信息

application.ymlapplication.properties 中配置 RabbitMQ 服务器信息:

spring:
  rabbitmq:
    host: localhost    # RabbitMQ 服务器地址
    port: 5672         # 默认端口
    username: guest    # 默认用户名
    password: guest    # 默认密码
    virtual-host: /    # 虚拟主机(可选)

3. 定义交换机、队列和绑定关系

创建一个配置类 RabbitMQConfig,定义交换机和队列的绑定关系:

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    // 定义交换机
    public static final String EXCHANGE_NAME = "demo_exchange";
    // 定义队列
    public static final String QUEUE_NAME = "demo_queue";
    // 定义路由键
    public static final String ROUTING_KEY = "demo_routing_key";

    // 创建直连交换机(Direct Exchange)
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(EXCHANGE_NAME);
    }

    // 创建队列
    @Bean
    public Queue queue() {
        return new Queue(QUEUE_NAME);
    }

    // 绑定队列到交换机,并指定路由键
    @Bean
    public Binding binding(Queue queue, DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY);
    }
}

4. 创建消息生产者

通过 AmqpTemplate 发送消息:

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MessageProducer {

    @Autowired
    private AmqpTemplate amqpTemplate;

    public void sendMessage(String message) {
        // 发送消息到交换机,并指定路由键
        amqpTemplate.convertAndSend(
            RabbitMQConfig.EXCHANGE_NAME,
            RabbitMQConfig.ROUTING_KEY,
            message
        );
        System.out.println("消息已发送: " + message);
    }
}

5. 创建消息消费者

使用 @RabbitListener 监听队列并处理消息:

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class MessageConsumer {

    // 监听指定队列
    @RabbitListener(queues = RabbitMQConfig.QUEUE_NAME)
    public void receiveMessage(String message) {
        System.out.println("收到消息: " + message);
    }
}

6. 测试消息发送和接收

在 Controller 中调用生产者发送消息:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @Autowired
    private MessageProducer messageProducer;

    @GetMapping("/send")
    public String sendMessage() {
        messageProducer.sendMessage("Hello RabbitMQ!");
        return "消息已发送";
    }
}

7. 启动应用并验证

  1. 启动 RabbitMQ 服务器(本地或远程)。
  2. 启动 Spring Boot 应用,访问 http://localhost:8080/send 发送消息。
  3. 查看控制台输出,确认消费者是否收到消息:
    消息已发送: Hello RabbitMQ!
    收到消息: Hello RabbitMQ!
    

8. 补充说明

  • 交换机类型:RabbitMQ 支持 DirectTopicFanoutHeaders 四种交换机类型,根据业务需求选择。
  • 消息持久化:队列和消息可配置持久化(示例中未启用):
    @Bean
    public Queue durableQueue() {
        return new Queue(QUEUE_NAME, true); // true 表示持久化
    }
    
  • 消息确认机制:可通过配置 spring.rabbitmq.listener.simple.acknowledge-mode 控制消息确认模式。

通过以上步骤,即可在 Spring Boot 中完成 RabbitMQ 的集成。如果有其他需求(如延迟队列、死信队列),可进一步扩展配置。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值