routing路由模式
一:介绍
1.模式
2.应用场景
如果exchangge与队列中的key相同,消息就发送过去。
这个就是需要将交换机与队列增加key。
3.路由类型
上节课的订阅模式中的路由类型是Fanout。
这篇文章的路由类型是Direct。
二:程序
1.生产者
- package com.mq.routing;
- import com.mq.utils.ConnectionUtil;
- import com.rabbitmq.client.Channel;
- import com.rabbitmq.client.Connection;
- import com.rabbitmq.client.ConnectionFactory;
- public class RoutingSend {
- private static final String EXCHANGE_NAME="test_exchange_direct";
- private static final String QUEUE_NAME="test_queue_direct_1";
- public static void main(String[] args)throws Exception{
- Connection connection= ConnectionUtil.getConnection();
- Channel channel=connection.createChannel();
- channel.exchangeDeclare(EXCHANGE_NAME,"direct");
- String msg="hello routing";
- //重点是第二个参数routingKey
- String routingKey="info";
- channel.basicPublish(EXCHANGE_NAME,routingKey,null,msg.getBytes());
- System.out.println("msg send:"+msg);
- channel.close();
- connection.close();
- }
- }
2.消费者一
- package com.mq.routing;
- import com.mq.utils.ConnectionUtil;
- import com.rabbitmq.client.*;
- import java.io.IOException;
- public class RoutingReceive1 {
- private static final String EXCHANGE_NAME="test_exchange_direct";
- private static final String QUEUE_NAME="test_queue_direct_1";
- public static void main(String[] args)throws Exception{
- Connection connection= ConnectionUtil.getConnection();
- final Channel channel=connection.createChannel();
- channel.queueDeclare(QUEUE_NAME,false,false,false,null);
- channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"error");
- channel.basicQos(1);
- Consumer consumer=new DefaultConsumer(channel){
- @Override
- public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
- String msg = new String(body, "utf-8");
- System.out.println("[1] receive:" + msg);
- try {
- Thread.sleep(200);
- }catch (Exception e){
- e.printStackTrace();
- }finally {
- System.out.println("[done]");
- channel.basicAck(envelope.getDeliveryTag(),false);
- }
- }
- };
- boolean autoAck=false;
- channel.basicConsume(QUEUE_NAME,autoAck,consumer);
- }
- }
3.消费者二
- package com.mq.routing;
- import com.mq.utils.ConnectionUtil;
- import com.rabbitmq.client.*;
- import java.io.IOException;
- public class RoutingReceive2 {
- private static final String EXCHANGE_NAME="test_exchange_direct";
- private static final String QUEUE_NAME="test_queue_direct_2";
- public static void main(String[] args)throws Exception{
- Connection connection= ConnectionUtil.getConnection();
- final Channel channel=connection.createChannel();
- channel.queueDeclare(QUEUE_NAME,false,false,false,null);
- channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"error");
- channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"info");
- channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"waring");
- channel.basicQos(1);
- Consumer consumer=new DefaultConsumer(channel){
- @Override
- public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
- String msg = new String(body, "utf-8");
- System.out.println("[1] receive:" + msg);
- try {
- Thread.sleep(200);
- }catch (Exception e){
- e.printStackTrace();
- }finally {
- System.out.println("[done]");
- channel.basicAck(envelope.getDeliveryTag(),false);
- }
- }
- };
- boolean autoAck=false;
- channel.basicConsume(QUEUE_NAME,autoAck,consumer);
- }
- }
4.现象
send:
receive1:
receive2:
routing路由模式的更多相关文章
- LVS模式一:直接路由模式DR(Direct Routing)
(一)LVS 一.LVS的了解 LVS(Linux Virtual Server)可以理解为一个虚拟服务器系统. Internet的飞速发展,网络带宽的增长,Web服务中越来越多地使用CGI.动态主页 ...
- python使用rabbitMQ介绍四(路由模式)
一.模式介绍 路由模式,与发布-订阅模式一样,消息发送到exchange中,消费者把队列绑定到exchange上. 这种模式在exchange上添加添加了一个路由键(routing-key),生产者发 ...
- RabbitMQ六种队列模式-路由模式
前言 RabbitMQ六种队列模式-简单队列RabbitMQ六种队列模式-工作队列RabbitMQ六种队列模式-发布订阅RabbitMQ六种队列模式-路由模式 [本文]RabbitMQ六种队列模式-主 ...
- ASP.NET Core路由中间件[2]: 路由模式
一个Web应用本质上体现为一组终结点的集合.终结点则体现为一个暴露在网络中可供外界采用HTTP协议调用的服务,路由的作用就是建立一个请求URL模式与对应终结点之间的映射关系.借助这个映射关系,客户端可 ...
- .NET/ASP.NET Routing路由(深入解析路由系统架构原理)
阅读目录: 1.开篇介绍 2.ASP.NET Routing 路由对象模型的位置 3.ASP.NET Routing 路由对象模型的入口 4.ASP.NET Routing 路由对象模型的内部结构 4 ...
- 修改thinkphp路由模式,去掉Home
第一步:入口文件增加 define('BIND_MODULE', 'Home'); 第二步:修改config文件,我这里路由模式设置为2 效果展示:
- RabbitMQ 一二事(4) - 路由模式介绍
路由模式其实和订阅模式差不多,只不过交换机的类型不同而已 路由模式可以用下图来表示,比订阅模式多了一个key,举个栗子就是根据不同的人群来订阅公众号,来收取消息 根据不同的key来获取不同的消息 最简 ...
- .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 ...
- NET/ASP.NET Routing路由(深入解析路由系统架构原理)(转载)
NET/ASP.NET Routing路由(深入解析路由系统架构原理) 阅读目录: 1.开篇介绍 2.ASP.NET Routing 路由对象模型的位置 3.ASP.NET Routing 路由对象模 ...
随机推荐
- 液晶数字显示屏QLCDNumbe
import sys from PyQt5.QtWidgets import QApplication, QWidget, QLCDNumber, QVBoxLayout class Demo(QWi ...
- JavaScript之小工具之日志log()[兼容]
function log(){ try{ console.log.apply(console,arguments); }catch(e){ try{ opera.postError.apply(ope ...
- MHA-Failover(GTID,Auto_Position=0)
最近一位同学遇到的案例:凌晨数据库意外宕机,要求在一主两从的基础上,搭建MHA做故障切换.在部署测试中遇到一些问题找到我,交流的过程挖出一些之前忽略的坑,感谢这位同学无私分享!• GTID环境,KIL ...
- spring整合ehcache2.5.2缓存异常-- net.sf.ehcache.CacheException
报错如下: The source of the existing CacheManager is: DefaultConfigurationSource [ ehcache.xml or ehcach ...
- Eclipse中项目不会自动编译问题的坑和注意点
最近接受了几个又小有老的项目,用eclipse反而比idea方便,但是好长时间不用eclipse了,还有有些问题的! 主要是碰到了classnotfound这个难缠的问题:这里记录一下几个坑,避免以后 ...
- Django 利用管理器实现文章归档
Django管理器:class Manager 管理器是Django的模型进行数据库查询的接口,Django应用的每个模型都拥有至少一个管理器.默认情况下,Django为每个模型类添加一个名为obje ...
- openstack swift节点安装手册1-节点配置
本文参照官方教程:http://docs.openstack.org/project-install-guide/object-storage/draft/environment-networking ...
- navicat报caching_sha2_password异常
使用navicat连接mysql报错(升级到mysql8版本时的错) 解决办法: 通过命令行登录mysql后, 输入: alter user 'root'@'localhost' IDENTIFIED ...
- 基于TLS的EAP 认证方法
TLS: transport level security , 安全传输层协议,用于在两个通信应用程序之间提供保密性和数据完整性.该协议由两层组成: TLS 记录协议(TLS Record)和 TLS ...
- nagios系列(六)之nagios实现对服务器cpu温度的监控
1.安装硬件传感器监控软件sensors yum install -y lm_sensors* 2.运行sensors-detect进行传感器检测 ##一路回车即可 Do you want to ov ...