RabbitMQ SpringBoot Fanout-Exchange模式 应用

一.Fanout-Exchange 模式:

忽略key对比,发送Message到Exchange下游绑定的所有Queue
在这里插入图片描述
Fanout 就是我们熟悉的广播模式或者订阅模式,给Fanout交换机发送消息,绑定了这个交换机的所有队列都收到这个消息

项目使用上一篇中的项目 rabbitmq-produce、rabbitmq-consumer

二.rabbitmq-produce的改动

2.1 在rabbitmq-produce中,新增一个FanoutRabbitConfig配置类

FanoutRabbitConfig 的代码如下:

package com.example.rabbitmqproduce.config;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Fanout-exchange 配置类
 * Fanout 就是我们熟悉的广播模式或者订阅模式,给Fanout交换机发送消息,绑定了这个交换机的所有队列都收到这个消息
 * 因为是扇型交换机, bindingKey 无需配置,配置也不起作用
 */
@Configuration
public class FanoutRabbitConfig {

    private Logger logger = LoggerFactory.getLogger(FanoutRabbitConfig.class);

    //定义 Fanout-exchange模型 的队列名
    private static final String fanoutQueueNameA = "fanoutQueueA";

    //定义 Fanout-exchange模型 的队列名
    private static final String fanoutQueueNameB = "fanoutQueueB";

    //定义 Fanout-exchange模型 的交换机名
    private static final String fanoutExchangeName = "fanoutExchange";

    //定义 Fanout-exchange模型 的bingingKey
    //因为是扇型交换机, bindingKey 无需配置,配置也不起作用
    //private static final String fanoutBindingKey = "fanoutRouting";


    /**
     * 队列 起名:fanout
     */
    @Bean
    public Queue fanoutQueueA() {
        return new Queue(fanoutQueueNameA);
    }

    @Bean
    public Queue fanoutQueueB() {
        return new Queue(fanoutQueueNameB);
    }

    /**
     * Fanout交换机 起名:fanoutExchange
     */
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange(fanoutExchangeName);
    }


    /**
     * 绑定  将队列和交换机绑定
     * @return
     */
    @Bean
    public Binding bindingFanoutA() {
        return BindingBuilder.bind(fanoutQueueA()).to(fanoutExchange());
    }


    /**
     * 绑定  将队列和交换机绑定
     * @return
     */
    @Bean
    public Binding bindingFanoutB() {
        return BindingBuilder.bind(fanoutQueueB()).to(fanoutExchange());
    }

}

2.2 新建一个Fanout-exchange模式 的消息生产者FanoutExchangeProduce

FanoutExchangeProduce 的代码如下:

package com.example.rabbitmqproduce.produce;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Fanout-exchange模式 的消息生产者
 * @Component 注入到Spring容器中
 */
@Component
public class FanoutExchangeProduce {

    //注入一个AmqpTemplate来发布消息
    @Autowired
    private AmqpTemplate rabbitTemplate;

    private Logger logger = LoggerFactory.getLogger(FanoutExchangeProduce.class);

    private static final String fanoutExchangeName = "fanoutExchange";

    //routingKey 无需配置,配置也不起作用
    //private static final String fanoutRouteKey = "fanoutRouting";


    /**
     * 发送消息
     */
    public void sendMessage() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "hello!亚索 面对疾风吧";
        String createTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        logger.info("发送的内容 : " + map.toString());
        //因为是fanout广播,所以不用将routingKey 发送给路由器
        rabbitTemplate.convertAndSend("fanoutExchange", null, map);
    }

}

注:因为是fanout广播,所以不用将routingKey 发送给路由器

2.3 写个测试的TestController类

代码如下:

package com.example.rabbitmqproduce.controller;


import com.example.rabbitmqproduce.produce.DirectExchangeProduce;
import com.example.rabbitmqproduce.produce.FanoutExchangeProduce;
import com.example.rabbitmqproduce.produce.RabbitMqProduce;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("TestController")
public class TestController {

    private static final Logger logger = LoggerFactory.getLogger(TestController.class);

    @Autowired
    private RabbitMqProduce rabbitMqProduce;

    @Autowired
    private DirectExchangeProduce directExchangeProduce;

    @Autowired
    private FanoutExchangeProduce fanoutExchangeProduce;

    /**
     * 测试基本消息模型(简单队列)
     */
    @RequestMapping(value = "/testSimpleQueue", method = RequestMethod.POST)
    public void testSimpleQueue() {
        logger.info("测试基本消息模型(简单队列)SimpleQueue---开始");
        for (int i = 0; i < 10; i++) {
            rabbitMqProduce.sendMessage();
        }
        logger.info("测试基本消息模型(简单队列)SimpleQueue---结束");
    }


    /**
     * 测试 Direct-exchange模式
     */
    @RequestMapping(value = "/directExchangeTest", method = RequestMethod.POST)
    public void directExchangeTest() {
        logger.info("测试 Direct-exchange模式 队列名为directQueue---开始");
        for (int i = 0; i < 10; i++) {
            directExchangeProduce.sendMessage();
        }
        logger.info("测试 Direct-exchange模式 队列名为directQueue---结束");
    }


    /**
     * 测试 Fanout-exchange模式
     */
    @RequestMapping(value = "/fanoutExchangeTest", method = RequestMethod.POST)
    public void fanoutExchangeTest() {
        logger.info("测试 fanout-exchange模式 队列名为fanoutQueue---开始");
        fanoutExchangeProduce.sendMessage();
        logger.info("测试 fanout-exchange模式 队列名为fanoutQueue---结束");
    }
}

三.rabbitmq-consumer的改动

3.1 新建一个Fanout-exchange模式 的消息消费者FanoutExchangeConsumerA 类 和 FanoutExchangeConsumerB 类

FanoutExchangeConsumerA 代码如下:

package com.example.rabbitmqconsumer.consumer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Map;

/**
 * Fanout-exchange模式 的消息消费者
 * @RabbitListener(queues = "fanoutQueueA") 监听名为dfanoutQueueA的队列
 */

@Component
@RabbitListener(queues = "fanoutQueueA")
public class FanoutExchangeConsumerA {

    @Autowired
    private AmqpTemplate rabbitmqTemplate;

    private Logger logger = LoggerFactory.getLogger(FanoutExchangeConsumerA.class);

    /**
     * 消费消息
     * @RabbitHandler 代表此方法为接受到消息后的处理方法
     */
    @RabbitHandler
    public void receiveMessage(Map msg){
        logger.info("FanoutExchangeA消费者接收到的消息 :" + msg.toString());
    }

}

FanoutExchangeConsumerB 代码如下:

package com.example.rabbitmqconsumer.consumer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Map;

/**
 * Fanout-exchange模式 的消息消费者
 * @RabbitListener(queues = "fanoutQueueB") 监听名为fanoutQueueB的队列
 */

@Component
@RabbitListener(queues = "fanoutQueueB")
public class FanoutExchangeConsumerB {

    @Autowired
    private AmqpTemplate rabbitmqTemplate;

    private Logger logger = LoggerFactory.getLogger(FanoutExchangeConsumerB.class);

    /**
     * 消费消息
     * @RabbitHandler 代表此方法为接受到消息后的处理方法
     */
    @RabbitHandler
    public void receiveMessage(Map msg){
        logger.info("FanoutExchangeB消费者接收到的消息 :" + msg.toString());
    }

}

四.测试

首先启动生产者rabbitmq-produce项目。在postman或浏览器上访问:
http://localhost:8783/TestController/fanoutExchangeTest POST请求
这时可以在rabbitmq-produce的控制台可以看到
在这里插入图片描述

然后再启动消费者rabbitmq-consumer工程,在rabbitmq-consumer可以看到:
在这里插入图片描述
可以看到 绑定了这个交换机的所有队列都收到生产者发送的消息 。

下一章,学习 Topic-Exchange 模式 在SpringBoot中的应用

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘德华一不小心就打代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值