AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當前位置:首頁 > 科技  > 軟件

SpringBoot + Disruptor 實現特快高并發處理,贊!

來源: 責編: 時間:2023-10-23 17:03:45 242觀看
導讀1.背景工作中遇到項目使用Disruptor做消息隊列,對你沒看錯,不是Kafka也不是rabbitmq。Disruptor有個最大的優點就是快,還有一點它是開源的哦,下面做個簡單的記錄。2.Disruptor介紹Disruptor 是英國外匯交易公司LMAX開發的

1.背景

工作中遇到項目使用Disruptor做消息隊列,對你沒看錯,不是Kafka也不是rabbitmq。Disruptor有個最大的優點就是快,還有一點它是開源的哦,下面做個簡單的記錄。tdk28資訊網——每日最新資訊28at.com

tdk28資訊網——每日最新資訊28at.com

2.Disruptor介紹

Disruptor 是英國外匯交易公司LMAX開發的一個高性能隊列,研發的初衷是解決內存隊列的延遲問題(在性能測試中發現竟然與I/O操作處于同樣的數量級)。tdk28資訊網——每日最新資訊28at.com

基于 Disruptor 開發的系統單線程能支撐每秒 600 萬訂單,2010 年在 QCon 演講后,獲得了業界關注。tdk28資訊網——每日最新資訊28at.com

tdk28資訊網——每日最新資訊28at.com

Disruptor是一個開源的Java框架,它被設計用于在生產者—消費者(producer-consumer problem,簡稱PCP)問題上獲得盡量高的吞吐量(TPS)和盡量低的延遲。tdk28資訊網——每日最新資訊28at.com

從功能上來看,Disruptor 是實現了“隊列”的功能,而且是一個有界隊列。那么它的應用場景自然就是“生產者-消費者”模型的應用場合了。tdk28資訊網——每日最新資訊28at.com

Disruptor是LMAX在線交易平臺的關鍵組成部分,LMAX平臺使用該框架對訂單處理速度能達到600萬TPS,除金融領域之外,其他一般的應用中都可以用到Disruptor,它可以帶來顯著的性能提升。tdk28資訊網——每日最新資訊28at.com

其實Disruptor與其說是一個框架,不如說是一種設計思路,這個設計思路對于存在“并發、緩沖區、生產者—消費者模型、事務處理”這些元素的程序來說,Disruptor提出了一種大幅提升性能(TPS)的方案。tdk28資訊網——每日最新資訊28at.com

Disruptor的github主頁:https://github.com/LMAX-Exchange/disruptortdk28資訊網——每日最新資訊28at.com

3.Disruptor 的核心概念

先從了解 Disruptor 的核心概念開始,來了解它是如何運作的。下面介紹的概念模型,既是領域對象,也是映射到代碼實現上的核心對象。tdk28資訊網——每日最新資訊28at.com

(1) Ring Buffertdk28資訊網——每日最新資訊28at.com

如其名,環形的緩沖區。曾經 RingBuffer 是 Disruptor 中的最主要的對象,但從3.0版本開始,其職責被簡化為僅僅負責對通過 Disruptor 進行交換的數據(事件)進行存儲和更新。在一些更高級的應用場景中,Ring Buffer 可以由用戶的自定義實現來完全替代。tdk28資訊網——每日最新資訊28at.com

(2) Sequence Disruptortdk28資訊網——每日最新資訊28at.com

通過順序遞增的序號來編號管理通過其進行交換的數據(事件),對數據(事件)的處理過程總是沿著序號逐個遞增處理。一個 Sequence 用于跟蹤標識某個特定的事件處理者( RingBuffer/Consumer )的處理進度。tdk28資訊網——每日最新資訊28at.com

雖然一個 AtomicLong 也可以用于標識進度,但定義 Sequence 來負責該問題還有另一個目的,那就是防止不同的 Sequence 之間的CPU緩存偽共享(Flase Sharing)問題。tdk28資訊網——每日最新資訊28at.com

注:這是 Disruptor 實現高性能的關鍵點之一,網上關于偽共享問題的介紹已經汗牛充棟,在此不再贅述。tdk28資訊網——每日最新資訊28at.com

tdk28資訊網——每日最新資訊28at.com

(3) Sequencertdk28資訊網——每日最新資訊28at.com

Sequencer 是 Disruptor 的真正核心。此接口有兩個實現類 SingleProducerSequencer、MultiProducerSequencer ,它們定義在生產者和消費者之間快速、正確地傳遞數據的并發算法。tdk28資訊網——每日最新資訊28at.com

(4) Sequence Barriertdk28資訊網——每日最新資訊28at.com

用于保持對RingBuffer的 main published Sequence 和Consumer依賴的其它Consumer的 Sequence 的引用。 Sequence Barrier 還定義了決定 Consumer 是否還有可處理的事件的邏輯。tdk28資訊網——每日最新資訊28at.com

(5) Wait Strategytdk28資訊網——每日最新資訊28at.com

定義 Consumer 如何進行等待下一個事件的策略。 (注:Disruptor 定義了多種不同的策略,針對不同的場景,提供了不一樣的性能表現)tdk28資訊網——每日最新資訊28at.com

(6) Eventtdk28資訊網——每日最新資訊28at.com

在 Disruptor 的語義中,生產者和消費者之間進行交換的數據被稱為事件(Event)。它不是一個被 Disruptor 定義的特定類型,而是由 Disruptor 的使用者定義并指定。tdk28資訊網——每日最新資訊28at.com

(7) EventProcessortdk28資訊網——每日最新資訊28at.com

EventProcessor 持有特定消費者(Consumer)的 Sequence,并提供用于調用事件處理實現的事件循環(Event Loop)。tdk28資訊網——每日最新資訊28at.com

(8) EventHandlertdk28資訊網——每日最新資訊28at.com

Disruptor 定義的事件處理接口,由用戶實現,用于處理事件,是 Consumer 的真正實現。tdk28資訊網——每日最新資訊28at.com

(9) Producertdk28資訊網——每日最新資訊28at.com

即生產者,只是泛指調用 Disruptor 發布事件的用戶代碼,Disruptor 沒有定義特定接口或類型。tdk28資訊網——每日最新資訊28at.com

tdk28資訊網——每日最新資訊28at.com

4.案例-demo

通過下面8個步驟,你就能將Disruptor Get回家啦:tdk28資訊網——每日最新資訊28at.com

(1) 添加pom.xml依賴tdk28資訊網——每日最新資訊28at.com

<dependency>    <groupId>com.lmax</groupId>    <artifactId>disruptor</artifactId>    <version>3.4.4</version></dependency>

(2) 消息體Modeltdk28資訊網——每日最新資訊28at.com

/** * 消息體 */@Datapublic class MessageModel {    private String message;}

(3) 構造EventFactorytdk28資訊網——每日最新資訊28at.com

public class HelloEventFactory implements EventFactory<MessageModel> {    @Override    public MessageModel newInstance() {        return new MessageModel();    }}

(4) 構造EventHandler-消費者tdk28資訊網——每日最新資訊28at.com

@Slf4jpublic class HelloEventHandler implements EventHandler<MessageModel> {    @Override    public void onEvent(MessageModel event, long sequence, boolean endOfBatch) {        try {            //這里停止1000ms是為了確定消費消息是異步的            Thread.sleep(1000);            log.info("消費者處理消息開始");            if (event != null) {                log.info("消費者消費的信息是:{}",event);            }        } catch (Exception e) {            log.info("消費者處理消息失敗");        }        log.info("消費者處理消息結束");    }}

(5) 構造BeanManagertdk28資訊網——每日最新資訊28at.com

/** * 獲取實例化對象 */@Componentpublic class BeanManager implements ApplicationContextAware {    private static ApplicationContext applicationContext = null;    @Override    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {        this.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) 構造MQManagertdk28資訊網——每日最新資訊28at.com

@Configurationpublic class MQManager {    @Bean("messageModel")    public RingBuffer<MessageModel> messageModelRingBuffer() {        //定義用于事件處理的線程池, Disruptor通過java.util.concurrent.ExecutorSerivce提供的線程來觸發consumer的事件處理        ExecutorService executor = Executors.newFixedThreadPool(2);        //指定事件工廠        HelloEventFactory factory = new HelloEventFactory();        //指定ringbuffer字節大小,必須為2的N次方(能將求模運算轉為位運算提高效率),否則將影響效率        int bufferSize = 1024 * 256;        //單線程模式,獲取額外的性能        Disruptor<MessageModel> disruptor = new Disruptor<>(factory, bufferSize, executor,                ProducerType.SINGLE, new BlockingWaitStrategy());        //設置事件業務處理器---消費者        disruptor.handleEventsWith(new HelloEventHandler());        // 啟動disruptor線程        disruptor.start();        //獲取ringbuffer環,用于接取生產者生產的事件        RingBuffer<MessageModel> ringBuffer = disruptor.getRingBuffer();        return ringBuffer;    }}

(7) 構造Mqservice和實現類-生產者tdk28資訊網——每日最新資訊28at.com

public interface DisruptorMqService {    /**     * 消息     * @param message     */    void sayHelloMq(String message);}@Slf4j@Component@Servicepublic class DisruptorMqServiceImpl implements DisruptorMqService {    @Autowired    private RingBuffer<MessageModel> messageModelRingBuffer;    @Override    public void sayHelloMq(String message) {        log.info("record the message: {}",message);        //獲取下一個Event槽的下標        long sequence = messageModelRingBuffer.next();        try {            //給Event填充數據            MessageModel event = messageModelRingBuffer.get(sequence);            event.setMessage(message);            log.info("往消息隊列中添加消息:{}", event);        } catch (Exception e) {            log.error("failed to add event to messageModelRingBuffer for : e = {},{}",e,e.getMessage());        } finally {            //發布Event,激活觀察者去消費,將sequence傳遞給改消費者            //注意最后的publish方法必須放在finally中以確保必須得到調用;如果某個請求的sequence未被提交將會堵塞后續的發布操作或者其他的producer            messageModelRingBuffer.publish(sequence);        }    }}

(8) 構造測試類及方法tdk28資訊網——每日最新資訊28at.com

@Slf4j@RunWith(SpringRunner.class)@SpringBootTest(classes = DemoApplication.class)public class DemoApplicationTests {    @Autowired    private DisruptorMqService disruptorMqService;    /**     * 項目內部使用Disruptor做消息隊列     * @throws Exception     */    @Test    public void sayHelloMqTest() throws Exception{        disruptorMqService.sayHelloMq("消息到了,Hello world!");        log.info("消息隊列已發送完畢");        //這里停止2000ms是為了確定是處理消息是異步的        Thread.sleep(2000);    }}

測試運行結果:tdk28資訊網——每日最新資訊28at.com

2020-04-05 14:31:18.543  INFO 7274 --- [           main] c.e.u.d.d.s.Impl.DisruptorMqServiceImpl  : record the message: 消息到了,Hello world!2020-04-05 14:31:18.545  INFO 7274 --- [           main] c.e.u.d.d.s.Impl.DisruptorMqServiceImpl  : 往消息隊列中添加消息:MessageModel(message=消息到了,Hello world!)2020-04-05 14:31:18.545  INFO 7274 --- [           main] c.e.utils.demo.DemoApplicationTests      : 消息隊列已發送完畢2020-04-05 14:31:19.547  INFO 7274 --- [pool-1-thread-1] c.e.u.d.disrupMq.mq.HelloEventHandler    : 消費者處理消息開始2020-04-05 14:31:19.547  INFO 7274 --- [pool-1-thread-1] c.e.u.d.disrupMq.mq.HelloEventHandler    : 消費者消費的信息是:MessageModel(message=消息到了,Hello world!)2020-04-05 14:31:19.547  INFO 7274 --- [pool-1-thread-1] c.e.u.d.disrupMq.mq.HelloEventHandler    : 消費者處理消息結束

5.總結

其實 生成者 -> 消費者 模式是很常見的,通過一些消息隊列也可以輕松做到上述的效果。不同的地方在于,Disruptor 是在內存中以隊列的方式去實現的,而且是無鎖的。這也是 Disruptor 為什么高效的原因。tdk28資訊網——每日最新資訊28at.com

本文鏈接:http://www.tebozhan.com/showinfo-26-14572-0.htmlSpringBoot + Disruptor 實現特快高并發處理,贊!

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 深入探討 Golang 中的追加操作

下一篇: Golang數組:全面指南與實際示例

標簽:
  • 熱門焦點
Top