rabbitmq routing模型

本文介绍了RabbitMQ中直接交换机如何使用路由键来过滤消息,确保只有匹配路由键的队列才能接收消息。通过示例代码展示了如何创建发布者和两个消费者,一个消费者绑定错误路由键无法接收特定消息,而另一个消费者绑定多个路由键能正确接收不同类型的消息。
摘要由CSDN通过智能技术生成

订阅模型

希望发送的消息部分消费者消费,交换机与队列的绑定不能是任意绑定,而是指定一个routinkey,消息的发送方在向交换机发送消息时,必须指定消息的路由。

交换机不在把消息交给每一个绑定的队列,而是根据消息的routingkey进行判断,只能队列和消息的routingkey一致时,才会接收到消息。

In this setup, we can see the direct exchange X with two queues bound to it. The first queue is bound with binding key orange, and the second has two bindings, one with binding key black and the other one with green.

In such a setup a message published to the exchange with a routing key orange will be routed to queue Q1. Messages with a routing key of black or green will go to Q2. All other messages will be discarded.

相关代码

package com.baizhi.routing;


import com.baizhi.Utils.rabbitmqUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;

public class Provider {
    public static void main(String[] args) {
        Connection connection= rabbitmqUtils.getConnection();
        try {
            Channel channel=connection.createChannel();
            channel.exchangeDeclare("logs_direct", "direct");
            //
            String routingName="Info";
            channel.basicPublish("logs_direct", routingName, null, ("这是routingkey为"+routingName+"发布的消息").getBytes());
            rabbitmqUtils.closeChannlAndConnection(channel, connection);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
package com.baizhi.routing;

import com.baizhi.Utils.rabbitmqUtils;
import com.rabbitmq.client.*;

import java.io.IOException;

public class Customer1 {
    public static void main(String[] args) {
        Connection connection= rabbitmqUtils.getConnection();
        Channel channel= null;
        try {
            channel = connection.createChannel();
            channel.exchangeDeclare("logs_direct", "direct");
            String queueName=channel.queueDeclare().getQueue();
            channel.queueBind(queueName, "logs_direct", "error");
            channel.basicConsume(queueName, true, new DefaultConsumer(channel){
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    System.out.println("消费者1"+new String(body));
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
package com.baizhi.routing;

import com.baizhi.Utils.rabbitmqUtils;
import com.rabbitmq.client.*;

import java.io.IOException;

public class Customer2 {
    public static void main(String[] args) {
        Connection connection= rabbitmqUtils.getConnection();
        Channel channel= null;
        try {
            channel = connection.createChannel();
            channel.exchangeDeclare("logs_direct", "direct");
            String queueName=channel.queueDeclare().getQueue();
            //通道可以绑定多个路由
            channel.queueBind(queueName, "logs_direct", "Info");
            channel.queueBind(queueName, "logs_direct", "error");
            channel.queueBind(queueName, "logs_direct", "warning");

            channel.basicConsume(queueName, 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));
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

测试代码

provider提供Info信息,理论上消费者1无法接受,因为绑定的routingkey是error,消费者2可以正常接收。

 

 

provider提供error信息,消费者1,2都能接收。

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值