SpringBoot整合RabbitMQ

SpringBoot整合RabbitMQ

文章目录

RabbitMQ下载与安装




SpringBoot整合RabbitMQ(直连交换机模式)

导坐标

java 复制代码
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

改配置

java 复制代码
spring:
  rabbitmq:
    host: localhost
    port: 5672

定义消息队列

这里注意Queue的导包的时候,导的包一定要是

amop.core下的包。

java 复制代码
import org.springframework.amqp.core.Queue;

消息队列和交换机做绑定的操作

同一个交换机是可以复用的

java 复制代码
@Configuration
public class RabbitConfigDirect {

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

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

    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange("directExchange");
    }

    @Bean
    public Binding bindingDirect(){
        return BindingBuilder
                .bind(directQueue())
                .to(directExchange())
                .with("direct");
    }

    @Bean
    public Binding bindingDirect2(){
        return BindingBuilder
                .bind(directQueue2())
                .to(directExchange())
                .with("direct2");
    }
}

生产消息

java 复制代码
@Service
public class MessageServiceRabbitmqDirectImpl implements MessageService {

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Override
    public void sendMessage(String id) {
        System.out.println("待发送短信的订单已纳入处理队列 (rabbitmq direct),id:" + id);
        amqpTemplate.convertAndSend("directExchange","direct",id);

    }
}

消费消息------Listener

多个监听配上同一个消息队列,可以轮询消费消息

java 复制代码
@Component
public class MessageListener {

    @RabbitListener(queues = "dirct_queue")
    public void receive(String id){
        System.out.println("已完成短信发送业务(rabbitmq direct):id:" + id);
    }
}
java 复制代码
@Component
public class MessageListener2 {

    @RabbitListener(queues = "dirct_queue")
    public void receive(String id){
        System.out.println("已完成短信发送业务(rabbitmq direct two):id:" + id);
    }
}

SpringBoot整合RabbitMQ(主题交换机模式)

代码几乎没区别,但是代码模式有区别

使用主题交换机可以实现直连交换机

绑定匹配规则发生了一些变化

java 复制代码
@Service
public class MessageServiceRabbitmqTopicImpl implements MessageService {

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Override
    public void sendMessage(String id) {
        System.out.println("待发送短信的订单已纳入处理队列 (rabbitmq topic),id:" + id);
        amqpTemplate.convertAndSend("topicExchange","topic.*.id",id);
    }

    @Override
    public String doMessage() {
        return null;
    }
}

交换机的代码几乎不用发生变化,只需要把直连交换机换成主题交换机

java 复制代码
@Configuration
public class RabbitConfigTopic {

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

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

    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange("topicExchange");
    }

    @Bean
    public Binding bindingTopic(){
        return BindingBuilder
                .bind(topicQueue())
                .to(topicExchange())
                .with("topic.order.id");
    }

    @Bean
    public Binding bindingTopic2(){
        return BindingBuilder
                .bind(topicQueue2())
                .to(topicExchange())
                .with("topic2");
    }
}
相关推荐
止语Lab14 小时前
一次 goroutine 泄漏:pprof 说有 10 万个 goroutine,但问题不在 channel
rabbitmq
Flittly2 天前
【AgentScope Java新手村系列】(16)从RAG到多路检索
java·spring boot·spring
人活一口气2 天前
从JVM调优到MCP协议:Java全栈技术体系深度总结与企业级架构实践
java·spring boot
Java陈序员3 天前
企业级!一个基于 Java 开发的开源 AI 应用开发平台!
spring boot·agent·mcp
杨运交3 天前
[041][公共模块]分布式唯一ID生成器设计与实现:一款灵活可扩展的雪花算法框架
spring boot
Flittly4 天前
【AgentScope Java新手村系列】(14)人机交互
java·spring boot·spring
Flynt5 天前
从Spring Boot 4.0升到4.1,我在Maven和gRPC上栽了跟头
java·spring boot·后端
掉鱼的猫6 天前
Spring Boot → Solon 注解迁移实战指南:一张对照表说清楚
java·spring boot
人活一口气7 天前
Spring Boot与AIGC的完美结合:从零搭建智能内容生成平台
java·spring boot·aigc
java小白小10 天前
SpringBoot(01): 初识SpringBoot,从Spring的痛点说起
spring boot