一 . FanoutExchange模式
Fanout是路由广播的形式,将会把消息发给绑定它的全部队列,即便设置了key,也会被忽略.
二 .
1.导包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2. 在yaml文件中配置rabbitMq的基本配置;
server:
port: 8080
spring:
application:
name: springboot_rabbitmq_topic
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
3. 书写RabbitMq的配置类:
a . 创建对类queue
b . 创建交换机TopicExchange
c . 根据相应的规则将队列绑定到交换机上
package com.zdj.rabbitmq;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Created by Administrator on 2018/4/13.
*/
@Configuration
public class RabbitMqConfig {
/**
* 创建三个队列 一个fanoutexchange路由器(交换机),将三个队列分别绑定到交换机上去
* @return
*/
// a. 创建队列
@Bean(name = "Amessage")
public Queue AMessage(){
return new Queue("fanout.A");
}
@Bean(name = "Bmessage")
public Queue BMessage(){
return new Queue("fanout.B");
}
@Bean(name = "Cmessage")
public Queue CMessage(){
return new Queue("fanout.C");
}
// b. 创建交换机 FanoutExchange
@Bean
FanoutExchange fanoutExchange(){
return new FanoutExchange("fanoutExchange"); // 配置广播路由器
}
// c. 按照规则绑定 队列Queue 和 交换机 fanoutExchange
/*
将 队列 queue 绑定到 交换机 exchange上去,返回一个BindingBuilder对象
*/
@Bean
Binding bindingExchangeA(@Qualifier("Amessage")Queue aqueue,FanoutExchange exchange){
return BindingBuilder.bind(aqueue).to(exchange);
}
@Bean
Binding bindingExchangeB(@Qualifier("Bmessage")Queue bqueue,FanoutExchange exchange){
return BindingBuilder.bind(bqueue).to(exchange);
}
@Bean
Binding bindingExchangeC(@Qualifier("Cmessage")Queue cqueue,FanoutExchange exchange){
return BindingBuilder.bind(cqueue).to(exchange);
}
}
4. 创建消息发送类 AmqpTemplate
package com.zdj.rabbitmq;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* Created by Administrator on 2018/4/13.
*/
@Component
public class RabbitMqSender {
@Autowired
private AmqpTemplate amqpTemplate;
/*
第一个参数 交换机名称
第二个参数 绑定规则
第三个参数 发送的消息
因为这里是fanoutExchange ,即使配置了发送的routing_key也是不起作用的,故此处直接忽略参数2
*/
public void sendeMessage(){
amqpTemplate.convertAndSend("fanoutExchange","","这是fanoutExchange传递的消息");
}
}
5. 创建消息发送测试类
package com.zdj.rabbitmq;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
// 测试类模拟测试发送消息
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootRabbitmq0413ApplicationTests {
@Autowired
private RabbitMqSender rabbitMqSender;
@Test
public void contextLoads() {
rabbitMqSender.sendeMessage();
}
}
6. 创建消息接收类
package com.zdj.rabbitmq;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* Created by Administrator on 2018/4/13.
*/
@Component
public class RabbitReceiver {
@RabbitListener(queues = "fanout.A")
public void receiverA(){
System.out.println("我接收的是fanout.A队列的消息");
}
@RabbitListener(queues = "fanout.B")
public void receiverB(){
System.out.println("我接收的是fanout.B队列的消息");
}
@RabbitListener(queues = "fanout.C")
public void receiverC(){
System.out.println("我接收的是fanout.C队列的消息");
}
}
7. 测试:
(1)启动消息接收类主应用
(2) 启动消息测试类发送消息
(3) 分析:
FanoutExchange模式下,广播订阅,是向所有的消费者发布消息,但是只有消费者将队列绑定到该路由器才能收到消息,忽略routing_key的;