目录
简述
生产者不直接跟队列打交道,而是通过交换机。交换机类似于生产者和队列直接的一个管理者,它将生产的消息分配给对应的队列。
前期准备pom.xml中的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
具体整合详细可见 整合Springboot和RabbitMQ
交换机模式
Fanout模式
又叫广播模式,类似于一个微信公众号,当公众号发布消息的时候所有关注该公众号的用户都能收到消息,FanoutExchange的功能就类似与公众号。
测试步骤
Config中配置两个队列分别为03和04在配置一个FanoutExchange交换机
将交换机和两个队列03,04分别绑定
发送消息给交换机
分别接收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&#