1、交换机Exchanges
1.1 基本概念
1.1.1 Exchanges 概念1.1.2 Exchanges 的类型1.1.3 无名 exchange 1.2 临时队列1.3 绑定(bindings)1.4 Fanout1.5 Direct exchange1.6 Topics
要求:匹配案例 1、交换机Exchanges
1.1 基本概念 1.1.1 Exchanges 概念我们将消息传达给多个消费者。这种模式称为 ”发布/订阅"
1.1.2 Exchanges 的类型RabbitMQ 消息传递模型的核心思想是: 生产者生产的消息从不会直接发送到队列。实际上,通常生产者甚至都不知道这些消息传递传递到了哪些队列中。相反,生产者只能将消息发送到交换机(exchange),交换机工作的内容非常简单,一方面它接收来自生产者的消息,另一方面将它们推入队列。交换机必须确切知道如何处理收到的消息。是应该把这些消息放到特定队列还是说把他们到许多队列中还是说应该丢弃它们。这就的由交换机的类型来决定。
直接(direct), 主题(topic) ,标题(headers) , 扇出(fanout)
1.1.3 无名 exchange 之前能实现的
原因是因为我们使用的是默认交换,我们通过空字符串(“”)进行标识。
第一个参数是交换机的名称。空字符串表示默认或无名称交换机:消息能路由发送到队列中其实是由 routingKey(bindingkey)绑定 key 指定的,如果它存在的话
每当我们连接到 Rabbit 时,我们都需要一个全新的空队列,为此我们可以创建一个具有随机名称的队列,或者能让服务器为我们选择一个随机队列名称那就更好了。其次一旦我们断开了消费者的连接,队列将被自动删除。
创建临时队列的方式如下:
String queueName = channel.queueDeclare().getQueue();
1.3 绑定(bindings)1.4 Fanoutbinding 其实是 exchange 和 queue 之间的桥梁,它告诉我们 exchange 和那个队列进行了绑定关系
作用:它是将接收到的所有消息广播到它知道的所有队列中
消费者:
package com.ys.rabbitmq.five;import com.rabbitmq.client.Channel;import com.rabbitmq.client.DeliverCallback;import com.ys.rabbitmq.utils.RabbitMqUtils;public class ReceiveLogs01{ private static final String EXCHANGE_NAME = "logs"; public static void main(String[] argv) throws Exception { Channel channel= RabbitMqUtils.getChannel(); String queueName=channel.queueDeclare().getQueue(); channel.queueBind(queueName,EXCHANGE_NAME,""); System.out.println("01等待接收消息:"); DeliverCallback deliverCallback = (consumerTag, delivery) -> { String message = new String(delivery.getBody(), "UTF-8"); System.out.println("01控制台打印接收到的消息"+message); }; channel.basicConsume(queueName, true, deliverCallback, consumerTag -> { }); }}
package com.ys.rabbitmq.five;import com.rabbitmq.client.Channel;import com.rabbitmq.client.DeliverCallback;import com.ys.rabbitmq.utils.RabbitMqUtils;public class ReceiveLogs02{ private static final String EXCHANGE_NAME = "logs"; public static void main(String[] argv) throws Exception { Channel channel= RabbitMqUtils.getChannel(); String queueName=channel.queueDeclare().getQueue(); channel.queueBind(queueName,EXCHANGE_NAME,""); System.out.println("02等待接收消息:"); DeliverCallback deliverCallback = (consumerTag, delivery) -> { String message = new String(delivery.getBody(), "UTF-8"); System.out.println("02控制台打印接收到的消息"+message); }; channel.basicConsume(queueName, true, deliverCallback, consumerTag -> { }); }}
生产者:
package com.ys.rabbitmq.five;import com.rabbitmq.client.Channel;import com.ys.rabbitmq.utils.RabbitMqUtils;import java.util.Scanner;public class EmitLog{ private static final String EXCHANGE_NAME = "logs"; public static void main(String[] argv) throws Exception { Channel channel = RabbitMqUtils.getChannel(); channel.exchangeDeclare(EXCHANGE_NAME,"fanout"); Scanner scanner=new Scanner(System.in); System.out.println("请输入:"); while (scanner.hasNext()) { String s=scanner.nextLine(); channel.basicPublish(EXCHANGE_NAME,"",null,s.getBytes("UTF-8")); System.out.println(s+" 消息已发送"); } }}
1.5 Direct exchange结论:两个消费者都接收到了同样的消息 符合发布订阅模式
例如我们希望将日志消息写入磁盘的程序仅接收严重错误(errros),而不存储哪些警告(warning)或信息(info)日志消息避免浪费磁盘空间。Fanout 这种交换类型并不能给我们带来很大的灵活性-它只能进行无意识的广播,在这里我们将使用 direct 这种类型来进行替换,这种类型的工作方式是,消息只去到它绑定的routingKey 队列中去。
public class ReceiveLogsDirect01 { private static final String EXCHANGE_NAME = "direct_logs"; public static void main(String[] argv) throws Exception { Channel channel = RabbitUtils.getChannel(); channel.exchangeDeclare(EXCHANGE_NAME,BuiltinExchangeType.DIRECT); String queueName = "disk"; channel.queueDeclare(queueName, false, false, false, null); channel.queueBind(queueName, EXCHANGE_NAME, "error"); System.out.println("等待接收消息....."); DeliverCallback deliverCallback = (consumerTag, delivery) -> { String message = new String(delivery.getBody(), "UTF-8"); message="接收绑定键:"+delivery.getEnvelope().getRoutingKey()+",消息:"+message; File file = new File("C:\work\rabbitmq_info.txt"); FileUtils.writeStringToFile(file,message,"UTF-8"); System.out.println("错误日志已经接收"); }; channel.basicConsume(queueName, true, deliverCallback, consumerTag -> { }); }}
public class ReceiveLogsDirect02 { private static final String EXCHANGE_NAME = "direct_logs"; public static void main(String[] argv) throws Exception { Channel channel = RabbitUtils.getChannel(); channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT); String queueName = "console"; channel.queueDeclare(queueName, false, false, false, null); channel.queueBind(queueName, EXCHANGE_NAME, "info"); channel.queueBind(queueName, EXCHANGE_NAME, "warning"); System.out.println("等待接收消息....."); DeliverCallback deliverCallback = (consumerTag, delivery) -> { String message = new String(delivery.getBody(), "UTF-8"); System.out.println(" 接收绑定键 :"+delivery.getEnvelope().getRoutingKey()+", 消息:"+message); }; channel.basicConsume(queueName, true, deliverCallback, consumerTag -> { }); }}
public class EmitLogDirect { private static final String EXCHANGE_NAME = "direct_logs"; public static void main(String[] argv) throws Exception { try (Channel channel = RabbitUtils.getChannel()) { channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT); //创建多个 bindingKey Map
要求:尽管使用 direct 交换机改进了我们的系统,但是它仍然存在局限性-比方说我们想接收的日志类型有info.base 和 info.advantage,某个队列只想 info.base 的消息,那这个时候 direct 就办不到了。这个时候就只能使用 topic 类型
匹配案例发送到类型是 topic 交换机的消息的 routing_key 不能随意写,必须满足一定的要求,它必须是一个单词列表,以点号分隔开。这些单词可以是任意单词,比如说:“stock.usd.nyse”, “nyse.vmw”,“quick.orange.rabbit”.这种类型的。当然这个单词列表最多不能超过 255 个字节。在这个规则列表中,
其中有两个替换符是大家需要注意的*(星号)可以代替一个单词,#(井号)可以替代零个或多个单词
quick.orange.rabbit 被队列 Q1Q2 接收到lazy.orange.elephant 被队列 Q1Q2 接收到quick.orange.fox 被队列 Q1 接收到lazy.brown.fox 被队列 Q2 接收到lazy.pink.rabbit 虽然满足两个绑定但只被队列 Q2 接收一次quick.brown.fox 不匹配任何绑定不会被任何队列接收到会被丢弃quick.orange.male.rabbit 是四个单词不匹配任何绑定会被丢弃lazy.orange.male.rabbit 是四个单词但匹配 Q2
当队列绑定关系是下列这种情况时需要引起注意当一个队列绑定键是#,那么这个队列将接收所有数据,就有点像 fanout 了,如果队列绑定键当中没有#和*出现,那么该队列绑定类型就是 direct 了
消费者:package com.ys.rabbitmq.seven;import com.rabbitmq.client.BuiltinExchangeType;import com.rabbitmq.client.Channel;import com.rabbitmq.client.DeliverCallback;import com.ys.rabbitmq.utils.RabbitMqUtils;public class ReceiveTopic01{ public static final String EXCHANGE_NAME="topic_logs"; public static void main(String[] args) throws Exception{ Channel channel = RabbitMqUtils.getChannel(); //声明交换机 channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC); //声明队列 String queueName="Q1"; channel.queueDeclare(queueName,false,false,false,null); channel.queueBind(queueName,EXCHANGE_NAME,"*.orange.*"); System.out.println("等待接收消息"); DeliverCallback deliverCallback=(consumerTag,message)->{ System.out.println(queueName+"/tRoutingKey:"+message.getEnvelope().getRoutingKey()+" /t接收到:"+new String(message.getBody(), "UTF-8")); }; channel.basicConsume(queueName,true,deliverCallback,consumerTag->{}); }}
消费者package com.ys.rabbitmq.seven;import com.rabbitmq.client.BuiltinExchangeType;import com.rabbitmq.client.Channel;import com.rabbitmq.client.DeliverCallback;import com.ys.rabbitmq.utils.RabbitMqUtils;public class ReceiveTopic02{ public static final String EXCHANGE_NAME="topic_logs"; public static void main(String[] args) throws Exception{ Channel channel = RabbitMqUtils.getChannel(); //声明交换机 channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC); //声明队列 String queueName="Q2"; channel.queueDeclare(queueName,false,false,false,null); channel.queueBind(queueName,EXCHANGE_NAME,"*.*.rabbit"); channel.queueBind(queueName,EXCHANGE_NAME,"lazy.#"); System.out.println("等待接收消息"); DeliverCallback deliverCallback=(consumerTag, message)->{ System.out.println(queueName+"/tRoutingKey:"+message.getEnvelope().getRoutingKey()+" /t接收到:"+new String(message.getBody(), "UTF-8")); }; channel.basicConsume(queueName,true,deliverCallback,consumerTag->{}); }}
生产者:package com.ys.rabbitmq.seven;import com.rabbitmq.client.BuiltinExchangeType;import com.rabbitmq.client.Channel;import com.ys.rabbitmq.utils.RabbitMqUtils;import java.util.HashMap;import java.util.Map;import java.util.Scanner;public class EmitLog{ private static final String EXCHANGE_NAME = "topic_logs"; public static void main(String[] argv) throws Exception { Channel channel = RabbitMqUtils.getChannel(); channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC); Map