一.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可以看到:
可以看到 绑定了这个交换机的所有队列都收到生产者发送的消息 。