东莞做网站电话,公司网站建设多少费用哪儿济南兴田德润联系电话,淘宝这种网站怎么做的,南京网站建设 雷仁网文章目录 交换机fanoutDirecttopicHeadersRPC 交换机
**交换机 **是消息队列中的一个组件#xff0c;其作用类似于网络路由器。它负责将我们发送的消息转发到相应的目标#xff0c;就像快递站将快递发送到对应的站点#xff0c;或者网络路由器将网络请求转发到相应的服务器… 文章目录 交换机fanoutDirecttopicHeadersRPC 交换机
**交换机 **是消息队列中的一个组件其作用类似于网络路由器。它负责将我们发送的消息转发到相应的目标就像快递站将快递发送到对应的站点或者网络路由器将网络请求转发到相应的服务器或客户端一样。交换机的主要功能是提供转发消息的能力根据消息的路由规则将消息投递到合适的队列或绑定的消费者。 我们可以理解为如果说一个快递站已经承受不了那么多的快递了就建多个快递站。
fanout
扇出广播 特点消息会被转发到所有绑定到该交换机的队列 场景:很适用于发布订阅的场景比如写日志可以多个系统间共享 示例场景 生产者代码
package com.yupi.springbootinit.mq;import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;import java.util.Scanner;public class FanoutProducer {// 交换机名字private static final String EXCHANGE_NAME fanout-exchange;public static void main(String[] argv) throws Exception {ConnectionFactory factory new ConnectionFactory();factory.setHost(localhost);try (Connection connection factory.newConnection();Channel channel connection.createChannel()) {// 创建交换机channel.exchangeDeclare(EXCHANGE_NAME, fanout);Scanner scanner new Scanner(System.in);while (scanner.hasNext()) {String message scanner.nextLine();channel.basicPublish(EXCHANGE_NAME, , null, message.getBytes(UTF-8));System.out.println( [x] Sent message );}}}
}消费者代码
package com.yupi.springbootinit.mq;import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;public class FanoutConsumer {//交换机名字private static final String EXCHANGE_NAME fanout-exchange;public static void main(String[] argv) throws Exception {//建立连接ConnectionFactory factory new ConnectionFactory();factory.setHost(localhost);Connection connection factory.newConnection();//创建频道Channel channel1 connection.createChannel();// 声明交换机channel1.exchangeDeclare(EXCHANGE_NAME, fanout);// 创建队列1连接到交换机上String queueName xiaowang_queue;channel1.queueDeclare(queueName, true, false, false, null);channel1.queueBind(queueName, EXCHANGE_NAME, );// 创建队列2,连接到交换机上String queueName2 xiaoli_queue;channel1.queueDeclare(queueName2, true, false, false, null);channel1.queueBind(queueName2, EXCHANGE_NAME, );System.out.println( [*] Waiting for messages. To exit press CTRLC);// 创建交付回调函数1DeliverCallback deliverCallback1 (consumerTag, delivery) - {String message new String(delivery.getBody(), UTF-8);System.out.println( [小王] Received message );};// 创建交付回调函数2DeliverCallback deliverCallback2 (consumerTag, delivery) - {String message new String(delivery.getBody(), UTF-8);System.out.println( [小李] Received message );};// 开始消费消息队列1channel1.basicConsume(queueName, true, deliverCallback1, consumerTag - { });// 开始消费消息队列2channel1.basicConsume(queueName2, true, deliverCallback2, consumerTag - { });}
}Direct
官方教程https://www.rabbitmq.com/tutorials/tutorial-four-java.html 特点消息会根据路由键转发到指定的队列 场景特定的消息只交给特定的系统程序来处理 注意不同队列可以绑定相同的路由键 示例场景 老板在发送消息同时会带上路由键根据路由键找对应的队列来发送 生产者代码
package com.yupi.springbootinit.mq;import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;import java.util.Scanner;public class DirectProducer {private static final String EXCHANGE_NAME direct-exchange;public static void main(String[] argv) throws Exception {ConnectionFactory factory new ConnectionFactory();factory.setHost(localhost);try (Connection connection factory.newConnection();Channel channel connection.createChannel()) {//声明交换机是directchannel.exchangeDeclare(EXCHANGE_NAME, direct);//输入消息 和 路由键Scanner scanner new Scanner(System.in);while (scanner.hasNext()) {String userInput scanner.nextLine();String[] strings userInput.split( );if (strings.length 1) {continue;}String message strings[0];String routingKey strings[1];//发布消息的时候注意指定路由键channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes(UTF-8));System.out.println( [x] Sent message with routing: routingKey );}}}
}消费者代码
package com.yupi.springbootinit.mq;import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;public class DirectConsumer {private static final String EXCHANGE_NAME direct-exchange;public static void main(String[] argv) throws Exception {ConnectionFactory factory new ConnectionFactory();factory.setHost(localhost);Connection connection factory.newConnection();Channel channel connection.createChannel();//声明交换机不过生成者已经声明过了消费者声不声明都可以channel.exchangeDeclare(EXCHANGE_NAME, direct);// 创建队列String queueName xiaoyu_queue;channel.queueDeclare(queueName, true, false, false, null);channel.queueBind(queueName, EXCHANGE_NAME, xiaoyu); //指定2交换机和路由键// 创建队列随机分配一个队列名称String queueName2 xiaopi_queue;channel.queueDeclare(queueName2, true, false, false, null);channel.queueBind(queueName2, EXCHANGE_NAME, xiaopi);System.out.println( [*] Waiting for messages. To exit press CTRLC);DeliverCallback xiaoyuDeliverCallback (consumerTag, delivery) - {String message new String(delivery.getBody(), UTF-8);System.out.println( [xiaoyu] Received delivery.getEnvelope().getRoutingKey() : message );};DeliverCallback xiaopiDeliverCallback (consumerTag, delivery) - {String message new String(delivery.getBody(), UTF-8);System.out.println( [xiaopi] Received delivery.getEnvelope().getRoutingKey() : message );};channel.basicConsume(queueName, true, xiaoyuDeliverCallback, consumerTag - {});channel.basicConsume(queueName2, true, xiaopiDeliverCallback, consumerTag - {});}
}topic
官方教程https://www.rabbitmq.com/tutorials/tutorial-five-java.html 特点消息会根据一个模糊的路由键转发到指定的队列 场景:特定的一类消息可以交给特定的一类系统程序来处理 规则
:匹配一个单词比如.orange那么 abc.orange、ikun.orange 都能匹配#:匹配0个或多个单词比如orange.#那么orangeorange.abc.ikun都能匹配 应用场景 老板要下发一个任务让多个组来处理 生产者代码
package com.yupi.springbootinit.mq;import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;import java.util.Scanner;public class TopicProducer {private static final String EXCHANGE_NAME topic-exchange;public static void main(String[] argv) throws Exception {ConnectionFactory factory new ConnectionFactory();factory.setHost(localhost);try (Connection connection factory.newConnection();Channel channel connection.createChannel()) {channel.exchangeDeclare(EXCHANGE_NAME, topic);Scanner scanner new Scanner(System.in);while (scanner.hasNext()) {String userInput scanner.nextLine();String[] strings userInput.split( );if (strings.length 1) {continue;}String message strings[0];String routingKey strings[1];channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes(UTF-8));System.out.println( [x] Sent message with routing: routingKey );}}}
}消费者代码
package com.yupi.springbootinit.mq;import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;public class TopicConsumer {private static final String EXCHANGE_NAME topic-exchange;public static void main(String[] argv) throws Exception {ConnectionFactory factory new ConnectionFactory();factory.setHost(localhost);Connection connection factory.newConnection();Channel channel connection.createChannel();channel.exchangeDeclare(EXCHANGE_NAME, topic);// 创建队列String queueName frontend_queue;channel.queueDeclare(queueName, true, false, false, null);channel.queueBind(queueName, EXCHANGE_NAME, #.前端.#);// 创建队列String queueName2 backend_queue;channel.queueDeclare(queueName2, true, false, false, null);channel.queueBind(queueName2, EXCHANGE_NAME, #.后端.#);// 创建队列String queueName3 product_queue;channel.queueDeclare(queueName3, true, false, false, null);channel.queueBind(queueName3, EXCHANGE_NAME, #.产品.#);System.out.println( [*] Waiting for messages. To exit press CTRLC);DeliverCallback xiaoaDeliverCallback (consumerTag, delivery) - {String message new String(delivery.getBody(), UTF-8);System.out.println( [xiaoa] Received delivery.getEnvelope().getRoutingKey() : message );};DeliverCallback xiaobDeliverCallback (consumerTag, delivery) - {String message new String(delivery.getBody(), UTF-8);System.out.println( [xiaob] Received delivery.getEnvelope().getRoutingKey() : message );};DeliverCallback xiaocDeliverCallback (consumerTag, delivery) - {String message new String(delivery.getBody(), UTF-8);System.out.println( [xiaoc] Received delivery.getEnvelope().getRoutingKey() : message );};channel.basicConsume(queueName, true, xiaoaDeliverCallback, consumerTag - {});channel.basicConsume(queueName2, true, xiaobDeliverCallback, consumerTag - {});channel.basicConsume(queueName3, true, xiaocDeliverCallback, consumerTag - {});}
}这样生产者发消息前端.后端 就可以匹配到前端和后端两个队列
Headers
可以根据headers中的内容来指定发送到哪个队列由于性能差比较复杂一般不推荐使用
RPC
支持用消息队列来模拟RPC的调用但是一般没必要直接用 Dubbo、GRPC 等 RPC 框架就好了。 文章转载自: http://www.morning.xdxpq.cn.gov.cn.xdxpq.cn http://www.morning.tpssx.cn.gov.cn.tpssx.cn http://www.morning.c7491.cn.gov.cn.c7491.cn http://www.morning.cnhgc.cn.gov.cn.cnhgc.cn http://www.morning.mrbzq.cn.gov.cn.mrbzq.cn http://www.morning.hfnbr.cn.gov.cn.hfnbr.cn http://www.morning.mwbqk.cn.gov.cn.mwbqk.cn http://www.morning.rlbc.cn.gov.cn.rlbc.cn http://www.morning.sftpg.cn.gov.cn.sftpg.cn http://www.morning.wrwcf.cn.gov.cn.wrwcf.cn http://www.morning.sqqhd.cn.gov.cn.sqqhd.cn http://www.morning.xbhpm.cn.gov.cn.xbhpm.cn http://www.morning.rjnky.cn.gov.cn.rjnky.cn http://www.morning.mtymb.cn.gov.cn.mtymb.cn http://www.morning.qpqwb.cn.gov.cn.qpqwb.cn http://www.morning.qsmmq.cn.gov.cn.qsmmq.cn http://www.morning.fkwgk.cn.gov.cn.fkwgk.cn http://www.morning.hcqd.cn.gov.cn.hcqd.cn http://www.morning.djxnw.cn.gov.cn.djxnw.cn http://www.morning.sgbk.cn.gov.cn.sgbk.cn http://www.morning.kwksj.cn.gov.cn.kwksj.cn http://www.morning.xqkcs.cn.gov.cn.xqkcs.cn http://www.morning.qsctt.cn.gov.cn.qsctt.cn http://www.morning.xflwq.cn.gov.cn.xflwq.cn http://www.morning.dlmqn.cn.gov.cn.dlmqn.cn http://www.morning.bnrnb.cn.gov.cn.bnrnb.cn http://www.morning.bdfph.cn.gov.cn.bdfph.cn http://www.morning.zrpys.cn.gov.cn.zrpys.cn http://www.morning.dbfp.cn.gov.cn.dbfp.cn http://www.morning.fbxdp.cn.gov.cn.fbxdp.cn http://www.morning.nbnq.cn.gov.cn.nbnq.cn http://www.morning.chmkt.cn.gov.cn.chmkt.cn http://www.morning.yfzld.cn.gov.cn.yfzld.cn http://www.morning.bkqw.cn.gov.cn.bkqw.cn http://www.morning.kxqfz.cn.gov.cn.kxqfz.cn http://www.morning.sgrdp.cn.gov.cn.sgrdp.cn http://www.morning.sfsjh.cn.gov.cn.sfsjh.cn http://www.morning.mjzgg.cn.gov.cn.mjzgg.cn http://www.morning.1000sh.com.gov.cn.1000sh.com http://www.morning.rjnky.cn.gov.cn.rjnky.cn http://www.morning.nfks.cn.gov.cn.nfks.cn http://www.morning.ybgcn.cn.gov.cn.ybgcn.cn http://www.morning.ypwlb.cn.gov.cn.ypwlb.cn http://www.morning.ruyuaixuexi.com.gov.cn.ruyuaixuexi.com http://www.morning.snzgg.cn.gov.cn.snzgg.cn http://www.morning.zbtfz.cn.gov.cn.zbtfz.cn http://www.morning.ymhzd.cn.gov.cn.ymhzd.cn http://www.morning.bbgn.cn.gov.cn.bbgn.cn http://www.morning.mbqyl.cn.gov.cn.mbqyl.cn http://www.morning.nwbnt.cn.gov.cn.nwbnt.cn http://www.morning.bmrqz.cn.gov.cn.bmrqz.cn http://www.morning.lcbt.cn.gov.cn.lcbt.cn http://www.morning.kmqlf.cn.gov.cn.kmqlf.cn http://www.morning.ykmkz.cn.gov.cn.ykmkz.cn http://www.morning.cwcdr.cn.gov.cn.cwcdr.cn http://www.morning.bpmz.cn.gov.cn.bpmz.cn http://www.morning.msgrq.cn.gov.cn.msgrq.cn http://www.morning.bzkgn.cn.gov.cn.bzkgn.cn http://www.morning.pdkht.cn.gov.cn.pdkht.cn http://www.morning.hilmwmu.cn.gov.cn.hilmwmu.cn http://www.morning.qbgdy.cn.gov.cn.qbgdy.cn http://www.morning.fnfxp.cn.gov.cn.fnfxp.cn http://www.morning.jbmsp.cn.gov.cn.jbmsp.cn http://www.morning.hlyfn.cn.gov.cn.hlyfn.cn http://www.morning.zmzdx.cn.gov.cn.zmzdx.cn http://www.morning.qcztm.cn.gov.cn.qcztm.cn http://www.morning.rtbj.cn.gov.cn.rtbj.cn http://www.morning.fkffr.cn.gov.cn.fkffr.cn http://www.morning.fdrb.cn.gov.cn.fdrb.cn http://www.morning.qsy41.cn.gov.cn.qsy41.cn http://www.morning.whpsl.cn.gov.cn.whpsl.cn http://www.morning.rnxs.cn.gov.cn.rnxs.cn http://www.morning.qggxt.cn.gov.cn.qggxt.cn http://www.morning.rmpkn.cn.gov.cn.rmpkn.cn http://www.morning.rwls.cn.gov.cn.rwls.cn http://www.morning.qgmbx.cn.gov.cn.qgmbx.cn http://www.morning.hrpbq.cn.gov.cn.hrpbq.cn http://www.morning.lpbrp.cn.gov.cn.lpbrp.cn http://www.morning.nllst.cn.gov.cn.nllst.cn http://www.morning.yhywr.cn.gov.cn.yhywr.cn