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

相关推荐
武子康4 小时前
Java-80 深入浅出 RPC Dubbo 动态服务降级:从雪崩防护到配置中心秒级生效
java·分布式·后端·spring·微服务·rpc·dubbo
舒一笑5 小时前
我的开源项目-PandaCoder迎来史诗级大更新啦
后端·程序员·intellij idea
@昵称不存在6 小时前
Flask input 和datalist结合
后端·python·flask
zhuyasen6 小时前
Go 分布式任务和定时任务太难?sasynq 让异步任务从未如此简单
后端·go
东林牧之7 小时前
Django+celery异步:拿来即用,可移植性高
后端·python·django
超浪的晨7 小时前
Java UDP 通信详解:从基础到实战,彻底掌握无连接网络编程
java·开发语言·后端·学习·个人开发
AntBlack8 小时前
从小不学好 ,影刀 + ddddocr 实现图片验证码认证自动化
后端·python·计算机视觉
Pomelo_刘金8 小时前
Clean Architecture 整洁架构:借一只闹钟讲明白「整洁架构」的来龙去脉
后端·架构·rust
双力臂4048 小时前
Spring Boot 单元测试进阶:JUnit5 + Mock测试与切片测试实战及覆盖率报告生成
java·spring boot·后端·单元测试
midsummer_woo10 小时前
基于spring boot的医院挂号就诊系统(源码+论文)
java·spring boot·后端