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

相关推荐
江上月5131 小时前
django与vue3的对接流程详解(上)
后端·python·django
秦禹辰1 小时前
轻量级开源文件共享系统PicoShare本地部署并实现公网环境文件共享
开发语言·后端·golang
Emrys_1 小时前
Redis 为什么这么快?一次彻底搞懂背-后的秘密 🚀
后端·面试
程序员小假1 小时前
我们来说一说 Java 自动装箱与拆箱是什么?
java·后端
随便写写1 小时前
python项目和环境管理工具 UV
后端
摇滚侠1 小时前
Spring Boot 3零基础教程,依赖管理机制,笔记06
spring boot·笔记·后端
❀͜͡傀儡师2 小时前
Spring 前后端通信加密解密
java·后端·spring
徐小夕2 小时前
花了4个月时间,我写了一款支持AI的协同Word文档编辑器
前端·vue.js·后端
花花无缺3 小时前
资源泄露问题
java·后端·http
paopaokaka_luck4 小时前
基于SpringBoot+Vue的少儿编程培训机构管理系(WebSocket及时通讯、协同过滤算法、Echarts图形化分析)
java·vue.js·spring boot·后端·spring