RabbitMQ中交换机的几种模式

目录

简述

交换机模式

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=queue_fanout02])

Fanout模式原理图

p࿱

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值