RabbitMQ--Routing之订阅模型-Direct(直连)

Routing之订阅模型-Direct(直连)

在Fanout模式中,一条消息会被所有订阅的队列都消费。但是,在某些场景下,我们希望不同的消息被不同的队列消费。这时就要用到Direct的Exchange。
在Direct模型下:

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

Provider

public class Provider {
    
    
    public static void main(String[] args) throws IOException {
    
    
        //获取连接对象
        Connection connection = RabbitMqUtils.getConnection();
        //获取连接通道对象
        Channel channel = connection.createChannel();
        //通过通道声明我们的交换机
        //参数1:交换机名称
        //参数2:direct  路由模式
        channel.exchangeDeclare("logs_direct","direct");
        //发送消息
        String routingkey = "info";
        channel.basicPublish("logs_direct",routingkey,null,
                ("这个时direct模型发布的基于route key:["+routingkey+"] 发送的消息").getBytes());
        //关闭连接
        RabbitMqUtils.closeConnectionAndChannel(channel,connection);
    }
}

Consumer1

public class Consumer1 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        //创建连接对象
        Connection connection = RabbitMqUtils.getConnection();
        //创建连接通道
        Channel channel = connection.createChannel();
        //定义交换机名称
        String exchangeName = "logs_direct";
        //通道声明交换机以及交换机的类型
        channel.exchangeDeclare(exchangeName, "direct");
        //创建一个临时队列
        String queue = channel.queueDeclare().getQueue();
        //基于route key 绑定队列和交换机
        channel.queueBind(queue, exchangeName, "error");
        //获取消费的消息
        channel.basicConsume(queue, true, new DefaultConsumer(channel) {
    
    
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                                       byte[] body) {
    
    
                System.out.println("消费者1:" + new String(body));
            }
        });
        //关闭资源
        RabbitMqUtils.closeConnectionAndChannel(channel,connection);
    }
}

Consumer2

public class Consumer2 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        Connection connection = RabbitMqUtils.getConnection();
        Channel channel = connection.createChannel();
        //定义交换机名称
        String exchangeName = "logs_direct";
        //声明交换机以及交换机类型
        channel.exchangeDeclare(exchangeName, "direct");
        //创建临时队列
        String queue = channel.queueDeclare().getQueue();
        //临时队列和交换机绑定
        channel.queueBind(queue, exchangeName, "info");
        channel.queueBind(queue, exchangeName, "error");
        channel.queueBind(queue, exchangeName, "warning");
        //消费消息
        channel.basicConsume(queue, true, new DefaultConsumer(channel) {
    
    
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                                       byte[] body) throws IOException {
    
    
                System.out.println("消费者2:" + new String(body));
            }
        });
    }
}

猜你喜欢

转载自blog.csdn.net/qq_38530648/article/details/114037228