5、RabbitMQ学习笔记—Exchange交换机

Exchange交换机

接收消息,并根据路由键转发消息所绑定的队列
5、RabbitMQ学习笔记—Exchange交换机_第1张图片
这是官网的一个交换机模型图,其中:

  • 蓝色框:表示客户端(client)发送消息到交换机(exchange)并通过一定路由规则(routingkey)进行转发到消息队列(queue)中。
  • 绿色框:表示消息消费者从队列(queue)中获取消息进行处理消费。
  • 红色框:表示rabbitmq服务包含的内容
  • 黄色框:交换机和消息队列之间的绑定关系(routingkey)

属性

  • name 交换机名称
  • type 交换机类型 direct、topic、fanout、headers
  • durability 是否需要持久化,boolen值
  • autodelete 当最后一个绑定到exchange上的队列删除后,自动删除该exchange
  • internal 当前exchange是否用于rabbitmq内部使用,默认false
  • arguments 扩展参数,用于扩展AMQP协议自制定化使用

交换机类型

1、direct
所有发送到direct exchange的消息被转发到routingkey中指定的queue

注意
direct模式可以使用rabbitmq自带的exchange:default exchange,所以不需要将exchange进行任何的绑定(binding)操作,消息传递时,routingkey必须完全匹配才会被队列接受,否则该消息会被抛弃。
5、RabbitMQ学习笔记—Exchange交换机_第2张图片
生产者

 public static final String EXCHANGE_NAME = "test_exchange_direct";
    public static final String ROUTING_KEY = "routingkey.direct";
    public static void main(String[] args) throws IOException, TimeoutException {
        // 获取连接
        Connection connection = ConnectionsUtils.getConnection();
        // 创建信道
        Channel channel = connection.createChannel();
        // 发送消息
        String message = "test message for exchange_direct";
        channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY, null, message.getBytes());
        System.out.println("发送成功:" + message);
        channel.close();
        connection.close();
    }

消费者

public static final String EXCHANGE_NAME = "test_exchange_direct";
    public static final String EXCHANGE_TYPE = "direct";
    public static final String ROUTING_KEY = "routingkey.direct";
    public static final String QUEUE_NAME = "test_queue";
    public static void main(String[] args) throws IOException, TimeoutException {
        // 获取连接
        Connection connection = ConnectionsUtils.getConnection();
        // 创建信道
        Channel channel = connection.createChannel();
        // 声明交换机
        channel.exchangeDeclare(EXCHANGE_NAME, EXCHANGE_TYPE, true, false, false ,null);
        // 声明队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        // 绑定
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY);
        // 创建消费者进行监听获取消息
        DefaultConsumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("接收消息message: " + new String(body));
                System.out.println("consumerTag: " + consumerTag);
                System.out.println("envelope: " + envelope);
                System.out.println("properties: " + properties);
            }
        };
        channel.basicConsume(QUEUE_NAME, true, consumer);
    }

首先运行消费者,启动监听。再运行生产者,此时查看消费者控制台,可看到已接收到消息
在这里插入图片描述
如果修改其中任一的routingkey,使得两者routingkey不一致,那么再次执行上述步骤,消费者则无法接受到消息。
2、topic
所有发送到topic exchange的消息被转发到所有关心routingkey中指定topic的queue上
exchange将routingkey 和某个topic进行模糊匹配,此时队列需要绑定一个topic

可以使用通配符

5、RabbitMQ学习笔记—Exchange交换机_第3张图片
5、RabbitMQ学习笔记—Exchange交换机_第4张图片
创建生产者

    public static final String EXCHANGE_NAME = "test_exchange_topic";
    public static final String ROUTING_KEY1 = "routingkey.topic";
    public static final String ROUTING_KEY2 = "routingkey.topic.a";
    public static final String ROUTING_KEY3 = "routingkey2.topic";
    public static void main(String[] args) throws IOException, TimeoutException {
        // 获取连接
        Connection connection = ConnectionsUtils.getConnection();
        // 创建信道
        Channel channel = connection.createChannel();
        // 发送消息
        String message1 = "test message for exchange_topic 1";
        String message2 = "test message for exchange_topic 2";
        String message3 = "test message for exchange_topic 3";
        channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY1, null, message1.getBytes());
        channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY2, null, message2.getBytes());
        channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY3, null, message3.getBytes());
        System.out.println("发送成功");
        channel.close();
        connection.close();
    }

创建三个消费者,分别使用不同的routingkey

// 消费者1 接受消息1和消息2
public static final String EXCHANGE_NAME = "test_exchange_topic";
    public static final String EXCHANGE_TYPE = "topic";
    public static final String ROUTING_KEY = "routingkey.#";
    public static final String QUEUE_NAME = "test_queue1";
    public static void main(String[] args) throws IOException, TimeoutException {
        // 获取连接
        Connection connection = ConnectionsUtils.getConnection();
        // 创建信道
        Channel channel = connection.createChannel();
        // 声明交换机
        channel.exchangeDeclare(EXCHANGE_NAME, EXCHANGE_TYPE, true, false, false ,null);
        // 声明队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        // 绑定
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY);
        // 创建消费者进行监听获取消息
        DefaultConsumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("接收消息message: " + new String(body));
                System.out.println("consumerTag: " + consumerTag);
                System.out.println("envelope: " + envelope);
                System.out.println("properties: " + properties);
            }
        };
        channel.basicConsume(QUEUE_NAME, true, consumer);
    }
// 消费者2 接受消息1
    public static final String EXCHANGE_NAME = "test_exchange_topic";
    public static final String EXCHANGE_TYPE = "topic";
    public static final String ROUTING_KEY = "routingkey.*";
    public static final String QUEUE_NAME = "test_queue2";
    public static void main(String[] args) throws IOException, TimeoutException {
        // 获取连接
        Connection connection = ConnectionsUtils.getConnection();
        // 创建信道
        Channel channel = connection.createChannel();
        // 声明交换机
        channel.exchangeDeclare(EXCHANGE_NAME, EXCHANGE_TYPE, true, false, false ,null);
        // 声明队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        // 绑定
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY);
        // 创建消费者进行监听获取消息
        DefaultConsumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("接收消息message: " + new String(body));
                System.out.println("consumerTag: " + consumerTag);
                System.out.println("envelope: " + envelope);
                System.out.println("properties: " + properties);
            }
        };
        channel.basicConsume(QUEUE_NAME, true, consumer);
    }
// 消费者3 接受消息1和消息3
public static final String EXCHANGE_NAME = "test_exchange_topic";
    public static final String EXCHANGE_TYPE = "topic";
    public static final String ROUTING_KEY = "*.topic";
    public static final String QUEUE_NAME = "test_queue3";
    public static void main(String[] args) throws IOException, TimeoutException {
        // 获取连接
        Connection connection = ConnectionsUtils.getConnection();
        // 创建信道
        Channel channel = connection.createChannel();
        // 声明交换机
        channel.exchangeDeclare(EXCHANGE_NAME, EXCHANGE_TYPE, true, false, false ,null);
        // 声明队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        // 绑定
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY);
        // 创建消费者进行监听获取消息
        DefaultConsumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("接收消息message: " + new String(body));
                System.out.println("consumerTag: " + consumerTag);
                System.out.println("envelope: " + envelope);
                System.out.println("properties: " + properties);
            }
        };
        channel.basicConsume(QUEUE_NAME, true, consumer);
    }

我们分别启动三个消费者,然后启动生产者后,查看三个消费者的控制台输出
5、RabbitMQ学习笔记—Exchange交换机_第5张图片
5、RabbitMQ学习笔记—Exchange交换机_第6张图片
5、RabbitMQ学习笔记—Exchange交换机_第7张图片
3、fanout
不处理任何路由键,只需要简单的将队列绑定到交换机上
发送到交换机的消息都会被转发到与该交换机绑定的所有队列上
fanout交换机转发消息的效率是最快的
5、RabbitMQ学习笔记—Exchange交换机_第8张图片
生产者

public static final String EXCHANGE_NAME = "test_exchange_fanout";
    public static final String ROUTING_KEY1 = "routingkey.fanout";
    public static final String ROUTING_KEY2 = "routingkey.fanout.a";
    public static final String ROUTING_KEY3 = "routingkey2.fanout";
    public static void main(String[] args) throws IOException, TimeoutException {
        // 获取连接
        Connection connection = ConnectionsUtils.getConnection();
        // 创建信道
        Channel channel = connection.createChannel();
        // 发送消息
        String message1 = "test message for exchange_fanout 1";
        String message2 = "test message for exchange_fanout 2";
        String message3 = "test message for exchange_fanout 3";
        channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY1, null, message1.getBytes());
        channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY2, null, message2.getBytes());
        channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY3, null, message3.getBytes());
        System.out.println("发送成功");
        channel.close();
        connection.close();
    }

消费者1

public static final String EXCHANGE_NAME = "test_exchange_fanout";
    public static final String EXCHANGE_TYPE = "fanout";
    public static final String ROUTING_KEY = "routingkey.#";
    public static final String QUEUE_NAME = "test_queue1";
    public static void main(String[] args) throws IOException, TimeoutException {
        // 获取连接
        Connection connection = ConnectionsUtils.getConnection();
        // 创建信道
        Channel channel = connection.createChannel();
        // 声明交换机
        channel.exchangeDeclare(EXCHANGE_NAME, EXCHANGE_TYPE, true, false, false ,null);
        // 声明队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        // 绑定
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY);
        // 创建消费者进行监听获取消息
        DefaultConsumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("接收消息message: " + new String(body));
                System.out.println("consumerTag: " + consumerTag);
                System.out.println("envelope: " + envelope);
                System.out.println("properties: " + properties);
            }
        };
        channel.basicConsume(QUEUE_NAME, true, consumer);
    }

消费者2

public static final String EXCHANGE_NAME = "test_exchange_fanout";
    public static final String EXCHANGE_TYPE = "fanout";
    public static final String ROUTING_KEY = "routingkey.*";
    public static final String QUEUE_NAME = "test_queue2";
    public static void main(String[] args) throws IOException, TimeoutException {
        // 获取连接
        Connection connection = ConnectionsUtils.getConnection();
        // 创建信道
        Channel channel = connection.createChannel();
        // 声明交换机
        channel.exchangeDeclare(EXCHANGE_NAME, EXCHANGE_TYPE, true, false, false ,null);
        // 声明队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        // 绑定
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY);
        // 创建消费者进行监听获取消息
        DefaultConsumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("接收消息message: " + new String(body));
                System.out.println("consumerTag: " + consumerTag);
                System.out.println("envelope: " + envelope);
                System.out.println("properties: " + properties);
            }
        };
        channel.basicConsume(QUEUE_NAME, true, consumer);
    }

消费者3

public static final String EXCHANGE_NAME = "test_exchange_fanout";
    public static final String EXCHANGE_TYPE = "fanout";
    public static final String ROUTING_KEY = "*.fanout";
    public static final String QUEUE_NAME = "test_queue3";
    public static void main(String[] args) throws IOException, TimeoutException {
        // 获取连接
        Connection connection = ConnectionsUtils.getConnection();
        // 创建信道
        Channel channel = connection.createChannel();
        // 声明交换机
        channel.exchangeDeclare(EXCHANGE_NAME, EXCHANGE_TYPE, true, false, false ,null);
        // 声明队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        // 绑定
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY);
        // 创建消费者进行监听获取消息
        DefaultConsumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("接收消息message: " + new String(body));
                System.out.println("consumerTag: " + consumerTag);
                System.out.println("envelope: " + envelope);
                System.out.println("properties: " + properties);
            }
        };
        channel.basicConsume(QUEUE_NAME, true, consumer);
    }

因为fanout模式不会处理任何的路由键,因此,这里我们将exchange模式设置为fanout后,无论设置routing可以与否,都会将消息发送到每个绑定该exchange的队列上去,因此,三个消费者都会接收到三条消息
5、RabbitMQ学习笔记—Exchange交换机_第9张图片
5、RabbitMQ学习笔记—Exchange交换机_第10张图片
5、RabbitMQ学习笔记—Exchange交换机_第11张图片

你可能感兴趣的:(RabbitMQ)