日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

rabbitmq direct 多个消费者_一文解析 RabbitMQ 最常用的三大模式

發布時間:2023/12/20 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 rabbitmq direct 多个消费者_一文解析 RabbitMQ 最常用的三大模式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Direct 模式

  • 所有發送到 Direct Exchange 的消息被轉發到 RouteKey 中指定的 Queue。
  • Direct 模式可以使用 RabbitMQ 自帶的 Exchange: default Exchange,所以不需要將 Exchange 進行任何綁定(binding)操作。
  • 消息傳遞時,RouteKey 必須完全匹配才會被隊列接收,否則該消息會被拋棄,

import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory;public class DirectProducer {public static void main(String[] args) throws Exception {//1. 創建一個 ConnectionFactory 并進行設置ConnectionFactory factory = new ConnectionFactory();factory.setHost("localhost");factory.setVirtualHost("/");factory.setUsername("guest");factory.setPassword("guest");//2. 通過連接工廠來創建連接Connection connection = factory.newConnection();//3. 通過 Connection 來創建 ChannelChannel channel = connection.createChannel();//4. 聲明String exchangeName = "test_direct_exchange";String routingKey = "item.direct";//5. 發送String msg = "this is direct msg";channel.basicPublish(exchangeName, routingKey, null, msg.getBytes());System.out.println("Send message : " + msg);//6. 關閉連接channel.close();connection.close();} } import com.rabbitmq.client.*; import java.io.IOException;public class DirectConsumer {public static void main(String[] args) throws Exception {//1. 創建一個 ConnectionFactory 并進行設置ConnectionFactory factory = new ConnectionFactory();factory.setHost("localhost");factory.setVirtualHost("/");factory.setUsername("guest");factory.setPassword("guest");factory.setAutomaticRecoveryEnabled(true);factory.setNetworkRecoveryInterval(3000);//2. 通過連接工廠來創建連接Connection connection = factory.newConnection();//3. 通過 Connection 來創建 ChannelChannel channel = connection.createChannel();//4. 聲明String exchangeName = "test_direct_exchange";String queueName = "test_direct_queue";String routingKey = "item.direct";channel.exchangeDeclare(exchangeName, "direct", true, false, null);channel.queueDeclare(queueName, false, false, false, null);//一般不用代碼綁定,在管理界面手動綁定channel.queueBind(queueName, exchangeName, routingKey);//5. 創建消費者并接收消息Consumer consumer = new DefaultConsumer(channel) {@Overridepublic void handleDelivery(String consumerTag, Envelope envelope,AMQP.BasicProperties properties, byte[] body)throws IOException {String message = new String(body, "UTF-8");System.out.println(" [x] Received '" + message + "'");}};//6. 設置 Channel 消費者綁定隊列channel.basicConsume(queueName, true, consumer);} } Send message : this is direct msg[x] Received 'this is direct msg' Topic 模式

Topic 模式

可以使用通配符進行模糊匹配

  • 符號'#" 匹配一個或多個詞
  • 符號"*”匹配不多不少一個詞

例如:

  • 'log.#"能夠匹配到'log.info.oa"
  • "log.*"只會匹配到"log.erro“

import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory;public class TopicProducer {public static void main(String[] args) throws Exception {//1. 創建一個 ConnectionFactory 并進行設置ConnectionFactory factory = new ConnectionFactory();factory.setHost("localhost");factory.setVirtualHost("/");factory.setUsername("guest");factory.setPassword("guest");//2. 通過連接工廠來創建連接Connection connection = factory.newConnection();//3. 通過 Connection 來創建 ChannelChannel channel = connection.createChannel();//4. 聲明String exchangeName = "test_topic_exchange";String routingKey1 = "item.update";String routingKey2 = "item.delete";String routingKey3 = "user.add";//5. 發送String msg = "this is topic msg";channel.basicPublish(exchangeName, routingKey1, null, msg.getBytes());channel.basicPublish(exchangeName, routingKey2, null, msg.getBytes());channel.basicPublish(exchangeName, routingKey3, null, msg.getBytes());System.out.println("Send message : " + msg);//6. 關閉連接channel.close();connection.close();} } import com.rabbitmq.client.*; import java.io.IOException;public class TopicConsumer {public static void main(String[] args) throws Exception {//1. 創建一個 ConnectionFactory 并進行設置ConnectionFactory factory = new ConnectionFactory();factory.setHost("localhost");factory.setVirtualHost("/");factory.setUsername("guest");factory.setPassword("guest");factory.setAutomaticRecoveryEnabled(true);factory.setNetworkRecoveryInterval(3000);//2. 通過連接工廠來創建連接Connection connection = factory.newConnection();//3. 通過 Connection 來創建 ChannelChannel channel = connection.createChannel();//4. 聲明String exchangeName = "test_topic_exchange";String queueName = "test_topic_queue";String routingKey = "item.#";channel.exchangeDeclare(exchangeName, "topic", true, false, null);channel.queueDeclare(queueName, false, false, false, null);//一般不用代碼綁定,在管理界面手動綁定channel.queueBind(queueName, exchangeName, routingKey);//5. 創建消費者并接收消息Consumer consumer = new DefaultConsumer(channel) {@Overridepublic void handleDelivery(String consumerTag, Envelope envelope,AMQP.BasicProperties properties, byte[] body)throws IOException {String message = new String(body, "UTF-8");System.out.println(" [x] Received '" + message + "'");}};//6. 設置 Channel 消費者綁定隊列channel.basicConsume(queueName, true, consumer);} } Send message : this is topc msg[x] Received 'this is topc msg' [x] Received 'this is topc msg'

Fanout 模式

不處理路由鍵,只需要簡單的將隊列綁定到交換機上發送到交換機的消息都會被轉發到與該交換機綁定的所有隊列上。
Fanout交換機轉發消息是最快的。

import com.rabbitmq.client.*; import java.io.IOException;public class FanoutConsumer {public static void main(String[] args) throws Exception {//1. 創建一個 ConnectionFactory 并進行設置ConnectionFactory factory = new ConnectionFactory();factory.setHost("localhost");factory.setVirtualHost("/");factory.setUsername("guest");factory.setPassword("guest");factory.setAutomaticRecoveryEnabled(true);factory.setNetworkRecoveryInterval(3000);//2. 通過連接工廠來創建連接Connection connection = factory.newConnection();//3. 通過 Connection 來創建 ChannelChannel channel = connection.createChannel();//4. 聲明String exchangeName = "test_fanout_exchange";String queueName = "test_fanout_queue";String routingKey = "item.#";channel.exchangeDeclare(exchangeName, "fanout", true, false, null);channel.queueDeclare(queueName, false, false, false, null);//一般不用代碼綁定,在管理界面手動綁定channel.queueBind(queueName, exchangeName, routingKey);//5. 創建消費者并接收消息Consumer consumer = new DefaultConsumer(channel) {@Overridepublic void handleDelivery(String consumerTag, Envelope envelope,AMQP.BasicProperties properties, byte[] body)throws IOException {String message = new String(body, "UTF-8");System.out.println(" [x] Received '" + message + "'");}};//6. 設置 Channel 消費者綁定隊列channel.basicConsume(queueName, true, consumer);} } import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; public class FanoutProducer {public static void main(String[] args) throws Exception {//1. 創建一個 ConnectionFactory 并進行設置ConnectionFactory factory = new ConnectionFactory();factory.setHost("localhost");factory.setVirtualHost("/");factory.setUsername("guest");factory.setPassword("guest");//2. 通過連接工廠來創建連接Connection connection = factory.newConnection();//3. 通過 Connection 來創建 ChannelChannel channel = connection.createChannel();//4. 聲明String exchangeName = "test_fanout_exchange";String routingKey1 = "item.update";String routingKey2 = "";String routingKey3 = "ookjkjjkhjhk";//任意routingkey//5. 發送String msg = "this is fanout msg";channel.basicPublish(exchangeName, routingKey1, null, msg.getBytes());channel.basicPublish(exchangeName, routingKey2, null, msg.getBytes());channel.basicPublish(exchangeName, routingKey3, null, msg.getBytes());System.out.println("Send message : " + msg);//6. 關閉連接channel.close();connection.close();} } Send message : this is fanout msg[x] Received 'this is fanout msg' [x] Received 'this is fanout msg' [x] Received 'this is fanout msg' 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的rabbitmq direct 多个消费者_一文解析 RabbitMQ 最常用的三大模式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。