一.Direct-Exchange 模式:
完全匹配,Exchange根据对比Message的routing key和Queue的binding key,
然后按一定的分发路由规则,决定Message发送到哪个Queue
完全匹配,消息路由到那些 Routing Key 与 Binding Key 完全匹配的 Queue 中(Routing Key = Binding Key)。比如 Routing Key 为cleint-key,只会转发cleint-key,不会转发cleint-key.1,也不会转发cleint-key.1.2.
项目使用上一篇中的项目 rabbitmq-produce、rabbitmq-consumer
二.rabbitmq-produce的改动
2.1 在rabbitmq-produce中,新增一个DirectRabbitConfig配置类
注:
队列与交换机的绑定,不能是任意绑定了,而是要指定一个bindingKey,消息的发送方在 向 Exchange发送消息时,也必须指定消息的 RoutingKey。Exchange不再把消息交给每一个绑定的队列,而是根据消息的Routing Key进行判断,只有队列的Routingkey与消息的 bindingKey完全一致,才会接收到消息.
DirectRabbitConfig 的代码如下:
package com.example.rabbitmqproduce.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.amqp.core.Binding;
/**
* Direct-exchange 配置类
* Direct
* 队列与交换机的绑定,不能是任意绑定了,而是要指定一个bindingKey
* 消息的发送方在 向 Exchange发送消息时,也必须指定消息的 RoutingKey。
* Exchange不再把消息交给每一个绑定的队列,而是根据消息的Routing Key进行判断,
* 只有队列的Routingkey与消息的 bindingKey完全一致,才会接收到消息.
*/
@Configuration
public class DirectRabbitConfig {
private Logger logger = LoggerFactory.getLogger(DirectRabbitConfig.class);
//定义 Direct-exchange模型 的队列名
private static final String directQueueName = "directQueue";
//定义 Direct-exchange模型 的交换机名
private static final String directExchangeName = "directExchange";
//定义 Direct-exchange模型 的bingingKey
private static final String directBindingKey = "directRouting";
/**
* 队列 起名:direct
*/
@Bean
public Queue directQueue() {
return new Queue(directQueueName);
}
/**
* Direct交换机 起名:directExchange
*/
@Bean
public DirectExchange directExchange() {
return new DirectExchange(directExchangeName);
}
/**
* 绑定 将队列和交换机绑定, 并设置用于匹配键:directRouting
* @return
*/
@Bean
public Binding bindingDirect() {
return BindingBuilder.bind(directQueue()).to(directExchange()).with(directBindingKey);
}
}
2.2 新建一个Direct-exchange模式 的消息生产者DirectExchangeProduce
DirectExchangeProduce的代码如下:
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;
/**
* Direct-exchange模式 的消息生产者
* @Component 注入到Spring容器中
*/
@Component
public class DirectExchangeProduce {
//注入一个AmqpTemplate来发布消息
@Autowired
private AmqpTemplate rabbitTemplate;
private Logger logger = LoggerFactory.getLogger(DirectExchangeProduce.class);
//direct 交换机名称
private static final String directExchangeName = "directExchange";
//direct 的RoutingKey
private static final String directRouteKey = "directRouting";
/**
* 发送消息
*/
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());
//将消息携带绑定键值:directRouting 发送到交换机directExchange
rabbitTemplate.convertAndSend(directExchangeName, directRouteKey, map);
}
}
2.3 写个测试的TestController类
代码如下:
package com.example.rabbitmqproduce.controller;
import com.example.rabbitmqproduce.produce.DirectExchangeProduce;
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;
/**
* 测试基本消息模型(简单队列)
*/
@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---结束");
}
}
三.rabbitmq-consumer的改动
3.1 新建一个direct-exchange模式 的消息消费者DirectExchangeConsumer类
DirectExchangeConsumer 代码如下:
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;
/**
* Direct-exchange模式 的消息消费者
* @RabbitListener(queues = "directQueue") 监听名为directQueue的队列
*/
@Component
@RabbitListener(queues = "directQueue")
public class DirectExchangeConsumer {
@Autowired
private AmqpTemplate rabbitmqTemplate;
private Logger logger = LoggerFactory.getLogger(DirectExchangeConsumer.class);
/**
* 消费消息
* @RabbitHandler 代表此方法为接受到消息后的处理方法
*/
@RabbitHandler
public void receiveMessage(Map msg){
logger.info("DirectExchange消费者接收到的消息 :" + msg.toString());
}
}
四.测试
首先启动生产者rabbitmq-produce项目。在postman或浏览器上访问:
http://localhost:8783/TestController/directExchangeTestPOST请求
这时可以在rabbitmq-produce的控制台可以看到
然后再启动消费者rabbitmq-consumer工程,在rabbitmq-consumer可以看到: