springboot如何接入mq?

springboot如何接入mq?

下面以 RabbitMQ 为例,介绍如何在 Spring Boot 中接入 MQ。

  1. 添加依赖:在 pom.xml 文件中添加 RabbitMQ 的相关依赖。可以根据需要选择合适的版本,例如:
xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
  1. 配置 RabbitMQ 连接信息:在 application.properties(或 application.yml)中配置 RabbitMQ 的连接信息,例如:
java 复制代码
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
  1. 创建消息发送者:创建一个类来发送消息,例如 MessageSender
java 复制代码
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MessageSender {

    private final AmqpTemplate amqpTemplate;

    @Autowired
    public MessageSender(AmqpTemplate amqpTemplate) {
        this.amqpTemplate = amqpTemplate;
    }

    public void sendMessage(String exchange, String routingKey, Object message) {
        amqpTemplate.convertAndSend(exchange, routingKey, message);
    }
}

MessageSender 类中,通过自动注入 AmqpTemplate 对象,使用 convertAndSend() 方法发送消息到指定的交换机和路由键。

  1. 创建消息接收者:创建一个类来接收消息,例如 MessageReceiver
java 复制代码
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class MessageReceiver {

    @RabbitListener(queues = "myQueue")
    public void receiveMessage(String message) {
        // 处理接收到的消息
        System.out.println("Received message: " + message);
    }
}

MessageReceiver 类中,使用 @RabbitListener 注解指定需要监听的队列,然后定义一个方法来处理接收到的消息。

  1. 配置交换机和队列:如果需要自定义交换机和队列,可以创建一个配置类来进行配置,例如:
java 复制代码
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    @Bean
    public Queue myQueue() {
        return new Queue("myQueue");
    }

    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange("myExchange");
    }

    @Bean
    public Binding binding(Queue myQueue, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(myQueue).to(fanoutExchange);
    }
}

可以在配置类中使用 @Bean 注解来创建队列、交换机和绑定规则。

  1. 使用消息发送者发送消息:在需要发送消息的地方,通过依赖注入 MessageSender,然后调用 sendMessage() 方法发送消息,例如:
java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    private final MessageSender messageSender;

    @Autowired
    public MyController(MessageSender messageSender) {
        this.messageSender = messageSender;
    }

    @GetMapping("/send-message")
    public void sendMessage() {
        String exchange = "myExchange";
        String routingKey = "";
        Object message = "Hello, RabbitMQ!";
        messageSender.sendMessage(exchange, routingKey, message);
    }
}

我们通过依赖注入 MessageSender 对象,在需要发送消息的方法中调用 sendMessage() 方法发送消息。

我们可以在 Spring Boot 中接入 RabbitMQ,并使用消息发送者发送消息,消息接收者监听队列并处理接收到的消息。同时,还可以根据需要进行交换机和队列的配置。

相关推荐
leobertlan13 分钟前
2025年终总结
前端·后端·程序员
面向Google编程36 分钟前
从零学习Kafka:数据存储
后端·kafka
易安说AI2 小时前
Claude Opus 4.6 凌晨发布,我体验了一整晚,说说真实感受。
后端
易安说AI2 小时前
Ralph Loop 让Claude无止尽干活的牛马...
前端·后端
易安说AI2 小时前
用 Claude Code 远程分析生产日志,追踪 Claude Max 账户被封原因
后端
颜酱3 小时前
图结构完全解析:从基础概念到遍历实现
javascript·后端·算法
Coder_Boy_5 小时前
基于SpringAI的在线考试系统-考试系统开发流程案例
java·数据库·人工智能·spring boot·后端
掘金者阿豪6 小时前
关系数据库迁移的“暗礁”:金仓数据库如何规避数据完整性与一致性风险
后端
ServBay6 小时前
一个下午,一台电脑,终结你 90% 的 Symfony 重复劳动
后端·php·symfony
sino爱学习6 小时前
高性能线程池实践:Dubbo EagerThreadPool 设计与应用
java·后端