地址
- 0. 整體流程
- 1. 傳統(tǒng)方式
- 2. 使用樂觀鎖
- 3. 使用緩存
- 4. 使用分布式限流
- 5. 使用隊(duì)列異步下單
1. 思路介紹
不做任何控制,按照流程進(jìn)行檢查庫存,扣庫存,下訂單,這種方式會(huì)存在并發(fā)問題
2. 代碼實(shí)現(xiàn)
在SeckillEvolutionController創(chuàng)建一個(gè)傳統(tǒng)方式下訂單入口的方法
- SeckillEvolutionController
/**
* 傳統(tǒng)方式下訂單
*
* @param id 商品ID
* @return com.example.common.ResponseBean
* @throws Exception
* @author wliduo[i@dolyw.com]
* @date 2019/11/21 19:50
*/
@PostMapping("/createWrongOrder/{id}")
public ResponseBean createWrongOrder(@PathVariable("id") Integer id) throws Exception {
Integer orderCount = seckillEvolutionService.createWrongOrder(id);
return new ResponseBean(HttpStatus.OK.value(), "購買成功", orderCount);
}
在Service添加方法
- ISeckillEvolutionService
/**
* 傳統(tǒng)方式的創(chuàng)建訂單(并發(fā)會(huì)出現(xiàn)錯(cuò)誤)
*
* @param id
* @return java.lang.Integer
* @throws Exception
* @author wliduo[i@dolyw.com]
* @date 2019/11/22 14:21
*/
Integer createWrongOrder(Integer id) throws Exception;
- ISeckillEvolutionService
/**
* 傳統(tǒng)方式(名稱注入)
*/
@Autowired
@Qualifier("seckillTraditionService")
private ISeckillService seckillTraditionService;
@Override
@Transactional(rollbackFor = Exception.class)
public Integer createWrongOrder(Integer id) throws Exception {
// 檢查庫存
StockDto stockDto = seckillTraditionService.checkStock(id);
// 扣庫存
Integer saleCount = seckillTraditionService.saleStock(stockDto);
if (saleCount <= 0) {
throw new CustomException("扣庫存失敗");
}
// 下訂單
Integer orderCount = seckillTraditionService.createOrder(stockDto);
if (saleCount <= 0) {
throw new CustomException("下訂單失敗");
}
return orderCount;
}
然后創(chuàng)建一個(gè)ISeckillService的傳統(tǒng)方式的實(shí)現(xiàn)類提供上面使用
- SeckillTraditionServiceImpl
package com.example.seckill.impl;
import ...
/**
* 傳統(tǒng)方式(并發(fā)會(huì)出現(xiàn)錯(cuò)誤)
*
* @author wliduo[i@dolyw.com]
* @date 2019-11-20 18:03:33
*/
@Service("seckillTraditionService")
public class SeckillTraditionServiceImpl implements ISeckillService {
@Autowired
private StockDao stockDao;
@Autowired
private StockOrderDao stockOrderDao;
@Override
public StockDto checkStock(Integer id) {
StockDto stockDto = stockDao.selectByPrimaryKey(id);
if (stockDto.getCount() > 0) {
return stockDto;
}
throw new CustomException("庫存不足");
}
@Override
public Integer saleStock(StockDto stockDto) {
stockDto.setCount(stockDto.getCount() - 1);
stockDto.setSale(stockDto.getSale() + 1);
return stockDao.updateByPrimaryKey(stockDto);
}
@Override
public Integer createOrder(StockDto stockDto) {
StockOrderDto stockOrderDto = new StockOrderDto();
stockOrderDto.setStockId(stockDto.getId());
return stockOrderDao.insertSelective(stockOrderDto);
}
}
3. 開始測試
使用JMeter測試上面的代碼,JMeter的使用可以查看: JMeter的安裝使用
我們調(diào)用一下商品庫存初始化的方法,我使用的是PostMan,初始化庫存表商品10個(gè)庫存,而且清空訂單表

這時(shí)候可以看到我們的數(shù)據(jù),庫存為10,賣出為0,訂單表為空

打開JMeter,添加測試計(jì)劃(測試計(jì)劃文件在項(xiàng)目的src\main\resources\jmx下),模擬500個(gè)并發(fā)線程測試秒殺10個(gè)庫存的商品,填寫請求地址,點(diǎn)擊啟動(dòng)圖標(biāo)開始


可以看到500個(gè)并發(fā)線程,搶購到了24個(gè)訂單,而我們的商品實(shí)際顯示為賣出10,庫存還有0

這就是賣超問題了,這是因?yàn)橥粫r(shí)間大量線程同時(shí)請求校驗(yàn)庫存,扣庫存,創(chuàng)建訂單,這三個(gè)操作不在同一個(gè)原子,比如,很多線程同時(shí)讀到庫存為10,這樣都穿過了校驗(yàn)庫存的判斷,所以出現(xiàn)賣超問題
在這種情況下就引入了鎖的概念,鎖區(qū)分為樂觀鎖和悲觀鎖,悲觀鎖都是犧牲性能保證數(shù)據(jù),所以在這種高并發(fā)場景下,一般都是使用樂觀鎖解決