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");
    }
}
相关推荐
keep one's resolveY15 分钟前
SpringBoot实现重试机制的四种方案
java·spring boot·后端
阿丰资源2 小时前
基于Spring Boot的电影城管理系统(直接运行)
java·spring boot·后端
消失的旧时光-19433 小时前
Spring Boot 工程化进阶:统一返回 + 全局异常 + AOP 通用工具包
java·spring boot·后端·aop·自定义注解
StockTV4 小时前
印度股票实时数据 NSE和BSE的实时行情、K 线及指数数据
java·开发语言·spring boot·python
橘子海全栈攻城狮5 小时前
【最新源码】养老院系统管理A013
java·spring boot·后端·web安全·微信小程序
敖正炀5 小时前
反模式与排查宝典:Spring Boot 自动配置与核心机制的常见陷阱
spring boot
直奔標竿6 小时前
Java开发者AI转型第二十六课!Spring AI 个人知识库实战(五)——联网搜索增强实战
java·开发语言·人工智能·spring boot·后端·spring
吴爃7 小时前
Spring Boot 项目在 K8S 中的打包、部署与运维发布实践
运维·spring boot·kubernetes
a8a3027 小时前
Laravel8.x新特性全解析
java·spring boot·后端