七、Disruptor框架

前言

  • 参考并发编程网:https://ifeve.com/disruptor/

一、disruptor概述

  • 这个系统是建立在JVM平台上,其核心是一个业务逻辑处理器,它能够在一个线程里每秒处理6百万订单。
  • 业务逻辑处理器完全是运行在内存中,使`用事件源驱动方式。业务逻辑处理器的核心是Disruptor。
  • Disruptor它是一个开源的并发框架
  • Disruptor框架最主要的优点:高性能队列,无锁机制,底层使用CAS实现,基于事件驱动源

二、disruptor原理

  • RingBuffer:见名知意 -> 环形缓冲区
  • 假设RingBuffer的总大小为10,当我们需要取12的元素时
  • 通过计算12%10=2,可以快速定位到2的元素
  • 这个就是为什么disruptor可以实现高性能队列的原因
    在这里插入图片描述

三、disruptor实现生产消费

1、maven依赖

	<dependencies>
		<dependency>
			<groupId>com.lmax</groupId>
			<artifactId>disruptor</artifactId>
			<version>3.2.1</version>
		</dependency>
	</dependencies>

2、entity -> LongEvent.java

//声明一个event 生产者与消费者传递数据类型
public class LongEvent {

	private Long value;

	public Long getValue() {
		return value;
	}

	public void setValue(Long value) {
		this.value = value;
	}
}

3、factory -> LongEventFactory.java

import com.lmax.disruptor.EventFactory;

// EventFactory 实例化LongEvent
public class LongEventFactory implements EventFactory<LongEvent> {

	public LongEvent newInstance() {

		return new LongEvent();
	}
}

4.1、consumer -> LongEventHandler

import com.lmax.disruptor.EventHandler;

//消费者获取生产推送数据
public class LongEventHandler implements EventHandler<LongEvent> {

	public void onEvent(LongEvent event, long sequence, boolean endOfBatch) throws Exception {
		System.out.println("消费者1 获取生产者数据..event:" + event.getValue());
	}
}

4.2、consumer -> LongEventHandler2

import com.lmax.disruptor.EventHandler;

//消费者获取生产推送数据
public class LongEventHandler2 implements EventHandler<LongEvent> {

	public void onEvent(LongEvent event, long sequence, boolean endOfBatch) throws Exception {
		System.out.println("消费者2 获取生产者数据..event:" + event.getValue());
	}
}

5、producer -> LongEventProducer.java

import java.nio.ByteBuffer;
import com.lmax.disruptor.RingBuffer;

// 生产者
public class LongEventProducer {
	private RingBuffer<LongEvent> ringBuffer;

	public LongEventProducer(RingBuffer<LongEvent> ringBuffer) {
		this.ringBuffer = ringBuffer;
	}

	public void onData(ByteBuffer byteBuffer) {
		// 获取事件队列 下标位置
		long sequence = ringBuffer.next();
		try {
			// 取出空队列(Event)
			LongEvent longEvent = ringBuffer.get(sequence);
			// 给空队列赋值
			longEvent.setValue(byteBuffer.getLong(0));
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			System.out.println("生产者发送数据...");
			// 发送数据
			ringBuffer.publish(sequence);
		}
	}
}

6、Main.java

import java.nio.ByteBuffer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import com.lmax.disruptor.EventFactory;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.YieldingWaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;

public class Main {

	public static void main(String[] args) {
		// 1.创建可以缓存的线程池,提供发给consumer
		ExecutorService executor = Executors.newCachedThreadPool();
		// 2.创建 Event工厂
		EventFactory<LongEvent> eventFactory = new LongEventFactory();
		// 3.创建ringbuffer大小
		int ringbuffer = 1024 * 1024;// 2的N次方。
		// 4.创建Disruptor
		Disruptor<LongEvent> disruptor = new Disruptor<LongEvent>(eventFactory, ringbuffer, executor,
				ProducerType.MULTI, new YieldingWaitStrategy());
		// 5.连接消费者---注册消费者
		disruptor.handleEventsWith(new LongEventHandler());
		disruptor.handleEventsWith(new LongEventHandler2());
		// 多个消费者 一个生产者 默认重复消费、配置分组
		// 6.启动
		disruptor.start();
		// 7.创建RingBuffer容器
		RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();
		// 8.创建生产者
		LongEventProducer producer = new LongEventProducer(ringBuffer);
		// 9.指定缓冲区大小
		ByteBuffer byteBuffer = ByteBuffer.allocate(8);
		for (int i = 1; i < 100; i++) {
			byteBuffer.putLong(0, i);
			producer.onData(byteBuffer);
		}
		executor.shutdown();
		disruptor.shutdown();
	}
}

7、一个生产者,多个消费者测试

>

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无休止符

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值