前言 
在通过多线程来解决高并发的问题上,线程安全往往是最先需要考虑的问题,其次才是性能。库存超卖问题是有很多种技术解决方案的,比如悲观锁,分布式锁,乐观锁,队列串行化,Redis原子操作等。本篇通过MySQL乐观锁来演示基本实现。
  开发前准备 
  1. 环境参数 
开发工具:IDEA 
基础工具:Maven+JDK8 
所用技术:SpringBoot+Mybatis 
数据库:MySQL5.7 
SpringBoot版本:2.2.5.RELEASE 
 
  2. 创建数据库 
基本的scheme已建好,演示就拿最简单的数据结构最好不过了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 DROP TABLE IF EXISTS `goods`; CREATE TABLE `goods` (   `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '商品id',   `name` varchar(30) DEFAULT NULL COMMENT '商品名称',   `stock` int(11) DEFAULT '0' COMMENT '商品库存',   `version` int(11) DEFAULT '0' COMMENT '并发版本控制',   PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT '商品表'; INSERT INTO `goods` VALUES (1, 'iphone', 10, 0); INSERT INTO `goods` VALUES (2, 'huawei', 10, 0); DROP TABLE IF EXISTS `order`; CREATE TABLE `order` (   `id` int(11) AUTO_INCREMENT,   `uid` int(11) COMMENT '用户id',   `gid` int(11) COMMENT '商品id',   PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT '订单表'; 
 
没有环境的小伙伴可以通过Docker实战之MySQL主从复制 ,快速的进行MySQL环境的搭建。创建数据库test,然后导入相关的sql初始化Table。
  3. 配置 pom 文件中的相关依赖 
下边是pom.xml依赖配置。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 <dependency >     <groupId > org.springframework.boot</groupId >      <artifactId > spring-boot-starter-web</artifactId >  </dependency > <dependency >     <groupId > org.mybatis.spring.boot</groupId >      <artifactId > mybatis-spring-boot-starter</artifactId >      <version > 2.1.1</version >  </dependency > <dependency >     <groupId > org.springframework.boot</groupId >      <artifactId > spring-boot-devtools</artifactId >      <scope > runtime</scope >      <optional > true</optional >  </dependency > <dependency >     <groupId > mysql</groupId >      <artifactId > mysql-connector-java</artifactId >      <scope > runtime</scope >  </dependency > <dependency >     <groupId > org.projectlombok</groupId >      <artifactId > lombok</artifactId >      <optional > true</optional >  </dependency > <dependency >     <groupId > org.springframework.boot</groupId >      <artifactId > spring-boot-starter-test</artifactId >      <scope > test</scope >  </dependency > 
 
  4. 配置 application.yml 
由于演示中MyBatis基于接口映射,配置简单。application.yml中只需要配置mysql相关即可
1 2 3 4 5 6 7 spring:   datasource:      type:  com.zaxxer.hikari.HikariDataSource      driverClassName:  com.mysql.cj.jdbc.Driver      url:  jdbc:mysql://localhost:3307/test?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC      username:  root      password:  root  
 
  5. 创建相关Bean 
1 2 3 4 5 6 7 8 9 10 11 package  com.idcmind.ants.entity;public  class  Goods   {    private  int  id;     private  String name;     private  int  stock;     private  int  version;     ...     此处省略getter、setter以及 toString方法 } 
 
1 2 3 4 5 6 7 8 public  class  Order   {    private  int  id;     private  int  uid;     private  int  gid;     ...     此处省略getter、setter以及 toString方法 } 
 
  乐观锁解决库存超卖方案 
  1. Dao层开发 
GoodsDao.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 @Mapper public  interface  GoodsDao   {         @Select("SELECT * FROM goods WHERE id = #{id}")      Goods getStock (@Param("id")  int  id)  ;          @Update("UPDATE goods SET stock = stock - 1, version = version + 1 WHERE id = #{id} AND stock > 0 AND version = #{version}")      int  decreaseStockForVersion (@Param("id")  int  id, @Param("version")  int  version)  ; } 
 
OrderDao.java
这里需要特别注意,由于order是sql中的关键字,所以表名需要加上反引号。
1 2 3 4 5 6 7 8 9 10 11 12 @Mapper public  interface  OrderDao   {              @Insert("INSERT INTO `order` (uid, gid) VALUES (#{uid}, #{gid})")      @Options(useGeneratedKeys = true, keyProperty = "id")      int  insertOrder (Order order)  ; } 
 
  2. Service层开发 
GoodsService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 @Service public  class  GoodsService   {    @Autowired      private  GoodsDao goodsDao;     @Autowired      private  OrderDao orderDao;          @Transactional      public  int  sellGoods (int  gid, int  uid)   {                  Goods goods = goodsDao.getStock(gid);         if  (goods.getStock() > 0 ) {                          int  update = goodsDao.decreaseStockForVersion(gid, goods.getVersion());                          if  (update == 0 ) {                 return  0 ;             }                          Order order = new  Order();             order.setUid(uid);             order.setGid(gid);             int  result = orderDao.insertOrder(order);             return  result;         }                  return  0 ;     } } 
 
  并发测试 
这里我们写个单元测试进行并发测试。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 @SpringBootTest class  GoodsServiceTest   {    @Autowired      GoodsService goodsService;     @Test      void  seckill ()  throws  InterruptedException  {                  int  threadTotal = 100 ;         ExecutorService executorService = Executors.newCachedThreadPool();         final  CountDownLatch countDownLatch = new  CountDownLatch(threadTotal);         for  (int  i = 0 ; i < threadTotal ; i++) {             int  uid = i;             executorService.execute(() -> {                 try  {                     goodsService.sellGoods(1 , uid);                 } catch  (Exception e) {                     e.printStackTrace();                 }                 countDownLatch.countDown();             });         }                  countDownLatch.await();         executorService.shutdown();     } } 
 
查看数据库验证是否超卖
上图的结果与我们的预期一致。此外还可以通过Postman或者Jmeter进行并发测试。由于不是此处的重点,不再做演示,感兴趣的小伙伴可以留言,我会整理下相关的教程。
  后续 
这篇文章通过数据库乐观锁已经解决了库存超卖的问题,不过效率上并不是最优方案,后续会完善其他方案的演示。文中如有错漏之处,还望大家不吝赐教。