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,并使用消息发送者发送消息,消息接收者监听队列并处理接收到的消息。同时,还可以根据需要进行交换机和队列的配置。

相关推荐
苏三的开发日记13 分钟前
windows系统搭建kafka环境
后端
爬山算法23 分钟前
Netty(19)Netty的性能优化手段有哪些?
java·后端
Tony Bai24 分钟前
Cloudflare 2025 年度报告发布——Go 语言再次“屠榜”API 领域,AI 流量激增!
开发语言·人工智能·后端·golang
想用offer打牌37 分钟前
虚拟内存与寻址方式解析(面试版)
java·后端·面试·系统架构
無量41 分钟前
AQS抽象队列同步器原理与应用
后端
9号达人1 小时前
支付成功订单却没了?MyBatis连接池的坑我踩了
java·后端·面试
用户497357337981 小时前
【轻松掌握通信协议】C#的通信过程与协议实操 | 2024全新
后端
草莓熊Lotso1 小时前
C++11 核心精髓:类新功能、lambda与包装器实战
开发语言·c++·人工智能·经验分享·后端·nginx·asp.net
追逐时光者2 小时前
精选 8 个 .NET 开发实用的类库,效率提升利器!
后端·.net
a程序小傲2 小时前
京东Java面试被问:Fork/Join框架的使用场景
java·开发语言·后端·postgresql·面试·职场和发展