Search in sources :

Example 6 with SeckillMockRequestDTO

use of com.goodskill.api.dto.SeckillMockRequestDTO in project goodsKill by techa03.

the class SeckillMockController method doWithSychronized.

/**
 * 通过同步锁控制秒杀并发(秒杀未完成阻塞主线程)
 * 场景一:初始化当前库存为1000,通过线程池调度,模拟总共有2000人参与秒杀,期望值为最后成功笔数为1000
 * 结果:多次运行,最终的结果为1000
 * 总结:加上同步锁可以解决秒杀问题,适用于单机模式,扩展性差。
 */
@ApiOperation(value = "秒杀场景一(sychronized同步锁实现)")
@PostMapping("/sychronized")
public Result doWithSychronized(@RequestBody @Valid SeckillWebMockRequestDTO dto) {
    long seckillId = dto.getSeckillId();
    int seckillCount = dto.getSeckillCount();
    int requestCount = dto.getRequestCount();
    prepareSeckill(seckillId, seckillCount, SYCHRONIZED.getName());
    log.info(SYCHRONIZED.getName() + "开始时间:{},秒杀id:{}", new Date(), seckillId);
    seckillService.execute(new SeckillMockRequestDTO(seckillId, requestCount, null), SYCHRONIZED.getCode());
    return Result.ok();
// 待mq监听器处理完成打印日志,不在此处打印日志
}
Also used : SeckillMockRequestDTO(com.goodskill.api.dto.SeckillMockRequestDTO) Date(java.util.Date) ApiOperation(io.swagger.annotations.ApiOperation)

Example 7 with SeckillMockRequestDTO

use of com.goodskill.api.dto.SeckillMockRequestDTO in project goodsKill by techa03.

the class SeckillMockController method doWithRedissionLock.

/**
 * 通过同步锁控制秒杀并发(秒杀未完成阻塞主线程)
 * 场景一:初始化当前库存为1000,通过线程池调度,模拟总共有2000人参与秒杀,期望值为最后成功笔数为1000
 * 结果:多次运行,最终的结果为1000
 * 总结:加上同步锁可以解决秒杀问题,适用于分布式环境,但速度不如加同步锁。
 */
@ApiOperation(value = "秒杀场景二(redis分布式锁实现)", notes = "秒杀场景二(redis分布式锁实现)", httpMethod = "POST")
@PostMapping("/redisson")
public Result doWithRedissionLock(@RequestBody @Valid SeckillWebMockRequestDTO dto) {
    long seckillId = dto.getSeckillId();
    int seckillCount = dto.getSeckillCount();
    int requestCount = dto.getRequestCount();
    // 初始化库存数量
    prepareSeckill(seckillId, seckillCount, REDISSION_LOCK.getName());
    log.info(REDISSION_LOCK.getName() + "开始时间:{},秒杀id:{}", new Date(), seckillId);
    AtomicInteger atomicInteger = new AtomicInteger(0);
    for (int i = 0; i < requestCount; i++) {
        taskExecutor.execute(() -> seckillService.execute(new SeckillMockRequestDTO(seckillId, 1, String.valueOf(atomicInteger.addAndGet(1))), REDISSION_LOCK.getCode()));
    }
    return Result.ok();
}
Also used : SeckillMockRequestDTO(com.goodskill.api.dto.SeckillMockRequestDTO) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Date(java.util.Date) ApiOperation(io.swagger.annotations.ApiOperation)

Example 8 with SeckillMockRequestDTO

use of com.goodskill.api.dto.SeckillMockRequestDTO in project goodsKill by techa03.

the class SeckillMockController method limit.

@ApiOperation(value = "秒杀场景十(Sentinel限流+数据库原子性更新)")
@PostMapping("/limit")
public Result limit(@RequestBody @Valid SeckillWebMockRequestDTO dto) {
    long seckillId = dto.getSeckillId();
    int seckillCount = dto.getSeckillCount();
    int requestCount = dto.getRequestCount();
    // 初始化库存数量
    prepareSeckill(seckillId, seckillCount, SENTINEL_LIMIT.getName());
    log.info(SENTINEL_LIMIT.getName() + "开始时间:{},秒杀id:{}", new Date(), seckillId);
    AtomicInteger atomicInteger = new AtomicInteger(0);
    for (int i = 0; i < requestCount; i++) {
        taskExecutor.execute(() -> {
            seckillService.execute(new SeckillMockRequestDTO(seckillId, 1, String.valueOf(atomicInteger.addAndGet(1))), SENTINEL_LIMIT.getCode());
        });
    }
    return Result.ok();
// 待mq监听器处理完成打印日志,不在此处打印日志
}
Also used : SeckillMockRequestDTO(com.goodskill.api.dto.SeckillMockRequestDTO) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Date(java.util.Date) ApiOperation(io.swagger.annotations.ApiOperation)

Example 9 with SeckillMockRequestDTO

use of com.goodskill.api.dto.SeckillMockRequestDTO in project goodsKill by techa03.

the class SynchronizedLockStrategyTest method execute.

@Test
@Disabled
public void execute() {
    SeckillMockRequestDTO requestDto = new SeckillMockRequestDTO();
    long seckillId = 1L;
    requestDto.setSeckillId(seckillId);
    requestDto.setCount(2);
    requestDto.setPhoneNumber("1");
    Seckill sendTopicResult = new Seckill();
    sendTopicResult.setSeckillId(seckillId);
    sendTopicResult.setStatus(SeckillStatusConstant.END);
    Seckill seckill1 = new Seckill();
    seckill1.setNumber(0);
    when(seckillMapper.selectById(seckillId)).thenReturn(seckill1);
    synchronizedLockStrategy.execute(requestDto);
    verify(seckillMapper, times(2)).updateById(sendTopicResult);
}
Also used : SeckillMockRequestDTO(com.goodskill.api.dto.SeckillMockRequestDTO) Seckill(com.goodskill.entity.Seckill) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Aggregations

SeckillMockRequestDTO (com.goodskill.api.dto.SeckillMockRequestDTO)9 ApiOperation (io.swagger.annotations.ApiOperation)7 Date (java.util.Date)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Seckill (com.goodskill.entity.Seckill)3 Test (org.junit.jupiter.api.Test)2 Disabled (org.junit.jupiter.api.Disabled)1