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");
    }
}
相关推荐
计算机学姐1 天前
基于SpringBoot的校园资源共享系统【个性化推荐算法+数据可视化统计】
java·vue.js·spring boot·后端·mysql·spring·信息可视化
廋到被风吹走1 天前
【Spring】Spring Boot 配置管理深度指南:Profile、类型安全与加密
spring boot·安全·spring
J_liaty1 天前
RabbitMQ面试题终极指南
开发语言·后端·面试·rabbitmq
BD_Marathon1 天前
SpringBoot程序快速启动
java·spring boot·后端
万物皆字节1 天前
Spring Cloud Gateway 启动流程源码分析
java·开发语言·spring boot
a程序小傲1 天前
得物Java面试被问:方法句柄(MethodHandle)与反射的性能对比和底层区别
java·开发语言·spring boot·后端·python·面试·职场和发展
沙白猿1 天前
Redis报错:A bean with that name has already been defined in class path resource
spring boot·redis·mybatis
+VX:Fegn08951 天前
计算机毕业设计|基于springboot + vue物流配送中心信息化管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·小程序·课程设计
计算机毕设指导61 天前
基于微信小程序的钓鱼论坛系统【源码文末联系】
java·spring boot·mysql·微信小程序·小程序·tomcat·maven
qq_12498707531 天前
基于微信小程序的宠物交易平台的设计与实现(源码+论文+部署+安装)
java·spring boot·后端·微信小程序·小程序·毕业设计·计算机毕业设计