当前位置:网站首页>交换机Exchanges
交换机Exchanges
2022-07-23 04:28:00 【一个风轻云淡】
Exchanges概念
RabbitMQ消息传递模型的核心思想是: 生产者生产的消息从不会直接发送到队列。实际上,通常生产者甚至都不知道这些消息传递传递到了哪些队列中。
相反,生产者只能将消息发送到交换机(exchange),交换机工作的内容非常简单,一方面它接收来自生产者的消息,另一方面将它们推入队列。交换机必须确切知道如何处理收到的消息。是应该把这些消息放到特定队列还是说把他们到许多队列中还是说应该丢弃它们。这就的由交换机的类型来决定。
Exchanges的类型
总共有以下类型:
直接(direct), 主题(topic) ,标题(headers) , 扇出(fanout)
无名exchange
前面部分我们对exchange一无所知,但仍然能够将消息发送到队列。之前能实现的原因是因为我们使用的是默认交换,我们通过空字符串(“”)进行标识。

第一个参数是交换机的名称。空字符串表示默认或无名称交换机:消息能路由发送到队列中其实是由routingKey(bindingkey)绑定key指定的,如果它存在的话
临时队列
队列的名称我们来说至关重要-我们需要指定我们的消费者去消费哪个队列的消息。
每当我们连接到Rabbit时,我们都需要一个全新的空队列,为此我们可以创建一个具有随机名称的队列,或者能让服务器为我们选择一个随机队列名称那就更好了。其次一旦我们断开了消费者的连接,队列将被自动删除。
创建临时队列的方式如下:
String queueName = channel.queueDeclare().getQueue();
创建出来之后长成这样:
绑定(bindings)
什么是bingding呢,binding其实是exchange和queue之间的桥梁,它告诉我们exchange和那个队列进行了绑定关系。比如说下面这张图告诉我们的就是X与Q1和Q2进行了绑定
Fanout
Fanout介绍
Fanout这种类型非常简单。正如从名称中猜到的那样,它是将接收到的所有消息广播到它知道的所有队列中。系统中默认有些exchange类型

Fanout实战
工具类:
public class untils {
public static Channel getChannel() throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.231.132");
factory.setUsername("admin");
factory.setPassword("123");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
return channel;
}
}生产者:
public class EmitLog {
private static final String EXCHNGE_NAME="logs";
public static void main(String[] args) throws Exception{
/**
* 声明一个交换机
* 1.交换机的类型
* 2.exchang的类型
*/
Channel channel = untils.getChannel();
channel.exchangeDeclare(EXCHNGE_NAME,"fanout");
Scanner sc=new Scanner(System.in);
System.out.println("请输入消息");
while (sc.hasNext()){
String message=sc.nextLine();
channel.basicPublish(EXCHNGE_NAME,"",null,message.getBytes(StandardCharsets.UTF_8));
System.out.println("发送的消息是"+message);
}
}
}消费者:(开启俩个线程)
public class ReceiveLogs {
private static final String EXCHANGE_NAME="logs";
public static void main(String[] args) throws Exception {
Channel channel = untils.getChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
/**
* 生成一个临时的队列,队列的名称是随机的
* 当消费者断开和该队列连接时,队列自动删除
*/
String queue = channel.queueDeclare().getQueue();
//把该临时队列绑定我们的exchange,其中routingkey(也称为bindingkey)
channel.queueBind(queue, EXCHANGE_NAME, "");
System.out.println("等待接受消息....");
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println("控制台打印接收到的消息"+message); };
channel.basicConsume(queue,true,deliverCallback,consumerTag -> {});
}
}效果:


俩个消费者

边栏推荐
- 阿里云如何将一个域名解析到另一个域名上
- Antlr4 introductory learning (I): Download and test
- Kingbasees SQL language reference manual of Jincang database (8. Function (2))
- Kingbasees SQL language reference manual of Jincang database (8. Function (8))
- [qt5.12] qt5.12 installation tutorial
- MySQL查询优化-详解
- 序列模型(二)- 自然语言处理与词嵌套
- Idea integrated sonar complete process
- CV (3)- CNNs
- Basic process of dpdk cross compilation
猜你喜欢
随机推荐
No routines, no traps, no advertisements | are you sure you don't need this free instant messaging software?
数仓:工作流的设计以及优化实践
Chrome selenium uses the default profile without emptying it every time
Performance introduction
无套路、无陷阱、无广告 | 这个免费的即时通讯软件确定不用吗?
kex_ exchange_ Identification: read: connection reset by peer imperfect solution (one)
元宇宙浪潮震撼来袭,抓住时机,齐心协力
Self operation and maintenance: a new sample of it operation and maintenance services in Colleges and Universities
【Warning】YOLOV5训练时的ignoring corrupt image/label: [Errno 2].....,无法全部训练数据集,快速带你解决它
第四篇章:运行时数据区——共享空间
Interest rate in installment payment
toco生成tflite模型
分期付款中的利率问题
LeetCode每日一题(1946. Largest Number After Mutating Substring)
New file / filter / folder in VS
What is instant messaging? Development of instant messaging
赛尔运维:高校IT运维服务新样本
22. Notes on the use of combobox in WPF
HoloLens第三视角开发【保姆级教程】【踩坑记录】
写驱动程序的时候warning LNK4210报错









