在經過多線程來解決高併發的問題上,線程安全每每是最早須要考慮的問題,其次纔是性能。庫存超賣問題是有不少種技術解決方案的,好比悲觀鎖,分佈式鎖,樂觀鎖,隊列串行化,Redis原子操做等。本篇經過MySQL樂觀鎖來演示基本實現。java
基本的scheme已建好,演示就拿最簡單的數據結構最好不過了。mysql
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。web
下邊是pom.xml依賴配置。spring
<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>
因爲演示中MyBatis基於接口映射,配置簡單。application.yml中只須要配置mysql相關便可sql
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
package com.idcmind.ants.entity; public class Goods { private int id; private String name; private int stock; private int version; ... 此處省略getter、setter以及 toString方法 }
public class Order { private int id; private int uid; private int gid; ... 此處省略getter、setter以及 toString方法 }
GoodsDao.java數據庫
@Mapper public interface GoodsDao { /** * 查詢商品庫存 * @param id 商品id * @return */ @Select("SELECT * FROM goods WHERE id = #{id}") Goods getStock(@Param("id") int id); /** * 樂觀鎖方案扣減庫存 * @param id 商品id * @param version 版本號 * @return */ @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.javasegmentfault
這裏須要特別注意,因爲order
是sql中的關鍵字,因此表名須要加上反引號。安全
@Mapper public interface OrderDao { /** * 插入訂單 * 注意: order表是關鍵字,須要`order` * @param order */ @Insert("INSERT INTO `order` (uid, gid) VALUES (#{uid}, #{gid})") @Options(useGeneratedKeys = true, keyProperty = "id") int insertOrder(Order order); }
GoodsService.java數據結構
@Service public class GoodsService { @Autowired private GoodsDao goodsDao; @Autowired private OrderDao orderDao; /** * 扣減庫存 * @param gid 商品id * @param uid 用戶id * @return SUCCESS 1 FAILURE 0 */ @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; } }
這裏咱們寫個單元測試進行併發測試。mybatis
@SpringBootTest class GoodsServiceTest { @Autowired GoodsService goodsService; @Test void seckill() throws InterruptedException { // 庫存初始化爲10,這裏經過CountDownLatch和線程池模擬100個併發 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進行併發測試。因爲不是此處的重點,再也不作演示,感興趣的小夥伴能夠留言,我會整理下相關的教程。
這篇文章經過數據庫樂觀鎖已經解決了庫存超賣的問題,不過效率上並非最優方案,後續會完善其餘方案的演示。文中若有錯漏之處,還望你們不吝賜教。
公衆號【當我趕上你】