RabbitMQ six kinds queue mode - routing mode

Foreword

RabbitMQ six kinds queue mode - Simple Queue
RabbitMQ six kinds queue mode - work queue
RabbitMQ six kinds queue mode - Publish Subscribe
RabbitMQ six kinds queue mode - routing mode [paper]
RabbitMQ six kinds queue mode - Themes

This article take you to find RabbitMQ queue mode routing mode.

In fact, after just read the article on the release mode, I believe routing mode is very easy to get started, the only gap is two parameters, exchange type and routingKey.

Article Directory

1. What is routing mode 2. Code section 2.1 log producers 2.2 info consumer 2.3 error consumers 2.4 run shot 3. Models for routing

1. What is the routing mode

Official website link: https://msd.misuland.com/pd/2884250137616455578

Routing mode with a similar subscription model release, then the subscription model on the basis of the type plus the subscription model is bound to be distributed to all switches queue, routing mode is only distributed to key bindings specified route in the switch above the queue, we you can look at the picture below:

P represents for producers, X represents a switch, it indicates that the queue C1C2 represented consumers, red.

The figure is a combination of log consumption levels with maps, routing mode it will route the message to those Queue binding key exact match with the routing key in this mode is the direct mode Exchange mode.

以上图的配置为例,我们以 routingKey="error" 发送消息到 Exchange,则消息会路由到Queue1(amqp.gen-S9b…,这是由RabbitMQ自动生成的Queue名称)和Queue2(amqp.gen-Agl…)。如果我们以 routingKey="info" 或 routingKey="warning" 来发送消息,则消息只会路由到 Queue2。如果我们以其他 routingKey 发送消息,则消息不会路由到这两个 Queue 中。

相对于发布订阅模式,我们可以看到不再是广播似的接收全部消息,而是有选择性的消费。

我们就以接收不同日志级别的队列为例吧。

2. 代码部分

2.1 日志生产者

public class ProdecerRouting {

    private static final String EXCHANGE_NAME = "my_fanout_exchange";

    public static void main(String[] args) throws IOException, TimeoutException {
        /** 1.创建新的连接 */
        Connection connection = MQConnectionUtils.newConnection();
        /** 2.创建通道 */
        Channel channel = connection.createChannel();
        /** 3.绑定的交换机 参数1交互机名称 参数2 exchange类型 */
        channel.exchangeDeclare(EXCHANGE_NAME, "direct");
        /** 4.发送消息 */
        String message = "",sendType="";
        for (int i = 0; i < 10; i++)
        {
            if(i%2==0){
                sendType = "info";
                message = "我是 info 级别的消息类型:" + i;
            }else{
                sendType = "error";
                message = "我是 error 级别的消息类型:" + i;
            }
            System.out.println("[send]:" + message + "  " +sendType);
            channel.basicPublish(EXCHANGE_NAME, sendType, null, message.getBytes("utf-8"));
            try {
                Thread.sleep(5 * i);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        /** 5.关闭通道、连接 */
        channel.close();
        connection.close();
        /** 注意:如果消费没有绑定交换机和队列,则消息会丢失 */
    }

}

注意:exchangeDeclare() 方法 exchange 类型为 direct

2.2 info消费者

public class ConsumerInfo {

    private static final String QUEUE_NAME = "consumer_info";
    private static final String EXCHANGE_NAME = "my_fanout_exchange";

    public static void main(String[] args) throws IOException, TimeoutException {
        System.out.println("info消费者启动");
        /* 1.创建新的连接 */
        Connection connection = MQConnectionUtils.newConnection();
        /* 2.创建通道 */
        Channel channel = connection.createChannel();
        /* 3.消费者关联队列 */
        channel.queueDeclare(QUEUE_NAME, falsefalsefalsenull);
        /* 4.消费者绑定交换机 参数1 队列 参数2交换机 参数3 routingKey */
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "info");
        DefaultConsumer 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("消费者获取生产者消息:" + msg);
            }
        };
        /* 5.消费者监听队列消息 */
        channel.basicConsume(QUEUE_NAME, true, consumer);
    }

}

2.3 error消费者

public class ConsumerError {

    private static final String QUEUE_NAME = "consumer_error";
    private static final String EXCHANGE_NAME = "my_fanout_exchange";

    public static void main(String[] args) throws IOException, TimeoutException {
        System.out.println("error消费者启动");
        /* 1.创建新的连接 */
        Connection connection = MQConnectionUtils.newConnection();
        /* 2.创建通道 */
        Channel channel = connection.createChannel();
        /* 3.消费者关联队列 */
        channel.queueDeclare(QUEUE_NAME, falsefalsefalsenull);
        /* 4.消费者绑定交换机 参数1 队列 参数2交换机 参数3 routingKey */
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "error");
        DefaultConsumer 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("消费者获取生产者消息:" + msg);
            }
        };
        /* 5.消费者监听队列消息 */
        channel.basicConsume(QUEUE_NAME, true, consumer);
    }

}

2.4 运行截图

先运行两个消费者,再运行生产者。如果没有提前将队列绑定到交换机,那么直接运行生产者的话,消息是不会发到任何队列里的。

生产者

info消费者

error消费者

3. 路由模式总结

1、两个队列消费者设置的路由不一样,接收到的消息就不一样。路由模式下,决定消息向队列推送的主要取决于路由,而不是交换机了。  

2、该模式必须设置交换机,且声明路由模式 channel.exchangeDeclare(EXCHANGE_NAME, "direct");

生产者发送消息到交换机,同时定义了一个路由 routingKey,多个消费者声明多个队列,与交换机进行绑定,同时定义路由 routingKey,只有路由 routingKey相同的消费者才能消费数据

案例代码:https://www.lanzous.com/i5ydu6d

我创建了一个java相关的公众号,用来记录自己的学习之路,感兴趣的小伙伴可以关注一下微信公众号哈:niceyoo

Guess you like

Origin www.cnblogs.com/niceyoo/p/11448093.html