✨✨个人主页:沫洺的主页
📚📚系列专栏: 📖 JavaWeb专栏📖 JavaSE专栏 📖 Java基础专栏📖vue3专栏
📖MyBatis专栏📖Spring专栏📖SpringMVC专栏📖SpringBoot专栏
📖Docker专栏📖Reids专栏📖MQ专栏📖SpringCloud专栏
💖💖如果文章对你有所帮助请留下三连✨✨
🙈SpringBoot使用扇型(广播)交换机(Fanout Exchange)
消费者
不需要routing key
扇型交换机在接收到消息后,会直接转发到绑定到它上面的所有队列。
@Component public class FanoutConsumer { private static final String ENAME = "211-FanoutExchage-01"; private static final String QNAME1 = "211-FanoutQueue-11"; private static final String QNAME2 = "211-FanoutQueue-12"; //定义一个交换机 @Bean public FanoutExchange fanoutExchange(){ return new FanoutExchange(ENAME, true, false); } //定义一个队列 @Bean public Queue fanoutQueue1(){ return QueueBuilder.durable(QNAME1).build(); } //定义一个队列 @Bean public Queue fanoutQueue2(){ return QueueBuilder.durable(QNAME2).build(); } //创建队列和交换机的绑定关系 @Bean public Binding binding1(){ return BindingBuilder.bind(fanoutQueue1()).to(fanoutExchange()); } //创建队列和交换机的绑定关系 @Bean public Binding binding2(){ return BindingBuilder.bind(fanoutQueue2()).to(fanoutExchange()); } @RabbitHandler @RabbitListener(queues =QNAME1) public void process1(UserRegisterOk userRegisterOk){ System.out.println("消费者1收到:"+userRegisterOk.getName()+","+userRegisterOk.getPhone()); } @RabbitHandler @RabbitListener(queues =QNAME2) public void process2(UserRegisterOk userRegisterOk){ System.out.println("消费者2收到:"+userRegisterOk.getName()+","+userRegisterOk.getPhone()); } }
生产者
@Component public class FanoutProducer { @Autowired private RabbitTemplate rabbitTemplate; public void sendMessage(){ UserRegisterOk userRegisterOk = UserRegisterOk.builder().name("张三").phone("123456").build(); //要将对象序列化,转成字符串,使用消息转换器MessageConverter rabbitTemplate.convertAndSend("211-FanoutExchage-01","",userRegisterOk); System.out.println("生产者生产-->张三注册成功"); } }
启动类
@SpringBootApplication public class App1 { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(App1.class, args); FanoutProducer producer = context.getBean(FanoutProducer.class); producer.sendMessage(); } }
🙉SpringBoot使用主题交换机(Topic Exchange )
消费者
路由键和绑定键之间是有规则的
- * 代表两点之间一个占位单词
- # 代表后面所有,匹配所有
@Component public class TopicConsumer { private static final String ENAME = "211-TopicExchage-01"; private static final String QNAME1 = "211-TopicQueue-刘亦菲"; private static final String QNAME2 = "211-TopicQueue-关晓彤"; private static final String QNAME3 = "211-TopicQueue-邓紫棋"; //定义一个交换机 @Bean public TopicExchange topicExchange(){ return new TopicExchange(ENAME, true, false); } //定义一个队列 @Bean public Queue topicQueue1(){ return QueueBuilder.durable(QNAME1).build(); } //定义一个队列 @Bean public Queue topicQueue2(){ return QueueBuilder.durable(QNAME2).build(); } //定义一个队列 @Bean public Queue topicQueue3(){ return QueueBuilder.durable(QNAME3).build(); } //创建队列和交换机的绑定关系 @Bean public Binding binding1(){ return BindingBuilder.bind(topicQueue1()).to(topicExchange()).with("1.9.*"); } //创建队列和交换机的绑定关系 @Bean public Binding binding2(){ return BindingBuilder.bind(topicQueue2()).to(topicExchange()).with("1.8.*"); } //创建队列和交换机的绑定关系 @Bean public Binding binding3(){ return BindingBuilder.bind(topicQueue3()).to(topicExchange()).with("1.#"); } //三个消费者 @RabbitHandler @RabbitListener(queues =QNAME1) public void process1(UserRegisterOk userRegisterOk){ System.out.println("刘亦菲收到:"+userRegisterOk.getName()+","+userRegisterOk.getHeight()); } @RabbitHandler @RabbitListener(queues =QNAME2) public void process2(UserRegisterOk userRegisterOk){ System.out.println("关晓彤收到:"+userRegisterOk.getName()+","+userRegisterOk.getHeight()); } @RabbitHandler @RabbitListener(queues =QNAME3) public void process3(UserRegisterOk userRegisterOk){ System.out.println("邓紫棋收到:"+userRegisterOk.getName()+","+userRegisterOk.getHeight()); } }
生产者
@Component public class TopicProducer { @Autowired private RabbitTemplate rabbitTemplate; public void sendMessage(){ UserRegisterOk userRegisterOk = UserRegisterOk.builder().name("moming").phone("123456").height("1.8.5").build(); //要将对象序列化,转成字符串,使用消息转换器MessageConverter rabbitTemplate.convertAndSend("211-TopicExchage-01",userRegisterOk.getHeight(),userRegisterOk); System.out.println("生产者生产-->moming"); } }
启动类
@SpringBootApplication public class App2 { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(App2.class, args); TopicProducer producer = context.getBean(TopicProducer.class); producer.sendMessage(); } }