RabbitMq04 direct路由模式

一、

在这里插入图片描述
路由模式特点:
队列与交换机的绑定,不能是任意绑定了,而是要指定一个 RoutingKey (路由key)
消息的发送方在 向 Exchange发送消息时,也必须指定消息的 RoutingKey 。
Exchange不再把消息交给每一个绑定的队列,而是根据消息的 Routing Key 进行判断,只有队列的
Routingkey 与消息的 Routing key 完全一致,才会接收到消息

二、案例需求

一个频道
一个交换机(类型为:direct)
两个路由键(info and error)
两个队列(一个队列为info,一个队列为info and error)
一个生产者
两个消费者

三、代码

product:

package com.xiaoxu.direct;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.xiaoxu.ConnectionUtis;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Direct 路由模式
 */
public class Producter {

    //定义两个消息队列
    static  final  String QUEUE_DIRECT_1 = "queue_direct_1";
    static  final  String QUEUE_DIRECT_2 = "queue_direct_2";
    //defining a exchange
    static final  String EXCHANGE_NAME = "exchange_direct";

    public static void main(String[] args) throws IOException, TimeoutException {
        //1、创建连接工厂
        //2. 创建连接;(抽取一个获取连接的工具类)
        Connection connection = ConnectionUtis.getConnection();
        // 3. 创建频道;
        Channel channel = connection.createChannel();

        //statement exchange
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);

        //5. 声明队列;
        /**
         * 参数1:队列名称
         * 参数2:是否定义持久化队列
         * 参数3:是否独占本次连接
         * 参数4:是否在不使用的时候自动删除队列
         * 参数5:队列其它参数
         */
        channel.queueDeclare(QUEUE_DIRECT_1,true,false,false,null);
        channel.queueDeclare(QUEUE_DIRECT_2,true,false,false,null);
        // 5. 队列绑定交换机
        channel.queueBind(QUEUE_DIRECT_1, EXCHANGE_NAME, "infor");
        channel.queueBind(QUEUE_DIRECT_2, EXCHANGE_NAME, "error");
        channel.queueBind(QUEUE_DIRECT_2, EXCHANGE_NAME, "infor");

        // 6. 监听队列
        /**
         * 参数1:交换机名称,如果没有指定则使用默认Default Exchage
         * 参数2:路由key,简单模式可以传递队列名称
         * 参数3:消息其它属性
         * 参数4:消息内容
         */

        String message = "我是一个消息。。infor";
        channel.basicPublish(EXCHANGE_NAME, "infor", null, message.getBytes());
        System.out.println(message);

        message = "我是一个消息。。error";
        channel.basicPublish(EXCHANGE_NAME,"error",null,message.getBytes());
        System.out.println(message);

        //7、关闭资源
        channel.close();
        connection.close();
    }
}

consumer1:

package com.xiaoxu.direct;


import com.rabbitmq.client.*;
import com.xiaoxu.ConnectionUtis;


import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer1 {

    public static void main(String[] args) throws IOException, TimeoutException {

        Connection connection = ConnectionUtis.getConnection();
        //创建频道
        Channel channel = connection.createChannel();
        //statement exchange
        channel.exchangeDeclare(Producter.EXCHANGE_NAME,BuiltinExchangeType.FANOUT);
        // create queue
        channel.queueDeclare(Producter.QUEUE_DIRECT_1,true,false,false,null);
        //bainding  queue and exchange
        channel.queueBind(Producter.QUEUE_DIRECT_1,Producter.EXCHANGE_NAME,"infor");


        //creat consumer and set information
        DefaultConsumer consumer1 = new DefaultConsumer(channel) {
            @Override
          public void handleDelivery(String consumerTag, Envelope envelope,
                                     AMQP.BasicProperties properties, byte[] body) throws IOException{

                //路由key
                System.out.println("路由key为:" + envelope.getRoutingKey());
                //交换机
                System.out.println("交换机为:" + envelope.getExchange());
                //消息id
                System.out.println("消息id为:" + envelope.getDeliveryTag());
                //收到的消息
                System.out.println("接收到的消息为:" + new String(body, "utf-8"));

                System.out.println("我是消费者一号。。。");
            }
        };

        //listen to the message
        channel.basicConsume(Producter.QUEUE_DIRECT_1,true,consumer1);
    }
}

consumer2:

package com.xiaoxu.direct;


import com.rabbitmq.client.*;
import com.xiaoxu.ConnectionUtis;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer2 {

    public static void main(String[] args) throws IOException, TimeoutException {

        Connection connection = ConnectionUtis.getConnection();
        //创建频道
        Channel channel = connection.createChannel();
        //statement exchange
        channel.exchangeDeclare(Producter.EXCHANGE_NAME,BuiltinExchangeType.FANOUT);
        // create queue
        channel.queueDeclare(Producter.QUEUE_DIRECT_2,true,false,false,null);
        //bainding  queue and exchange
        channel.queueBind(Producter.QUEUE_DIRECT_2,Producter.EXCHANGE_NAME,"infor");
        channel.queueBind(Producter.QUEUE_DIRECT_2,Producter.EXCHANGE_NAME,"error");

        //creat consumer and set information
        DefaultConsumer consumer1 = new DefaultConsumer(channel) {
            @Override
          public void handleDelivery(String consumerTag, Envelope envelope,
                                     AMQP.BasicProperties properties, byte[] body) throws IOException{

                //路由key
                System.out.println("路由key为:" + envelope.getRoutingKey());
                //交换机
                System.out.println("交换机为:" + envelope.getExchange());
                //消息id
                System.out.println("消息id为:" + envelope.getDeliveryTag());
                //收到的消息
                System.out.println("接收到的消息为:" + new String(body, "utf-8"));

                System.out.println("我是消费者二号。。。");
            }
        };

        //listen to the message
        channel.basicConsume(Producter.QUEUE_DIRECT_2,true,consumer1);


    }
}

四、测试

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

五、小结

启动所有消费者,然后使用生产者发送消息;在消费者对应的控制台可以查看到生产者发送对应routing key对应队
列的消息;到达按照需要接收的效果。
Routing模式要求队列在绑定交换机时要指定routing key,消息会转发到符合routing key的队列。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

徐睡睡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值