最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot基于Disruptor實現(xiàn)高效的消息隊列?

 更新時間:2024年02月22日 09:04:48   作者:wx59bcc77095d22  
Disruptor是一個開源的Java框架,它被設(shè)計用于在生產(chǎn)者-消費者問題上獲得盡量高的吞吐量和盡量低的延遲,本文主要介紹了SpringBoot基于Disruptor實現(xiàn)高效的消息隊列?,具有一定的參考價值,感興趣的可以了解一下

一、前言

Disruptor是一個開源的Java框架,它被設(shè)計用于在生產(chǎn)者-消費者問題上獲得盡量高的吞吐量和盡量低的延遲,從功能上來看Disruptor是實現(xiàn)了隊列的功能,而且是一個有界隊列。那么它的應用場景自然就是“生產(chǎn)者-消費者”模型的應用場合了。Disruptor 是在內(nèi)存中以隊列的方式去實現(xiàn)的,而且是無鎖的。這也是 Disruptor 為什么高效的原因。

二、SpringBoot整合Disruptor

1.添加依賴

<!--Disruptor-->
<dependency>
    <groupId>com.lmax</groupId>
    <artifactId>disruptor</artifactId>
    <version>3.4.4</version>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>

2.創(chuàng)建消息體實體

package com.example.aopdemo.disruptor;

import lombok.Data;

/**
 * @author qx
 * @date 2024/2/21
 * @des 消息體
 */
@Data
public class MessageModel {

    private String message;

}

3.創(chuàng)建事件工廠類

package com.example.aopdemo.disruptor;

import com.lmax.disruptor.EventFactory;

/**
 * @author qx
 * @date 2024/2/21
 * @des 事件工廠類
 */
public class MessageEventFactory implements EventFactory<MessageModel> {
    @Override
    public MessageModel newInstance() {
        return new MessageModel();
    }
}

4.創(chuàng)建消費者

package com.example.aopdemo.disruptor;

import com.lmax.disruptor.EventHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * @author qx
 * @date 2024/2/21
 * @des 消息消費者
 */
@Slf4j
public class MessageEventHandler implements EventHandler<MessageModel> {
    @Override
    public void onEvent(MessageModel messageModel, long sequence, boolean endOfBatch) {
        log.info("消費者獲取消息:{}", messageModel);
    }
}

5.構(gòu)造BeanManager

package com.example.aopdemo.disruptor;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author qx
 * @date 2024/2/21
 * @des
 */
@Component
public class BeanManager implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        BeanManager.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static Object getBean(String name) {
        return applicationContext.getBean(name);
    }

    public static <T> T getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }
}

6.創(chuàng)建消息管理器

package com.example.aopdemo.disruptor;

import com.lmax.disruptor.BlockingWaitStrategy;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author qx
 * @date 2024/2/21
 * @des 事件管理器
 */
@Configuration
public class MessageManager {

    @Bean("messageModel")
    public RingBuffer<MessageModel> messageModelRingBuffer() {
        // 定義線程池
        ExecutorService executorService = Executors.newFixedThreadPool(2);

        // 指定事件工廠
        MessageEventFactory factory = new MessageEventFactory();

        // 指定ringbuffer字節(jié)大小,必須為2的N次方(能將求模運算轉(zhuǎn)為位運算提高效率),否則將影響效率
        int bufferSize = 1024 * 256;

        //單線程模式,獲取額外的性能
        Disruptor<MessageModel> disruptor = new Disruptor<>(factory, bufferSize, executorService, ProducerType.SINGLE, new BlockingWaitStrategy());

        //設(shè)置事件業(yè)務處理器---消費者
        disruptor.handleEventsWith(new MessageEventHandler());

        //啟動disruptor線程
        disruptor.start();

        //獲取ringbuffer環(huán),用于接取生產(chǎn)者生產(chǎn)的事件
        return disruptor.getRingBuffer();
    }

}

7.創(chuàng)建生產(chǎn)者

package com.example.aopdemo.disruptor;

import com.lmax.disruptor.RingBuffer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author qx
 * @date 2024/2/21
 * @des 生產(chǎn)者
 */
@Service
@Slf4j
public class DisruptorService {

    @Autowired
    private RingBuffer<MessageModel> messageModelRingBuffer;

    public void sayMessage(String message) {
        // 獲取下一個Event槽的下標
        long sequence = messageModelRingBuffer.next();
        try {
            // 填充數(shù)據(jù)
            MessageModel messageModel = messageModelRingBuffer.get(sequence);
            messageModel.setMessage(message);
            log.info("往消息隊列中添加消息:{}", messageModel);
        } catch (Exception e) {
            log.error("failed to add event to messageModelRingBuffer for : e = {},{}", e, e.getMessage());
        } finally {
            //發(fā)布Event,激活觀察者去消費,將sequence傳遞給改消費者
            //注意最后的publish方法必須放在finally中以確保必須得到調(diào)用;如果某個請求的sequence未被提交將會堵塞后續(xù)的發(fā)布操作或者其他的producer
            messageModelRingBuffer.publish(sequence);
        }

    }

}

8.創(chuàng)建測試類

package com.example.aopdemo.controller;

import com.example.aopdemo.disruptor.DisruptorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author qx
 * @date 2024/2/21
 * @des Disruptor測試
 */
@RestController
public class DisruptorController {

    @Autowired
    private DisruptorService disruptorService;

    @GetMapping("/disruptor")
    public String disruptorTest(String message) {
        disruptorService.sayMessage(message);
        return "發(fā)送消息成功";
    }
}

9.測試

啟動程序,在瀏覽器訪問請求連接進行測試。

我們在控制臺上可以獲取到消息的發(fā)送和接收信息。

2024-02-21 15:22:16.059  INFO 6788 --- [nio-8080-exec-1] c.e.aopdemo.disruptor.DisruptorService   : 往消息隊列中添加消息:MessageModel(message=hello)
2024-02-21 15:22:16.060  INFO 6788 --- [pool-1-thread-1] c.e.a.disruptor.MessageEventHandler      : 消費者獲取消息:MessageModel(message=hello)

到此這篇關(guān)于SpringBoot基于Disruptor實現(xiàn)高效的消息隊列 的文章就介紹到這了,更多相關(guān)SpringBoot Disruptor消息隊列內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot文件訪問映射如何實現(xiàn)

    SpringBoot文件訪問映射如何實現(xiàn)

    這篇文章主要介紹了SpringBoot文件訪問映射如何實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-02-02
  • springBoot3 生成訂單號的示例代碼

    springBoot3 生成訂單號的示例代碼

    本文主要介紹了springBoot3 生成訂單號的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2025-10-10
  • 使用linux部署Spring Boot程序

    使用linux部署Spring Boot程序

    springboot程序在linux服務器上應該怎么部署?這次就分享下linux下如何正確部署springboot程序,感興趣的朋友一起看看吧
    2018-01-01
  • Java Fluent Mybatis 聚合查詢與apply方法詳解流程篇

    Java Fluent Mybatis 聚合查詢與apply方法詳解流程篇

    Java中常用的ORM框架主要是mybatis, hibernate, JPA等框架。國內(nèi)又以Mybatis用的多,基于mybatis上的增強框架,又有mybatis plus和TK mybatis等。今天我們介紹一個新的mybatis增強框架 fluent mybatis關(guān)于聚合查詢、apply方法詳解
    2021-10-10
  • springboot捕獲全局異常實現(xiàn)過程

    springboot捕獲全局異常實現(xiàn)過程

    本文主要介紹了Java中的異常和錯誤,包括Exception和Error的區(qū)別、如何捕捉全局異常、自定義異常的實現(xiàn)等,通過實例代碼和步驟,展示了如何在Spring?Boot項目中實現(xiàn)全局異常處理,并自定義異常類來增強程序的健壯性
    2026-03-03
  • SpringBoot查看項目配置信息的幾種常見方法

    SpringBoot查看項目配置信息的幾種常見方法

    這篇文章主要為大家詳細介紹了查看Spring Boot項目所有配置信息的幾種方法,包括 Actuator端點,日志輸出,代碼級獲取等方式并附帶詳細步驟和示例,希望對大家有一定的幫助
    2025-04-04
  • 如何利用Java遞歸解決“九連環(huán)”公式

    如何利用Java遞歸解決“九連環(huán)”公式

    這篇文章主要給大家介紹了關(guān)于如何利用Java遞歸解決“九連環(huán)”公式的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-02-02
  • Spring Boot的filter(過濾器)簡單使用實例詳解

    Spring Boot的filter(過濾器)簡單使用實例詳解

    過濾器(Filter)的注冊方法和 Servlet 一樣,有兩種方式:代碼注冊或者注解注冊,下面通過實例給大家介紹Spring Boot的filter(過濾器)簡單使用,一起看看吧
    2017-04-04
  • idea項目一直在build的問題分析及解決

    idea項目一直在build的問題分析及解決

    IDEA項目構(gòu)建緩慢的原因可能包括構(gòu)建進程堆大小過小、緩存問題、依賴包下載緩慢或網(wǎng)絡問題,解決方法包括增加構(gòu)建進程堆大小、清理緩存、配置國內(nèi)鏡像和檢查網(wǎng)絡連接
    2025-11-11
  • 寧可用Lombok也不把成員設(shè)置為public原理解析

    寧可用Lombok也不把成員設(shè)置為public原理解析

    這篇文章主要為大家介紹了寧可用Lombok也不把成員設(shè)置為public原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03

最新評論

郓城县| 惠水县| 大关县| 读书| 清涧县| 商城县| 湖南省| 威远县| 凤庆县| 玉树县| 平山县| 钟祥市| 合水县| 城步| 右玉县| 柳林县| 永川市| 潮安县| 新兴县| 吴忠市| 克拉玛依市| 绩溪县| 兴仁县| 鄢陵县| 龙岩市| 兴化市| 黔江区| 克拉玛依市| 上林县| 精河县| 江阴市| 永城市| 昭通市| 乐陵市| 乌审旗| 建阳市| 鹤山市| 乌拉特中旗| 罗城| 阳春市| 安平县|