2.5 RabbitMQ高级特效-消费端的限流

什么是消费端的限流?
假设一个场景,首先我们RabbitMQ服务器有上万条未处理的消息,我们随便打开一个消费者客户端,会出现下面情况:巨量的消息瞬间全部图送过来,但是我们单个客户端无法同时处理这么多数据!
RabbitMQ提供了一种qos(服务质量保证)功能,即在非自动确认消息的前提下,如果一定数目的消息(通过基于consume或者channel设置Qos的值)未被确认前,不进行消费。

void BasicQos(unit prefecthSize,ushort prefetchCount,bool global);
prefecthSize:0
prefetchCount:会告诉RabbitMQ不要同时给一个消费者推送对于N个消息,即一旦有N个消息还没有ack,则该Consumer讲block掉,直到有消息ack
global:true/false是否将上面设置应用于channel,简单来说就是上面限制是channel级别还是consumer级别。

注意:
prefechSize和global这两项,RabbitMQ没有实现,暂不研究,prefetchCount在no_ask=false的情况下生效,即在自动应答的情况下这两个值是不生效的。

消费者端代码

package com.star.movie.limit;

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

import java.io.IOException;

/**
 * @Description:
 * @author:kaili
 * @Date: 2019-04-22 18:22
 **/
public class MyLimitConsumer extends DefaultConsumer {

    private Channel channel;

    public MyLimitConsumer(Channel channel) {
        super(channel);
        this.channel = channel;
    }

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

package com.star.movie.limit;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.star.movie.common.Constant;

/**
 * @Description:
 * @author:kaili
 * @Date: 2019-04-22 18:17
 **/
public class LimitConsumer {
    public static void main(String[] args) throws Exception {
        Connection connection = Constant.getConnection();
        Channel channel = connection.createChannel();
        String exchangeName = "test_qos_exchange";
        String queueName = "test_qos_queue";
        String routingKey = "qos.#";

        channel.exchangeDeclare(exchangeName, "topic", true, false, null);
        channel.queueDeclare(queueName, true, false, false, null);
        channel.queueBind(queueName, exchangeName, routingKey);

        //1 限流方式  第一件事就是 autoAck设置为 false
        channel.basicQos(0,1,false);

        channel.basicConsume(queueName,false,new MyLimitConsumer(channel));
    }
}

生产者代码

package com.star.movie.limit;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.star.movie.common.Constant;

/**
 * @Description:
 * @author:kaili
 * @Date: 2019-04-22 18:16
 **/
public class LimitProducer {
    public static void main(String[] args) throws Exception{
        Connection connection = Constant.getConnection();
        Channel channel = connection.createChannel();

        String exchange = "test_qos_exchange";
        String routingKey = "qos.save";

        String msg = "Hello RabbitMQ QOS Message";

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章