一:介绍

1.模式

  

2.应用场景

  如果exchangge与队列中的key相同,消息就发送过去。

  这个就是需要将交换机与队列增加key。

3.路由类型

  上节课的订阅模式中的路由类型是Fanout。

  这篇文章的路由类型是Direct。

二:程序

1.生产者

  1. package com.mq.routing;
  2.  
  3. import com.mq.utils.ConnectionUtil;
  4. import com.rabbitmq.client.Channel;
  5. import com.rabbitmq.client.Connection;
  6. import com.rabbitmq.client.ConnectionFactory;
  7.  
  8. public class RoutingSend {
  9. private static final String EXCHANGE_NAME="test_exchange_direct";
  10. private static final String QUEUE_NAME="test_queue_direct_1";
  11. public static void main(String[] args)throws Exception{
  12. Connection connection= ConnectionUtil.getConnection();
  13. Channel channel=connection.createChannel();
  14. channel.exchangeDeclare(EXCHANGE_NAME,"direct");
  15. String msg="hello routing";
  16. //重点是第二个参数routingKey
  17. String routingKey="info";
  18. channel.basicPublish(EXCHANGE_NAME,routingKey,null,msg.getBytes());
  19. System.out.println("msg send:"+msg);
  20. channel.close();
  21. connection.close();
  22. }
  23. }

2.消费者一

  1. package com.mq.routing;
  2.  
  3. import com.mq.utils.ConnectionUtil;
  4. import com.rabbitmq.client.*;
  5.  
  6. import java.io.IOException;
  7.  
  8. public class RoutingReceive1 {
  9. private static final String EXCHANGE_NAME="test_exchange_direct";
  10. private static final String QUEUE_NAME="test_queue_direct_1";
  11. public static void main(String[] args)throws Exception{
  12. Connection connection= ConnectionUtil.getConnection();
  13. final Channel channel=connection.createChannel();
  14. channel.queueDeclare(QUEUE_NAME,false,false,false,null);
  15. channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"error");
  16. channel.basicQos(1);
  17. Consumer consumer=new DefaultConsumer(channel){
  18. @Override
  19. public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
  20. String msg = new String(body, "utf-8");
  21. System.out.println("[1] receive:" + msg);
  22. try {
  23. Thread.sleep(200);
  24. }catch (Exception e){
  25. e.printStackTrace();
  26. }finally {
  27. System.out.println("[done]");
  28. channel.basicAck(envelope.getDeliveryTag(),false);
  29. }
  30. }
  31. };
  32. boolean autoAck=false;
  33. channel.basicConsume(QUEUE_NAME,autoAck,consumer);
  34. }
  35. }

3.消费者二

  1. package com.mq.routing;
  2.  
  3. import com.mq.utils.ConnectionUtil;
  4. import com.rabbitmq.client.*;
  5.  
  6. import java.io.IOException;
  7.  
  8. public class RoutingReceive2 {
  9. private static final String EXCHANGE_NAME="test_exchange_direct";
  10. private static final String QUEUE_NAME="test_queue_direct_2";
  11. public static void main(String[] args)throws Exception{
  12. Connection connection= ConnectionUtil.getConnection();
  13. final Channel channel=connection.createChannel();
  14. channel.queueDeclare(QUEUE_NAME,false,false,false,null);
  15. channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"error");
  16. channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"info");
  17. channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"waring");
  18. channel.basicQos(1);
  19. Consumer consumer=new DefaultConsumer(channel){
  20. @Override
  21. public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
  22. String msg = new String(body, "utf-8");
  23. System.out.println("[1] receive:" + msg);
  24. try {
  25. Thread.sleep(200);
  26. }catch (Exception e){
  27. e.printStackTrace();
  28. }finally {
  29. System.out.println("[done]");
  30. channel.basicAck(envelope.getDeliveryTag(),false);
  31. }
  32. }
  33. };
  34. boolean autoAck=false;
  35. channel.basicConsume(QUEUE_NAME,autoAck,consumer);
  36. }
  37. }

4.现象

  send:

  

  receive1:

  

  receive2:

  

routing路由模式的更多相关文章

  1. LVS模式一:直接路由模式DR(Direct Routing)

    (一)LVS 一.LVS的了解 LVS(Linux Virtual Server)可以理解为一个虚拟服务器系统. Internet的飞速发展,网络带宽的增长,Web服务中越来越多地使用CGI.动态主页 ...

  2. python使用rabbitMQ介绍四(路由模式)

    一.模式介绍 路由模式,与发布-订阅模式一样,消息发送到exchange中,消费者把队列绑定到exchange上. 这种模式在exchange上添加添加了一个路由键(routing-key),生产者发 ...

  3. RabbitMQ六种队列模式-路由模式

    前言 RabbitMQ六种队列模式-简单队列RabbitMQ六种队列模式-工作队列RabbitMQ六种队列模式-发布订阅RabbitMQ六种队列模式-路由模式 [本文]RabbitMQ六种队列模式-主 ...

  4. ASP.NET Core路由中间件[2]: 路由模式

    一个Web应用本质上体现为一组终结点的集合.终结点则体现为一个暴露在网络中可供外界采用HTTP协议调用的服务,路由的作用就是建立一个请求URL模式与对应终结点之间的映射关系.借助这个映射关系,客户端可 ...

  5. .NET/ASP.NET Routing路由(深入解析路由系统架构原理)

    阅读目录: 1.开篇介绍 2.ASP.NET Routing 路由对象模型的位置 3.ASP.NET Routing 路由对象模型的入口 4.ASP.NET Routing 路由对象模型的内部结构 4 ...

  6. 修改thinkphp路由模式,去掉Home

    第一步:入口文件增加 define('BIND_MODULE', 'Home'); 第二步:修改config文件,我这里路由模式设置为2 效果展示:

  7. RabbitMQ 一二事(4) - 路由模式介绍

    路由模式其实和订阅模式差不多,只不过交换机的类型不同而已 路由模式可以用下图来表示,比订阅模式多了一个key,举个栗子就是根据不同的人群来订阅公众号,来收取消息 根据不同的key来获取不同的消息 最简 ...

  8. .NET/ASP.NET Routing路由(深入解析路由系统架构原理)http://wangqingpei557.blog.51cto.com/1009349/1312422

    阅读目录: 1.开篇介绍 2.ASP.NET Routing 路由对象模型的位置 3.ASP.NET Routing 路由对象模型的入口 4.ASP.NET Routing 路由对象模型的内部结构 4 ...

  9. NET/ASP.NET Routing路由(深入解析路由系统架构原理)(转载)

    NET/ASP.NET Routing路由(深入解析路由系统架构原理) 阅读目录: 1.开篇介绍 2.ASP.NET Routing 路由对象模型的位置 3.ASP.NET Routing 路由对象模 ...

随机推荐

  1. 液晶数字显示屏QLCDNumbe

    import sys from PyQt5.QtWidgets import QApplication, QWidget, QLCDNumber, QVBoxLayout class Demo(QWi ...

  2. JavaScript之小工具之日志log()[兼容]

    function log(){ try{ console.log.apply(console,arguments); }catch(e){ try{ opera.postError.apply(ope ...

  3. MHA-Failover(GTID,Auto_Position=0)

    最近一位同学遇到的案例:凌晨数据库意外宕机,要求在一主两从的基础上,搭建MHA做故障切换.在部署测试中遇到一些问题找到我,交流的过程挖出一些之前忽略的坑,感谢这位同学无私分享!• GTID环境,KIL ...

  4. spring整合ehcache2.5.2缓存异常-- net.sf.ehcache.CacheException

    报错如下: The source of the existing CacheManager is: DefaultConfigurationSource [ ehcache.xml or ehcach ...

  5. Eclipse中项目不会自动编译问题的坑和注意点

    最近接受了几个又小有老的项目,用eclipse反而比idea方便,但是好长时间不用eclipse了,还有有些问题的! 主要是碰到了classnotfound这个难缠的问题:这里记录一下几个坑,避免以后 ...

  6. Django 利用管理器实现文章归档

    Django管理器:class Manager 管理器是Django的模型进行数据库查询的接口,Django应用的每个模型都拥有至少一个管理器.默认情况下,Django为每个模型类添加一个名为obje ...

  7. openstack swift节点安装手册1-节点配置

    本文参照官方教程:http://docs.openstack.org/project-install-guide/object-storage/draft/environment-networking ...

  8. navicat报caching_sha2_password异常

    使用navicat连接mysql报错(升级到mysql8版本时的错) 解决办法: 通过命令行登录mysql后, 输入: alter user 'root'@'localhost' IDENTIFIED ...

  9. 基于TLS的EAP 认证方法

    TLS: transport level security , 安全传输层协议,用于在两个通信应用程序之间提供保密性和数据完整性.该协议由两层组成: TLS 记录协议(TLS Record)和 TLS ...

  10. nagios系列(六)之nagios实现对服务器cpu温度的监控

    1.安装硬件传感器监控软件sensors yum install -y lm_sensors* 2.运行sensors-detect进行传感器检测 ##一路回车即可 Do you want to ov ...