RabbitMQ-消费端限流

消费端限流

什么是消费端限流

假设一个场景,首先,我们RabbitMQ服务器上有上万条为处理的消息,我们随便打开一个消费者客户端,会出现
下面的情况:

巨量的消息瞬间全部推送过来,但是我们的单个客户端无法同时处理这么多数据,造成客户端宕机!

RabbitMQ消费端限流机制

  • RabbitMQ提供了一种QoS(服务质量保证)功能,即在非自动确认消息的前提下,如果一定数目的消息(通
    过基于Consume或者Channel设置的QoS值)未被确认前,不进行消费新的消息.

  • void basicQos(int prefetchSize, int prefetchCount, boolean global) throws IOException;

    • prefetchSize: 服务器将交付的最大内容量(以八进制为单位度量),如果没有限制,则设置为0

    • prefetchCount: 会告诉RabbitMQ不要同时给一个消费者推送多于prefetchCount个消息,即一旦有
      prefetchCount个消息未ack,则该Consumer将block掉,直到有消息ack.

    • global:true/false是否将上面的设置应用于Channel;简单点说,就是上面限制是Channel级别还是
      Consumer级别.

    • prefetchSize和global这两项,RabbitMQ没有实现,暂且不研究prefetchCount在no_ack=false的情况
      下生效,即在自动应答的情况下这两个值是不生效的.

RabbitMQ消费端限流代码演示

自定义消费者


import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

import java.io.IOException;

/**
 * @Classname MyConsumer
 * @Description TODO
 * @Date 2019/9/8 13:15
 * @Created by Jiavg
 */
public class MyConsumer extends DefaultConsumer {
    /**
     * Constructs a new instance and records its association to the passed-in channel.
     * @param channel the channel to which this consumer is attached
     */
    public MyConsumer(Channel channel) {
        super(channel);
        this.channel = channel;
    }

    private Channel channel;

    @Override
    public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
        System.out.println("---------consume message---------");
        System.out.println("consumerTag:" + consumerTag);
        System.out.println("envelope:" + envelope);
        System.out.println("properties:" + properties);
        System.out.println("body:" + new String(body));

        // 延时ack,便于演示消费端限流
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        channel.basicAck(envelope.getDeliveryTag(), false);
    }
}

消费端代码


import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * @Classname Consumer
 * @Description 消费端限流:消费者
 * @Date 2019/9/8 10:48
 * @Created by Jiavg
 */
public class Consumer {

    public static void main(String[] args) throws Exception{

        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");

        Connection connection = connectionFactory.newConnection();

        Channel channel = connection.createChannel();

        String exchangeName = "test_QoS_Exchange";
        String routingKey = "QoS.#";
        String queueName = "test_QoS_Queue";

        // 声明交换机、声明队列、绑定队列到交换机
        channel.exchangeDeclare(exchangeName, "topic", true, false, null);
        channel.queueDeclare(queueName, true, false, false, null);
        channel.queueBind(queueName, exchangeName, routingKey);

        // 限流方式
        channel.basicQos(0, 1, false);

        // autoAck设置为false
        channel.basicConsume(queueName, false, new MyConsumer(channel));        
    }   
}

启动消费端-管控台变化

交换机变化

交换机绑定情况

生产端代码


import com.rabbitmq.client.*;

/**
 * @Classname Producer
 * @Description 消费端限流:生产者
 * @Date 2019/9/8 10:50
 * @Created by Jiavg
 */
public class Producer {

    public static void main(String[] args) throws Exception{

        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");

        Connection connection = connectionFactory.newConnection();

        Channel channel = connection.createChannel();

        String exchangeName = "test_QoS_Exchange";
        String routingKey = "QoS.save";
        String msg = "Hello World send QoS Message";

        for (int i = 0; i < 4; i++){
            channel.basicPublish(exchangeName, routingKey, null, msg.getBytes());
        }       
    }   
}

生产端运行-管控台变化

Overview页面变化

由图可知,由于我们自定义消费者在处理消息的方法中(handleDelivery),使用Thread.sleep(10000);延
迟10秒ack,故在Overview页面显示每10秒才会消费一条消息(只有在Broker接收到消费端的ack才会发送下一
条消息).


   转载规则


《RabbitMQ-消费端限流》 Jiavg 采用 知识共享署名 4.0 国际许可协议 进行许可。
  目录