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

相关推荐
源码方舟2 小时前
SpringBoot + Shiro + JWT 实现认证与授权完整方案实现
java·spring boot·后端
热河暖男6 小时前
【实战解决方案】Spring Boot+Redisson构建高并发Excel导出服务,彻底解决系统阻塞难题
spring boot·后端·excel
noravinsc10 小时前
redis是内存级缓存吗
后端·python·django
noravinsc11 小时前
django中用 InforSuite RDS 替代memcache
后端·python·django
喝醉的小喵12 小时前
【mysql】并发 Insert 的死锁问题 第二弹
数据库·后端·mysql·死锁
kaixin_learn_qt_ing12 小时前
Golang
开发语言·后端·golang
炒空心菜菜13 小时前
MapReduce 实现 WordCount
java·开发语言·ide·后端·spark·eclipse·mapreduce
wowocpp15 小时前
spring boot Controller 和 RestController 的区别
java·spring boot·后端
后青春期的诗go15 小时前
基于Rust语言的Rocket框架和Sqlx库开发WebAPI项目记录(二)
开发语言·后端·rust·rocket框架
freellf15 小时前
go语言学习进阶
后端·学习·golang