rabbitmq基础(4)扇形交换机 java代码
扇形交换机概念
扇型交换机,这个交换机没有路由键概念,就算你绑了路由键也是无视的。 这个交换机在接收到消息后,会直接转发到绑定到它上面的所有队列。
上代码
(1)配置类
package com.zyw.rabbitmq.config;
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author zyw
*/
@Configuration
public class FanoutRabbitConfig {
/**
* 创建三个队列 :fanout.A fanout.B fanout.C
* 将三个队列都绑定在交换机 fanoutExchange 上
* 因为是扇型交换机, 路由键无需配置,配置也不起作用
*/
@Bean
public Queue queueA() {
return new Queue("fanout.A");
}
@Bean
public Queue queueB() {
return new Queue("fanout.B");
}
@Bean
public Queue queueC() {
return new Queue("fanout.C");
}
@Bean
FanoutExchange fanoutExchange() {
return new FanoutExchange("fanoutExchange");
}
@Bean
Binding bindingExchangeA() {
return BindingBuilder.bind(queueA()).to(fanoutExchange());
}
@Bean
Binding bindingExchangeB() {
return BindingBuilder.bind(queueB()).to(fanoutExchange());
}
@Bean
Binding bindingExchangeC() {
return BindingBuilder.bind(queueC()).to(fanoutExchange());
}
}
(2)生产者
package com.zyw.rabbitmq.controller;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import java.util.HashMap;
import java.util.Map;
/**
* @author zyw
* 扇形交换机
*/
@EnableScheduling
@Configuration
public class FanoutController {
@Autowired
RabbitTemplate rabbitTemplate;
@Scheduled(cron = "0 */1 * * * ?")
public void show(){
System.out.println("---------扇形换机開始----------------------");
Map<String,Object> map = new HashMap<>();
map.put("id","1111");
map.put("name","扇形");
rabbitTemplate.convertAndSend("fanoutExchange", null, map);
}
}
(3)消费者
消费者一
package com.zyw.rabbitcustomer;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
@RabbitListener(queues = "fanout.A")
public class FanoutReceiverA {
@RabbitHandler
public void process(Map testMessage) {
System.out.println("FanoutReceiverA消费者收到消息 : " +testMessage.toString());
}
}
消费者二
package com.zyw.rabbitcustomer;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
@RabbitListener(queues = "fanout.B")
public class FanoutReceiverB {
@RabbitHandler
public void process(Map testMessage) {
System.out.println("FanoutReceiverB消费者收到消息 : " + testMessage.toString());
}
}
实际输出
FanoutReceiverA消费者收到消息 : {name=扇形, id=1111}
FanoutReceiverB消费者收到消息 : {name=扇形, id=1111}
总结:
至此主要三种交换机我们都讲完了,后续我们从队列角度分析
扇形交换机和直流交换机还是有些区别的。扇形交换机直接转发到绑定队列即可 而直流需要转发到指的的队列。
RabbitMQ包含四种不同的交换机类型:
Direct exchange:直连交换机,转发消息到routigKey指定的队列
Fanout exchange:扇形交换机,转发消息到所有绑定队列(速度最快)
Topic exchange:主题交换机,按规则转发消息(最灵活)*
Headers exchange:首部交换机 (未接触)