這篇文章主要介紹了RabbitMQ最常用的三大模式是什么,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比龍灣網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式龍灣網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋龍灣地區(qū)。費(fèi)用合理售后完善,十載實(shí)體公司更值得信賴。Direct 模式
所有發(fā)送到 Direct Exchange 的消息被轉(zhuǎn)發(fā)到 RouteKey 中指定的 Queue。
Direct 模式可以使用 RabbitMQ 自帶的 Exchange: default Exchange,所以不需要將 Exchange 進(jìn)行任何綁定(binding)操作。
消息傳遞時(shí),RouteKey 必須完全匹配才會被隊(duì)列接收,否則該消息會被拋棄,
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)建一個(gè) ConnectionFactory 并進(jìn)行設(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)建一個(gè) ConnectionFactory 并進(jìn)行設(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); //一般不用代碼綁定,在管理界面手動(dòng)綁定 channel.queueBind(queueName, exchangeName, routingKey); //5. 創(chuàng)建消費(fèi)者并接收消息 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 消費(fèi)者綁定隊(duì)列 channel.basicConsume(queueName, true, consumer); } }
Send message : this is direct msg [x] Received 'this is direct msg'
Topic 模式
可以使用通配符進(jìn)行模糊匹配
符號'#" 匹配一個(gè)或多個(gè)詞
符號"*”匹配不多不少一個(gè)詞
例如
'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)建一個(gè) ConnectionFactory 并進(jìn)行設(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)建一個(gè) ConnectionFactory 并進(jìn)行設(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); //一般不用代碼綁定,在管理界面手動(dòng)綁定 channel.queueBind(queueName, exchangeName, routingKey); //5. 創(chuàng)建消費(fèi)者并接收消息 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 消費(fèi)者綁定隊(duì)列 channel.basicConsume(queueName, true, consumer); } }
Send message : this is topc msg [x] Received 'this is topc msg' [x] Received 'this is topc msg'
Fanout 模式
不處理路由鍵,只需要簡單的將隊(duì)列綁定到交換機(jī)上發(fā)送到交換機(jī)的消息都會被轉(zhuǎn)發(fā)到與該交換機(jī)綁定的所有隊(duì)列上。
Fanout交換機(jī)轉(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)建一個(gè) ConnectionFactory 并進(jìn)行設(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); //一般不用代碼綁定,在管理界面手動(dòng)綁定 channel.queueBind(queueName, exchangeName, routingKey); //5. 創(chuàng)建消費(fèi)者并接收消息 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 消費(fèi)者綁定隊(duì)列 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)建一個(gè) ConnectionFactory 并進(jìn)行設(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'
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“RabbitMQ最常用的三大模式是什么”這篇文章對大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián)建站,關(guān)注創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站www.cdcxhl.com,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。