15 changed files with 215 additions and 9 deletions
@ -0,0 +1,31 @@
|
||||
package net.sopod.soim.entry.config; |
||||
|
||||
import lombok.Data; |
||||
import org.springframework.boot.context.properties.ConfigurationProperties; |
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
/** |
||||
* EntryServerConfig |
||||
* |
||||
* @author tmy |
||||
* @date 2022-04-11 16:08 |
||||
*/ |
||||
@Component |
||||
@ConfigurationProperties(prefix = "entry") |
||||
@EnableConfigurationProperties |
||||
@Data |
||||
public class EntryServerConfig { |
||||
|
||||
/** 消息消费者线程数 */ |
||||
private Integer workerSize; |
||||
|
||||
public Integer getWorkerSize() { |
||||
if (workerSize == null || workerSize < 1) { |
||||
workerSize = Runtime.getRuntime().availableProcessors(); |
||||
workerSize = Math.max(workerSize, 4); |
||||
} |
||||
return workerSize; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,78 @@
|
||||
package net.sopod.soim.entry.worker; |
||||
|
||||
import com.lmax.disruptor.*; |
||||
import com.lmax.disruptor.dsl.Disruptor; |
||||
import com.lmax.disruptor.dsl.ProducerType; |
||||
import io.netty.util.concurrent.DefaultThreadFactory; |
||||
import net.sopod.soim.common.util.ImClock; |
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger; |
||||
|
||||
/** |
||||
* Worker |
||||
* |
||||
* @author tmy |
||||
* @date 2022-04-11 17:05 |
||||
*/ |
||||
public class DisruptorTest { |
||||
public static class LongEvent { |
||||
int id; |
||||
private Long value; |
||||
public Long getValue() { |
||||
return value; |
||||
} |
||||
public void setValue(Long value) { |
||||
this.value = value; |
||||
} |
||||
} |
||||
public static class LongEventFactory implements EventFactory<LongEvent> { |
||||
private static final AtomicInteger counter = new AtomicInteger(1); |
||||
@Override |
||||
public LongEvent newInstance() { |
||||
int count = counter.getAndIncrement(); |
||||
System.out.println("instance:"+ count); |
||||
LongEvent event = new LongEvent(); |
||||
event.id = count; |
||||
return event; |
||||
} |
||||
} |
||||
public static class LongEventHandler implements EventHandler<LongEvent> { |
||||
@Override |
||||
public void onEvent(LongEvent event, long sequence, boolean endOfBatch) throws Exception { |
||||
System.out.println("消费者:"+event.id + "," + sequence + "," + event.getValue() + "," + Thread.currentThread().getName()); |
||||
Thread.sleep(20); |
||||
// event值置空,回收内存
|
||||
event.setValue(null); |
||||
} |
||||
} |
||||
public static void main(String[] args) throws InterruptedException, InsufficientCapacityException { |
||||
LongEventFactory eventFactory = new LongEventFactory(); |
||||
int ringBufferSize = 128;//64 * 1024;
|
||||
Disruptor<LongEvent> disruptor = new Disruptor<LongEvent>( |
||||
eventFactory, |
||||
ringBufferSize, |
||||
new DefaultThreadFactory("disruptor"), |
||||
ProducerType.SINGLE, |
||||
new BlockingWaitStrategy() |
||||
); |
||||
disruptor.handleEventsWith(new LongEventHandler()); |
||||
disruptor.start(); |
||||
|
||||
// 7.创建RingBuffer容器
|
||||
RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer(); |
||||
for (long i = 0; i < 1000; i++) { |
||||
long start = ImClock.millis(); |
||||
// 没有空闲位置时会阻塞
|
||||
long sequence = ringBuffer.tryNext(); |
||||
//long sequence = ringBuffer.next();
|
||||
LongEvent longEvent = ringBuffer.get(sequence); |
||||
longEvent.setValue(i); |
||||
Thread.sleep(10); |
||||
ringBuffer.publish(sequence); |
||||
System.out.println("生产者:" + i + "," + (ImClock.millis() - start) + "ms"); |
||||
} |
||||
|
||||
Thread.sleep(5000); |
||||
disruptor.shutdown(); |
||||
} |
||||
} |
||||
@ -1,10 +1,26 @@
|
||||
package net.sopod.soim.logic.user.service; |
||||
|
||||
import org.apache.dubbo.config.annotation.DubboService; |
||||
|
||||
import java.util.concurrent.CompletableFuture; |
||||
|
||||
/** |
||||
* UserServiceImpl |
||||
* |
||||
* @author tmy |
||||
* @date 2022-04-04 10:03 |
||||
*/ |
||||
public class UserServiceImpl { |
||||
@DubboService |
||||
public class UserServiceImpl implements UserService { |
||||
|
||||
@Override |
||||
public CompletableFuture<String> sayHi(String name) { |
||||
return CompletableFuture.completedFuture("hi," + name + "!"); |
||||
} |
||||
|
||||
@Override |
||||
public String sayHello(String name) { |
||||
return "hello," + name + "!"; |
||||
} |
||||
|
||||
} |
||||
|
||||
@ -0,0 +1,11 @@
|
||||
spring: |
||||
application: |
||||
name: im-logic-user |
||||
|
||||
dubbo: |
||||
application: |
||||
name: ${spring.application.name} |
||||
registry: |
||||
address: nacos://124.222.131.236:3848 |
||||
protocol: |
||||
port: 3002 |
||||
Loading…
Reference in new issue