前言
参考并发编程网: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
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;
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 {
LongEvent longEvent = ringBuffer. get ( sequence) ;
longEvent. setValue ( byteBuffer. getLong ( 0 ) ) ;
} catch ( Exception e) {
} 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) {
ExecutorService executor = Executors. newCachedThreadPool ( ) ;
EventFactory< LongEvent> eventFactory = new LongEventFactory ( ) ;
int ringbuffer = 1024 * 1024 ;
Disruptor< LongEvent> disruptor = new Disruptor < LongEvent> ( eventFactory, ringbuffer, executor,
ProducerType. MULTI, new YieldingWaitStrategy ( ) ) ;
disruptor. handleEventsWith ( new LongEventHandler ( ) ) ;
disruptor. handleEventsWith ( new LongEventHandler2 ( ) ) ;
disruptor. start ( ) ;
RingBuffer< LongEvent> ringBuffer = disruptor. getRingBuffer ( ) ;
LongEventProducer producer = new LongEventProducer ( ringBuffer) ;
ByteBuffer byteBuffer = ByteBuffer. allocate ( 8 ) ;
for ( int i = 1 ; i < 100 ; i++ ) {
byteBuffer. putLong ( 0 , i) ;
producer. onData ( byteBuffer) ;
}
executor. shutdown ( ) ;
disruptor. shutdown ( ) ;
}
}
7、一个生产者,多个消费者测试