Spring Boot RabbitMq 併發與限流

概述


電商中秒殺請求,屬於瞬間大流量,同一時刻會有大量的請求涌入到系統中,可能導致系統掛掉。應付這種瞬間大流量的其中一種方式,便是利用消息隊列

1、利用消息隊列先進先出的特性,將請求進行削峯;
2、控制好消費端的消費速度,進行必要的限流。

在消費端,要做到上面提到的第2點,在Spring Boot RabbitMQ中只需要利用@RabbitListener註解,做一些簡單配置就可以了。


一個listener對應多個consumer


默認情況一下,一個listener對應一個consumer,如果想對應多個,有兩種方式。

方式一:直接在application.yml文件中配置

spring:
  rabbitmq:
    listener:
      simple:
        concurrency: 5
        max-concurrency: 10

這個是個全局配置,應用裏的任何隊列對應的listener都至少有5個consumer,但是千萬別這麼做,因爲一般情況下,一個listener對應一個consumer是夠用的。只是針對部分場景,才需要一對多。

 

方式二:直接在@RabbitListener上配置

@Component
public class SpringBootMsqConsumer {
    @RabbitListener(queues = "spring-boot-direct-queue",concurrency = "5-10")
    public void receive(Message message) {
        System.out.println("receive message:" + new String(message.getBody()));
    }
}

利用@RabbitListener中的concurrency屬性進行指定就行。例如上面的

concurrency = “5-10”

就表示最小5個,最大10個consumer。啓動消費端應用後,找到spring-boot-direct-queue這個隊列的consumer,會發現有5個。

在這裏插入圖片描述

這5個消費者都可以從spring-boot-direct-queue這個隊列中獲取消息,加快隊列中消息的消費速度,提高吞吐量。


限流


我們經過壓測,來判斷consumer的消費能力,如果單位時間內,consumer到達的消息太多,也可能把消費者壓垮。
得到壓測數據後,可以在@RabbitListener中配置prefetch count

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class SpringBootMsqConsumer {
    @RabbitListener(queues = "spring-boot-direct-queue",concurrency = "5-10",containerFactory = "mqConsumerlistenerContainer")
    public void receive(Message message) {
        System.out.println("receive message:" + new String(message.getBody()));
    }
}

只需要在@RabbitListener中,用containerFactory指定一個監聽器工廠類就行。這裏用的是:

containerFactory = “mqConsumerlistenerContainer”

定義監聽器工廠類很簡單。

import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMqConfig {

    @Autowired
    private CachingConnectionFactory connectionFactory;

    @Bean(name = "mqConsumerlistenerContainer")
    public SimpleRabbitListenerContainerFactory mqConsumerlistenerContainer(){
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setPrefetchCount(50);
        return factory;
    }
}

上面有一句factory.setPrefetchCount(50);,就是用於設置prefetch count的,啓動後,會在spring-boot-direct-queue隊列的consumer中體現出來。
在這裏插入圖片描述
配置成功後,consumer單位時間內接收到消息就是50條。


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