1.Fanout Exchange介绍

Fanout Exchange 消息广播的模式,不管路由键或者是路由模式,会把消息发给绑定给它的全部队列,如果配置了routing_key会被忽略。
如上图所示,即当使用fanout交换器时,他会将消息广播到与该交换器绑定的所有队列上,这有利于你对单条消息做不同的反应。
例如存在以下场景:一个web服务要在用户完善信息时,获得积分奖励,这样你就可以创建两个对列,一个用来处理用户信息的请求,另一个对列获取这条消息是来完成积分奖励的任务。
2.代码示例
1).Queue配置类
FanoutRabbitConfig.java类:
package com.example.rabbitmqfanout;
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;
@Configuration
public class FanoutRabbitConfig {
//创建队列
@Bean
public Queue AMessage() {
return new Queue("fanout.A");
}
//创建队列
@Bean
public Queue BMessage() {
return new Queue("fanout.B");
}
//创建队列
@Bean
public Queue CMessage() {
return new Queue("fanout.C");
}
//创建Fanout交换器
@Bean
FanoutExchange fanoutExchange() {
return new FanoutExchange("fanoutExchange");
}
//将对列绑定到Fanout交换器
@Bean
Binding bindingExchangeA(Queue AMessage,FanoutExchange fanoutExchange) {
return BindingBuilder.bind(AMessage).to(fanoutExchange);
}
//将对列绑定到Fanout交换器
@Bean
Binding bindingExchangeB(Queue BMessage, FanoutExchange fanoutExchange) {
return BindingBuilder.bind(BMessage).to(fanoutExchange);
}
//将对列绑定到Fanout交换器
@Bean
Binding bindingExchangeC(Queue CMessage, FanoutExchange fanoutExchange) {
return BindingBuilder.bind(CMessage).to(fanoutExchange);
}
}
2).消息生产者
FanoutSender.java类:
package com.example.rabbitmqfanout.rabbitmq;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class FanoutSender {
@Autowired
private AmqpTemplate rabbitTemplate;
public void send() {
String context = "hi, fanout msg ";
System.out.println("Sender : " + context);
this.rabbitTemplate.convertAndSend("fanoutExchange","", context);
}
}
3).消息消费者
FanoutReceiverA.java类:
package com.example.rabbitmqfanout.rabbitmq;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "fanout.A")
public class FanoutReceiverA {
@RabbitHandler
public void process(String message) {
System.out.println("fanout Receiver A : " + message);
}
}
FanoutReceiverB.java类:
package com.example.rabbitmqfanout.rabbitmq;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "fanout.B")
public class FanoutReceiverB {
@RabbitHandler
public void process(String message) {
System.out.println("fanout Receiver B: " + message);
}
}
FanoutReceiverC.java类:
package com.example.rabbitmqfanout.rabbitmq;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "fanout.C")
public class FanoutReceiverC {
@RabbitHandler
public void process(String message) {
System.out.println("fanout Receiver C: " + message);
}
}
4).测试
FanoutTest.java类:
package com.example.rabbitmqfanout.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 FanoutTest {
@Autowired
private FanoutSender sender;
@Test
public void fanoutSender() throws Exception {
sender.send();
}
}
以上所述是小编给大家介绍的spring boot整合RabbitMQ(Fanout模式),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
# springboot
# rabbitmq
# Spring Boot+RabbitMQ 通过fanout模式实现消息接收功能(支持消费者多实例部署
# C#使用RabbitMq队列(Sample
# Work
# Fanout
# Direct等模式的简单使用)
# SpringBoot实现RabbitMQ三种使用方式
# 详解rabbitmq使用springboot实现fanout模式
# 绑定
# 交换器
# 将对
# 小编
# 在此
# 要在
# 这条
# 给大家
# 或者是
# 你对
# 所示
# 会把
# 会将
# 你就可以
# 来完成
# 所述
# 给我留言
# 感谢大家
# 与该
# 这有
相关文章:
建站主机是否属于云主机类型?
网站制作哪家好,cc、.co、.cm哪个域名更适合做网站?
如何快速搭建安全的FTP站点?
零服务器AI建站解决方案:快速部署与云端平台低成本实践
小型网站建站如何选择虚拟主机?
建站主机数据库如何配置才能提升网站性能?
网站制作大概要多少钱一个,做一个平台网站大概多少钱?
网站制作公司排行榜,抖音怎样做个人官方网站
rsync同步时出现rsync: failed to set times on “xxxx”: Operation not permitted
制作网站的网址是什么,请问后缀为.com和.com.cn还有.cn的这三种网站是分别是什么类型的网站?
如何快速搭建高效WAP手机网站吸引移动用户?
如何高效完成独享虚拟主机建站?
如何配置FTP站点权限与安全设置?
建站之星2.7模板:企业网站建设与h5定制设计专题
建站之星后台管理系统如何操作?
香港服务器如何优化才能显著提升网站加载速度?
如何选择高效响应式自助建站源码系统?
如何构建满足综合性能需求的优质建站方案?
高防服务器租用首荐平台,企业级优惠套餐快速部署
建站三合一如何选?哪家性价比更高?
如何在服务器上配置二级域名建站?
企业网站制作公司网页,推荐几家专业的天津网站制作公司?
如何自定义建站之星模板颜色并下载新样式?
如何快速使用云服务器搭建个人网站?
建站之星好吗?新手能否轻松上手建站?
h5网站制作工具有哪些,h5页面制作工具有哪些?
5种Android数据存储方式汇总
如何在阿里云域名上完成建站全流程?
免费网站制作模板下载,除了易企秀之外还有什么H5平台可以制作H5长页面,最好是免费的?
网站制作难吗安全吗,做一个网站需要多久时间?
如何通过WDCP绑定主域名及创建子域名站点?
如何在IIS7上新建站点并设置安全权限?
Python如何创建带属性的XML节点
建站之星五站合一营销型网站搭建攻略,流量入口全覆盖优化指南
如何选择可靠的免备案建站服务器?
广平建站公司哪家专业可靠?如何选择?
如何通过商城免费建站系统源码自定义网站主题?
,在苏州找工作,上哪个网站比较好?
七夕网站制作视频,七夕大促活动怎么报名?
制作农业网站的软件,比较好的农业网站推荐一下?
,怎么在广州志愿者网站注册?
网站企业制作流程,用什么语言做企业网站比较好?
如何用PHP快速搭建CMS系统?
如何选择适合PHP云建站的开源框架?
如何在橙子建站中快速调整背景颜色?
如何通过网站建站时间优化SEO与用户体验?
武汉外贸网站制作公司,现在武汉外贸前景怎么样啊?
如何处理“XML格式不正确”错误 常见XML well-formed问题解决方法
济南专业网站制作公司,济南信息工程学校怎么样?
C++ static_cast和dynamic_cast区别_C++静态转换与动态类型安全转换
*请认真填写需求信息,我们会在24小时内与您取得联系。