参考:Redis实现分布式锁的7种方案 - 知乎
1、 准备数据库表,如下SQL表示库存表,有主键ID和库存数量字段
CREATE TABLE `t_stock` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`quantity` bigint(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
初始数据id quantity
1111 9
2、pom.xml文件
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.6.4
com.hmblogs
hmblogs
0.0.1-SNAPSHOT
hmblogs
hmblogs
8
1.2.8
1.16
com.alibaba
druid-spring-boot-starter
${druid.version}
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
com.baomidou
mybatis-plus-boot-starter
3.5.3.1
org.springframework.boot
spring-boot-starter-web
org.projectlombok
lombok
true
mysql
mysql-connector-java
runtime
org.projectlombok
lombok
true
org.bgee.log4jdbc-log4j2
log4jdbc-log4j2-jdbc4.1
${log4jdbc.version}
com.alibaba
fastjson
1.2.9
redis.clients
jedis
org.springframework.boot
spring-boot-maven-plugin
3、应用配置文件
server:
port: 8081
servlet.context-path: /
#配置数据源
spring:
datasource:
druid:
db-type: com.alibaba.druid.pool.DruidDataSource
driverClassName: net.sf.log4jdbc.sql.jdbcapi.DriverSpy
url: jdbc:log4jdbc:mysql://${DB_HOST:localhost}:${DB_PORT:3306}/${DB_NAME:eladmin}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false
username: ${DB_USER:root}
password: ${DB_PWD:123456}
4、StockMapper.xml如下
id, quantity
update t_stock set quantity=quantity-1 where id=#{id}
5、BackendApplication
package com.hmblogs.backend;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class BackendApplication {
public static void main(String[] args) {
SpringApplication.run(BackendApplication.class, args);
}
}
6、Stock.java如下
package com.hmblogs.backend.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("t_stock")
public class Stock {
@TableId(value="id", type = IdType.AUTO)
private Integer id;
private Integer quantity;
}
7、StockMapper
package com.hmblogs.backend.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hmblogs.backend.entity.Stock;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface StockMapper extends BaseMapper {
List findAll();
Stock findById(Stock stock);
Integer updateStockById(Stock stock);
}
8、OrderController
package com.hmblogs.backend.controller;
import com.hmblogs.backend.dao.StockMapper;
import com.hmblogs.backend.entity.Stock;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
@RestController
@Slf4j
public class OrderController {
@Autowired
private StockMapper stockMapper;
//总库存
private long nKuCuen = 0;
//商品key名字
private String shangpingKey = "computer_key";
//获取锁的超时时间 秒
private int timeout = 30 * 1000;
@GetMapping("/qiangdan")
public List qiangdan() {
//抢到商品的用户
List shopUsers = new ArrayList<>();
//构造很多用户
List users = new ArrayList<>();
IntStream.range(0, 10000).parallel().forEach(b -> {
users.add("神牛-" + b);
});
//初始化库存
nKuCuen = 10;
//模拟开抢
users.parallelStream().forEach(b -> {
String shopUser = qiang(b);
if (!StringUtils.isEmpty(shopUser)) {
shopUsers.add(shopUser);
}
});
return shopUsers;
}
/**
* 模拟抢单动作
*
* @param b
* @return
*/
private String qiang(String b) {
//用户开抢时间
long startTime = System.currentTimeMillis();
//未抢到的情况下,30秒内继续获取锁
while ((startTime + timeout) >= System.currentTimeMillis()) {
//商品是否剩余
if (nKuCuen <= 0) {
break;
}
Jedis jedisCom = new Jedis("localhost",6379);
jedisCom.auth("heming");
if (jedisCom.setnx(shangpingKey, b)==1) {
//用户b拿到锁
log.info("用户{}拿到锁...", b);
try {
//商品是否剩余
if (nKuCuen <= 0) {
break;
}
//模拟生成订单耗时操作,方便查看:神牛-50 多次获取锁记录
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
//抢购成功,商品递减,记录用户
nKuCuen -= 1;
int id = 1111;
Stock stock = new Stock();
stock.setId(id);
Stock stockDo = stockMapper.findById(stock);
Integer quantity = stockDo.getQuantity();
if(quantity!=null && quantity>0){
stockMapper.updateStockById(stock);
log.info("update success.");
}else{
log.info("no update.");
}
//抢单成功跳出
log.info("用户{}抢单成功跳出...所剩库存:{}", b, nKuCuen);
return b + "抢单成功,所剩库存:" + nKuCuen;
} finally {
log.info("用户{}释放锁...", b);
//释放锁
jedisCom.del(shangpingKey, b);
}
}
}
return "";
}
}
9、验证
浏览器访问http://localhost:8081/qiangdan
查看Idea的console内容,
但是,搜索"update success."内容,预期是9次,实际也是9次,符合我的需要,没有让库存变成负数,
查看数据库表的库存,id为1111的记录的quantity为0,不是1,也不是负数
10、继续第二种纬度的验证
StockController的代码如下:
package com.hmblogs.backend.controller;
import com.hmblogs.backend.dao.StockMapper;
import com.hmblogs.backend.entity.Stock;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import java.util.Date;
@RestController
@Slf4j
public class StockController {
@Autowired
private StockMapper stockMapper;
/**
* redis test
* @return
*/
@GetMapping(value = "/reduceStock")
public void redisTestLock(){
log.info("reduceStock");
int id = 1111;
String key = "reduceStock"+id;
String time = new Date().getTime()+"";
Jedis jedisCom = new Jedis("localhost",6379);
jedisCom.auth("heming");
if (jedisCom.setnx(key, time)==1) {
log.info("{}生成锁...", time);
try{
Stock stock = new Stock();
stock.setId(id);
Stock stockDo = stockMapper.findById(stock);
Integer quantity = stockDo.getQuantity();
if(quantity!=null && quantity>0){
stockMapper.updateStockById(stock);
log.info("update success.");
}else{
log.info("no update.");
}
} finally {
log.info("{}释放锁...", time);
//释放锁
jedisCom.del(key, time);
}
}
}
}
将库存改为10,验证,通过压测验证有没有保证锁应该有的作用,控制查库存和当不小于0的时候减少库存这2个逻辑的原子性,预期做到,实际做到了。利用APIPost工具做压测,如下图所示
查看server的控制台
搜索reduceStock,有100个结果
搜索"生成锁..."和"释放锁...",都有14次结果,说明jedisCom.setnx(key, time)==1成立的次数有14次。
搜索"update success.",有10次结果,说明减少库存减少了10次,
此时查看数据库表的库存,发现为0
搜索"no update.",发现有4个结果,说明有4次获得锁了,但是库存已经是0了,不能再减库存了,库存不能为负的。