RabbitMQ 最常用的三大模式實例解析
這篇文章主要介紹了RabbitMQ 最常用的三大模式實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
Direct 模式
- 所有發(fā)送到 Direct Exchange 的消息被轉(zhuǎn)發(fā)到 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. 創(chuàng)建一個 ConnectionFactory 并進行設(shè)置 ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); factory.setVirtualHost("/"); factory.setUsername("guest"); factory.setPassword("guest"); //2. 通過連接工廠來創(chuàng)建連接 Connection connection = factory.newConnection(); //3. 通過 Connection 來創(chuàng)建 Channel Channel channel = connection.createChannel(); //4. 聲明 String exchangeName = "test_direct_exchange"; String routingKey = "item.direct"; //5. 發(fā)送 String msg = "this is direct msg"; channel.basicPublish(exchangeName, routingKey, null, msg.getBytes()); System.out.println("Send message : " + msg); //6. 關(guān)閉連接 channel.close(); connection.close(); } }
import com.rabbitmq.client.*; import java.io.IOException; public class DirectConsumer { public static void main(String[] args) throws Exception { //1. 創(chuàng)建一個 ConnectionFactory 并進行設(shè)置 ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); factory.setVirtualHost("/"); factory.setUsername("guest"); factory.setPassword("guest"); factory.setAutomaticRecoveryEnabled(true); factory.setNetworkRecoveryInterval(3000); //2. 通過連接工廠來創(chuàng)建連接 Connection connection = factory.newConnection(); //3. 通過 Connection 來創(chuàng)建 Channel Channel 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. 創(chuàng)建消費者并接收消息 Consumer consumer = new DefaultConsumer(channel) { @Override public 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. 設(shè)置 Channel 消費者綁定隊列 channel.basicConsume(queueName, true, consumer); } }
Send message : this is direct msg [x] Received 'this is direct msg'
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. 創(chuàng)建一個 ConnectionFactory 并進行設(shè)置 ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); factory.setVirtualHost("/"); factory.setUsername("guest"); factory.setPassword("guest"); //2. 通過連接工廠來創(chuàng)建連接 Connection connection = factory.newConnection(); //3. 通過 Connection 來創(chuàng)建 Channel Channel channel = connection.createChannel(); //4. 聲明 String exchangeName = "test_topic_exchange"; String routingKey1 = "item.update"; String routingKey2 = "item.delete"; String routingKey3 = "user.add"; //5. 發(fā)送 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. 關(guān)閉連接 channel.close(); connection.close(); } }
import com.rabbitmq.client.*; import java.io.IOException; public class TopicConsumer { public static void main(String[] args) throws Exception { //1. 創(chuàng)建一個 ConnectionFactory 并進行設(shè)置 ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); factory.setVirtualHost("/"); factory.setUsername("guest"); factory.setPassword("guest"); factory.setAutomaticRecoveryEnabled(true); factory.setNetworkRecoveryInterval(3000); //2. 通過連接工廠來創(chuàng)建連接 Connection connection = factory.newConnection(); //3. 通過 Connection 來創(chuàng)建 Channel Channel 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. 創(chuàng)建消費者并接收消息 Consumer consumer = new DefaultConsumer(channel) { @Override public 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. 設(shè)置 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 模式
不處理路由鍵,只需要簡單的將隊列綁定到交換機上發(fā)送到交換機的消息都會被轉(zhuǎn)發(fā)到與該交換機綁定的所有隊列上。
Fanout交換機轉(zhuǎn)發(fā)消息是最快的。
import com.rabbitmq.client.*; import java.io.IOException; public class FanoutConsumer { public static void main(String[] args) throws Exception { //1. 創(chuàng)建一個 ConnectionFactory 并進行設(shè)置 ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); factory.setVirtualHost("/"); factory.setUsername("guest"); factory.setPassword("guest"); factory.setAutomaticRecoveryEnabled(true); factory.setNetworkRecoveryInterval(3000); //2. 通過連接工廠來創(chuàng)建連接 Connection connection = factory.newConnection(); //3. 通過 Connection 來創(chuàng)建 Channel Channel 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. 創(chuàng)建消費者并接收消息 Consumer consumer = new DefaultConsumer(channel) { @Override public 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. 設(shè)置 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. 創(chuàng)建一個 ConnectionFactory 并進行設(shè)置 ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); factory.setVirtualHost("/"); factory.setUsername("guest"); factory.setPassword("guest"); //2. 通過連接工廠來創(chuàng)建連接 Connection connection = factory.newConnection(); //3. 通過 Connection 來創(chuàng)建 Channel Channel channel = connection.createChannel(); //4. 聲明 String exchangeName = "test_fanout_exchange"; String routingKey1 = "item.update"; String routingKey2 = ""; String routingKey3 = "ookjkjjkhjhk";//任意routingkey //5. 發(fā)送 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. 關(guān)閉連接 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'
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis以main方法形式調(diào)用dao層執(zhí)行代碼實例
這篇文章主要介紹了Mybatis以main方法形式調(diào)用dao層執(zhí)行代碼實例,MyBatis 是一款優(yōu)秀的持久層框架,MyBatis 免除了幾乎所有的 JDBC 代碼以及設(shè)置參數(shù)和獲取結(jié)果集的工作,需要的朋友可以參考下2023-08-08springboot使用@Validated或@Valid注解校驗參數(shù)方式
這篇文章主要介紹了springboot使用@Validated或@Valid注解校驗參數(shù)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07關(guān)于Mybatis-Plus?Update更新策略問題
這篇文章主要介紹了關(guān)于Mybatis-Plus?Update更新策略問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11SpringCloud之Zuul服務(wù)網(wǎng)關(guān)詳解
這篇文章主要介紹了SpringCloud之Zuul服務(wù)網(wǎng)關(guān)詳解,服務(wù)網(wǎng)關(guān)是微服務(wù)架構(gòu)中一個不可或缺的部分,通過服務(wù)網(wǎng)關(guān)統(tǒng)一向外系統(tǒng)提供REST?API的過程中,除了具備服務(wù)路由、均衡負載功能之外,它還具備了權(quán)限控制(鑒權(quán))等功能,需要的朋友可以參考下2023-08-08SpringBoot同一接口多個實現(xiàn)類配置的實例詳解
這篇文章主要介紹了SpringBoot同一接口多個實現(xiàn)類配置的實例詳解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11詳解如何獨立使用ribbon實現(xiàn)業(yè)務(wù)客戶端負載均衡
這篇文章主要為大家介紹了詳解如何獨立使用ribbon實現(xiàn)業(yè)務(wù)客戶端負載均衡,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06