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

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

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

一、前言

Disruptor是一個(gè)開源的Java框架,它被設(shè)計(jì)用于在生產(chǎn)者-消費(fèi)者問題上獲得盡量高的吞吐量和盡量低的延遲,從功能上來看Disruptor是實(shí)現(xiàn)了隊(duì)列的功能,而且是一個(gè)有界隊(duì)列。那么它的應(yīng)用場(chǎng)景自然就是“生產(chǎn)者-消費(fèi)者”模型的應(yīng)用場(chǎng)合了。Disruptor 是在內(nèi)存中以隊(duì)列的方式去實(shí)現(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)建消息體實(shí)體

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)建消費(fèi)者

package com.example.aopdemo.disruptor;

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

/**
 * @author qx
 * @date 2024/2/21
 * @des 消息消費(fèi)者
 */
@Slf4j
public class MessageEventHandler implements EventHandler<MessageModel> {
    @Override
    public void onEvent(MessageModel messageModel, long sequence, boolean endOfBatch) {
        log.info("消費(fèi)者獲取消息:{}", 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次方(能將求模運(yùn)算轉(zhuǎn)為位運(yùn)算提高效率),否則將影響效率
        int bufferSize = 1024 * 256;

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

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

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

    }

}

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

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測(cè)試
 */
@RestController
public class DisruptorController {

    @Autowired
    private DisruptorService disruptorService;

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

9.測(cè)試

啟動(dòng)程序,在瀏覽器訪問請(qǐng)求連接進(jìn)行測(cè)試。

我們?cè)诳刂婆_(tái)上可以獲取到消息的發(fā)送和接收信息。

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

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

相關(guān)文章

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

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

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

    springBoot3 生成訂單號(hào)的示例代碼

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

    使用linux部署Spring Boot程序

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

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

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

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

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

    SpringBoot查看項(xiàng)目配置信息的幾種常見方法

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

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

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

    Spring Boot的filter(過濾器)簡(jiǎn)單使用實(shí)例詳解

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

    idea項(xiàng)目一直在build的問題分析及解決

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

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

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

最新評(píng)論

中江县| 赤城县| 齐河县| 桐乡市| 额济纳旗| 苍南县| 霍州市| 丰城市| 建湖县| 鄯善县| 清丰县| 获嘉县| 永修县| 龙泉市| 连城县| 和政县| 赣州市| 东乡县| 鲁山县| 井陉县| 阜新| 团风县| 吉水县| 沙雅县| 登封市| 延吉市| 伊吾县| 泰和县| 施甸县| 得荣县| 萨迦县| 包头市| 海伦市| 延寿县| 三门峡市| 廉江市| 南投市| 大同县| 海晏县| 衡南县| 上饶市|