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());
        }
    }
}

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