RabbitMQ之路由模式RoutingKey模式
阿新 • • 發佈:2018-11-11
生產者傳送訊息到交換機並指定一個路由key,消費者佇列繫結到交換機時要制定路由key(key匹配就能接受訊息,key不匹配就不能接受訊息)
例如:我們可以把路由key設定為insert ,那麼消費者佇列key指定包含insert才可以接收訊息,消費者佇列key定義為update或者delete就不能接收訊息。很好的控制了更新,插入和刪除的操作。
採用交換機direct模式
前面做的demo中RoutingKey設定的空
zl介紹不為空的情況,如果不為空,那麼 經過訊息佇列之後,需要在經過 RoutingKey進行判斷決定 消費者
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.toov5.rabibitMQScribe</groupId> <artifactId>rabibitMQScribe</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>3.6.5</version> </dependency> </dependencies> </project>
producer
package com.toov5.routing; import java.io.IOException; import java.util.concurrent.TimeoutException; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.toov5.utils.MQConnectionUtils; //生產者 交換機型別 producerFanout型別 public class RoutingProducer {//交換機名稱 private static final String EXCHANGE_NAME = "my_routing"; public static void main(String[] args) throws IOException, TimeoutException { //建立MQ連線 Connection connection = MQConnectionUtils.newConnection(); //建立通道 Channel channel = connection.createChannel(); //生產者繫結交換機 channel.exchangeDeclare(EXCHANGE_NAME, "direct"); //交換機名稱 交換機型別 String routingKey="email"; //建立對應的訊息 String msString = "my_Routing_destination_msg"+routingKey; //通過頻道 傳送訊息 System.out.println("生產者投遞訊息:"+msString); channel.basicPublish(EXCHANGE_NAME, routingKey, null, msString.getBytes()); //關閉通道 和 連線 channel.close(); connection.close(); } }
consumer:
package com.toov5.routing; import java.io.IOException; import java.util.concurrent.TimeoutException; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.DefaultConsumer; import com.rabbitmq.client.Envelope; import com.rabbitmq.client.AMQP.BasicProperties; import com.toov5.utils.MQConnectionUtils; //郵件消費者 public class ConsumerEmailRouting { private static final String EMAIL_QUEUE ="email_queue_routing"; //交換機名稱 private static final String EXCHANGE_NAME = "my_routing"; public static void main(String[] args) throws IOException, TimeoutException { System.out.println("郵件消費者啟動"); //建立MQ連線 Connection connection = MQConnectionUtils.newConnection(); //建立通道 Channel channel = connection.createChannel(); //消費者宣告佇列 channel.queueDeclare(EMAIL_QUEUE, false, false, false, null); //消費者佇列繫結 路由 channel.queueBind(EMAIL_QUEUE, EXCHANGE_NAME, "email"); //消費者監聽訊息 DefaultConsumer defaultConsumer = new DefaultConsumer(channel) { //重寫監聽方法 @Override public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException { String msg = new String(body,"UTF-8"); System.out.println("郵件消費者獲取生產者訊息"+msg); } }; channel.basicConsume(EMAIL_QUEUE,true, defaultConsumer); //繫結佇列 事件監聽 } }
package com.toov5.routing; import java.io.IOException; import java.util.concurrent.TimeoutException; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.DefaultConsumer; import com.rabbitmq.client.Envelope; import com.rabbitmq.client.AMQP.BasicProperties; import com.toov5.utils.MQConnectionUtils; //郵件消費者 public class ConsumerSMSRouting { private static final String SMS_QUEUE ="sms_queue_routing"; //交換機名稱 private static final String EXCHANGE_NAME = "my_routing"; public static void main(String[] args) throws IOException, TimeoutException { System.out.println("簡訊消費者啟動"); //建立MQ連線 Connection connection = MQConnectionUtils.newConnection(); //建立通道 Channel channel = connection.createChannel(); //消費者宣告佇列 channel.queueDeclare(SMS_QUEUE, false, false, false, null); //消費者佇列繫結 路由 channel.queueBind(SMS_QUEUE, EXCHANGE_NAME, "email"); channel.queueBind(SMS_QUEUE, EXCHANGE_NAME, "sms"); //消費者監聽訊息 DefaultConsumer defaultConsumer = new DefaultConsumer(channel) { //重寫監聽方法 @Override public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException { String msg = new String(body,"UTF-8"); System.out.println("郵件消費者獲取生產者訊息"+msg); } }; channel.basicConsume(SMS_QUEUE,true, defaultConsumer); //繫結佇列 事件監聽 } }
執行結果:
繫結兩個: