springboot中怎么利用rabbitmq實現(xiàn)限流與并發(fā)

springboot中怎么利用rabbitmq實現(xiàn)限流與并發(fā),相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

創(chuàng)新互聯(lián)專注于成都網站設計、成都做網站、網頁設計、網站制作、網站開發(fā)。公司秉持“客戶至上,用心服務”的宗旨,從客戶的利益和觀點出發(fā),讓客戶在網絡營銷中找到自己的駐足之地。尊重和關懷每一位客戶,用嚴謹的態(tài)度對待客戶,用專業(yè)的服務創(chuàng)造價值,成為客戶值得信賴的朋友,為客戶解除后顧之憂。

一 并發(fā)

步驟:

1 、在rabbitListener中配置concurency=“min-max”

如下代表最小并發(fā)數是5

@Component
public class pricon {
    @RabbitListener(queues ="textQueue",concurrency = "5-10")
    public void hand(String str){
        System.out.println(Thread.currentThread().getName()+"接受到了一個消息:"+str+"現(xiàn)在時間:"+System.currentTimeMillis()/1000);
    }
}

2、測試

@Component
public class priConsumer {
    private static final String EXCHANGE = "textExchange";

    public static final String QUEUE = "textQueue";

    private static final String ROUTING_KEY = "textQueue";
    @Autowired
    RabbitTemplate template;
    public void test(){
        for(int i=50;i>1;i--){
            int finalI = i;
            template.convertAndSend(EXCHANGE,ROUTING_KEY,"queue:"+i);
        }

    }
}


3、在rabbitmq的控制面板中會顯示:

springboot中怎么利用rabbitmq實現(xiàn)限流與并發(fā)

二 限流

1、 配置Bean

setPrefetchCount 表示單位時間最多能處理多少消息

@Autowired
    CachingConnectionFactory connectionFactory;
    @Bean(name = "mqlistenerContainer")
    public SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory(){
        SimpleRabbitListenerContainerFactory factory=new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setPrefetchCount(10);
        return factory;
    }

2、在rabbitListener中配置工廠

 @RabbitListener(queues ="textQueue",concurrency = "5-10",containerFactory = "mqlistenerContainer")
    public void hand(String str){
        System.out.println(Thread.currentThread().getName()+"接受到了一個消息:"+str+"現(xiàn)在時間:"+System.currentTimeMillis()/1000);
    }

在控制面板中會顯示:

springboot中怎么利用rabbitmq實現(xiàn)限流與并發(fā)

springboot中怎么利用rabbitmq實現(xiàn)限流與并發(fā)

3結果:

springboot中怎么利用rabbitmq實現(xiàn)限流與并發(fā)

看完上述內容,你們掌握springboot中怎么利用rabbitmq實現(xiàn)限流與并發(fā)的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

分享文章:springboot中怎么利用rabbitmq實現(xiàn)限流與并發(fā)
網頁地址:http://bm7419.com/article0/jcsdoo.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供Google品牌網站設計、App設計、自適應網站、手機網站建設、搜索引擎優(yōu)化

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)