RabbitMQ中交换机的几种模式

本文介绍了RabbitMQ中的四种交换机模式:Fanout(广播模式)、Direct(直接模式)、Topic(主题模式)和Headers(头部模式)。通过具体测试案例展示了如何配置和使用这些模式,以及它们的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

简述

交换机模式

Fanout模式

Direct模式

Topic模式

Headers模式


简述

生产者不直接跟队列打交道,而是通过交换机。交换机类似于生产者和队列直接的一个管理者,它将生产的消息分配给对应的队列。

前期准备pom.xml中的依赖

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

具体整合详细可见 整合Springboot和RabbitMQ


交换机模式

Fanout模式

        又叫广播模式,类似于一个微信公众号,当公众号发布消息的时候所有关注该公众号的用户都能收到消息,FanoutExchange的功能就类似与公众号。

测试步骤

  1. Config中配置两个队列分别为03和04在配置一个FanoutExchange交换机

  2. 将交换机和两个队列03,04分别绑定

  3. 发送消息给交换机

  4. 分别接收03和04队列的消息

配置Config

package com.shao.seckill.config;

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;



@Configuration
public class RabbitMQConfigFanout {

    //准备两个队列名称和一个交换机名称
    private static final String QUEUE03 = "queue_fanout01";
    private static final String QUEUE04 = "queue_fanout02";
    private static final String EXCHANGE02 = "fanoutExchange";

    //创建队列QUEUE01
    @Bean
    public Queue queue01(){
        return new Queue(QUEUE03);
    }

    //创建队列QUEUE02
    @Bean
    public Queue queue02(){
        return new Queue(QUEUE04);
    }

    //创建交换机EXCHANGE
    @Bean
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange(EXCHANGE02);
    }

    //将EXCHANGE和QUEUE01绑定
    @Bean
    public Binding binding01(){
        return BindingBuilder.bind(queue01()).to(fanoutExchange());
    }

    //将EXCHANGE和QUEUE02绑定
    @Bean
    public Binding binding02(){
        return BindingBuilder.bind(queue02()).to(fanoutExchange());
    }

}

生产者/发送者 

package com.shao.seckill.rabbitmq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class MQSender {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送消息给fanoutExchange
     * @param msg
     */
    public void sendtoFanoutExchange(Object msg){
        log.info("发送消息:"+msg);
        rabbitTemplate.convertAndSend("fanoutExchange","",msg);
    }

}

消费者/接收者

package com.shao.seckill.rabbitmq;


import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class MQReceiver {

    //从queue_fanout01队列中接收消息
    @RabbitListener(queues = "queue_fanout01")
    public void receiver01(Object msg){
        log.info("QUEUE01接收消息:"+msg);
    }

    //从queue_fanout02队列中接收消息
    @RabbitListener(queues = "queue_fanout02")
    public void receiver02(Object msg){
        log.info("QUEUE02接收消息:"+msg);
    }

}

控制台打印结果

MQSender : 发送消息:Hello

MQReceiver : QUEUE01接收消息:(Body:'Hello' MessageProperties [headers={}, contentType=text/plain, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=fanoutExchange, receivedRoutingKey=, deliveryTag=1, consumerTag=amq.ctag-UIsBEjz13DiHyE60FUbZnw, consumerQueue=queue_fanout01])

MQReceiver : QUEUE02接收消息:(Body:'Hello' MessageProperties [headers={}, contentType=text/plain, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=fanoutExchange, receivedRoutingKey=, deliveryTag=1, consumerTag=amq.ctag-C5iC4RWpyAkD7PaoGU3eEg, consumerQueue&#

RabbitMQ是一个开源的消息中间件,它支持多种工作模式。以下是RabbitMQ几种常见工作模式: 1. 简单模式(Simple Mode):简单模式是最基本的工作模式,它包括一个生产者和一个消费者。生产者将消息发送到队列,消费者从队列中接收并处理消息。 2. 工作队列模式(Work Queue Mode):工作队列模式也被称为任务队列模式。它包括一个生产者和多个消费者。生产者将消息发送到队列,多个消费者从队列中接收消息并进行处理。消息在队列中以先进先出的顺序分发给消费者。 3. 发布/订阅模式(Publish/Subscribe Mode):发布/订阅模式用于将消息广播给多个消费者。生产者将消息发送到交换机(Exchange),交换机将消息广播给所有与之绑定的队列,然后消费者从队列中接收并处理消息。 4. 路由模式(Routing Mode):路由模式用于根据消息的路由键(Routing Key)将消息发送到指定的队列。生产者将消息发送到交换机,并指定一个路由键,交换机根据路由键将消息发送到与之匹配的队列,然后消费者从队列中接收并处理消息。 5. 主题模式(Topic Mode):主题模式是一种更灵活的路由模式,它根据消息的主题(Topic)将消息发送到指定的队列。生产者将消息发送到交换机,并指定一个主题,交换机根据主题将消息发送到与之匹配的队列,然后消费者从队列中接收并处理消息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值