Mysql、Redis、Redisson分布式锁
程序员文章站
2024-01-26 20:45:40
Mysql代码ProductEntity selectLock(@Param("title") String title);@RequestMapping("mysql/singleLock")...
Mysql
代码
ProductEntity selectLock(@Param("title") String title);
<select id="selectLock" resultType="com.csea.lock.pojo.ProductEntity">
select * from product where title = #{title} for update
</select>
@RequestMapping("mysql/singleLock")
@Transactional(rollbackFor = Exception.class)
public String mysqlSingleLock() throws Exception {
log.info("进入了方法");
ProductEntity selectLock = productMapper.selectLock("苹果");
if (StringUtils.isEmpty(selectLock)) {
throw new Exception("没有分布式锁");
}
log.info("拿到了锁");
Thread.sleep(5000);
return "完成了";
}
效果
启动8080,8081两个端口进行访问
5秒之后拿到了锁
Redis
原理
SET resource_name my_random_value NX PX 30000
resource_name:资源名称
my_random_value:随机值,每个线程随机值不同,用于释放锁时的校验
NX:key不存在时设置成功,反之不超过
PX:过期时间,出现异常情况,锁可以过期失效
利用NX的原子性,多个线程并发时候,只有一个线程可以设置成功;设置成功即可获得锁,继续执行后面的业务;出现异常,锁了有效期,锁就自动释放。
代码
private final RedisTemplate redisTemplate;
@GetMapping("/redis/lock")
public String redisLock() {
log.info("进入了方法");
String key = "Csea";
String value = UUID.randomUUID().toString();
RedisCallback<Boolean> redisCallback = redisConnection -> {
// 设置NX
RedisStringCommands.SetOption setOption = RedisStringCommands.SetOption.ifAbsent();
// 设置过期时间
Expiration expiration = Expiration.seconds(30);
// 序列化Key
byte[] redisKey = redisTemplate.getKeySerializer().serialize(key);
// 序列化Value
byte[] redisValue = redisTemplate.getValueSerializer().serialize(value);
// 返回 setnx操作结果
return redisConnection.set(redisKey, redisValue, expiration, setOption);
};
// 获取分布式锁
Boolean lock = (Boolean) redisTemplate.execute(redisCallback);
if (lock) {
log.info("拿到了锁");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
// 释放锁
// LUA脚本
String script = "if redis.call(\"get\", KEYS[1]) == ARGV[1] then\n" +
" return redis.call(\"del\",KEYS[1])\n" +
"else\n" +
" return 0\n" +
"end";
RedisScript<Boolean> redisScript = RedisScript.of(script, Boolean.class);
List<String> keyList = Arrays.asList(key);
Boolean result = (Boolean) redisTemplate.execute(redisScript, keyList, value);
log.info("释放锁结果:{}", result);
}
}
log.info("执行结束");
return "执行结束";
}
效果
启动8080,8081两个端口进行访问
8080没有拿到锁,就直接运行结束了。
分布式下的定时任务
redis锁的代码抽离
@Slf4j
public class RedisLock implements AutoCloseable {
private RedisTemplate redisTemplate;
private String key;
private String value;
private int expireTime;
public RedisLock(RedisTemplate redisTemplate, String key, int expireTime) {
this.redisTemplate = redisTemplate;
this.key = key;
this.value = UUID.randomUUID().toString();
this.expireTime = expireTime;
}
public boolean getLock() {
RedisCallback<Boolean> redisCallback = redisConnection -> {
// 设置NX
RedisStringCommands.SetOption setOption = RedisStringCommands.SetOption.ifAbsent();
// 设置过期时间
Expiration expiration = Expiration.seconds(expireTime);
// 序列化Key
byte[] redisKey = redisTemplate.getKeySerializer().serialize(key);
// 序列化Value
byte[] redisValue = redisTemplate.getValueSerializer().serialize(value);
// 返回 setnx操作结果
return redisConnection.set(redisKey, redisValue, expiration, setOption);
};
// 获取分布式锁
Boolean lock = (Boolean) redisTemplate.execute(redisCallback);
return lock;
}
public boolean unlock() {
// LUA脚本
String script = "if redis.call(\"get\", KEYS[1]) == ARGV[1] then\n" +
" return redis.call(\"del\",KEYS[1])\n" +
"else\n" +
" return 0\n" +
"end";
RedisScript<Boolean> redisScript = RedisScript.of(script, Boolean.class);
List<String> keyList = Arrays.asList(key);
Boolean result = (Boolean) redisTemplate.execute(redisScript, keyList, value);
log.info("释放锁的结果:{}", result);
return result;
}
@Override
public void close() throws Exception {
unlock();
}
}
定时任务代码
@Service
@Slf4j
@RequiredArgsConstructor
public class SchedulerService {
private final RedisTemplate redisTemplate;
@Scheduled(cron = "0/5 * * * * ?")
public void logInfo() {
try (RedisLock lock = new RedisLock(redisTemplate, "Csea", 30)) {
if (lock.getLock()) {
log.info("打印了*************Csea*************");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
效果
启动8080,8081两个端口
Redisson
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>3.13.1</version>
</dependency>
代码
private final RedissonClient redissonClient;
@GetMapping("/redisson/lock")
public String redissonLock() {
RLock lock = redissonClient.getLock("Csea");
log.info("进入了方法");
try {
lock.lock(30, TimeUnit.SECONDS);
log.info("拿到了锁");
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
log.info("执行结束");
return "执行结束";
}
效果
启动8080,8081两个端口
JMeter压测
压测代码
@Service
@Slf4j
public class JmService {
static Map<String, Integer> productMap = new HashMap<>(10);
static Map<String, Integer> stockMap = new HashMap<>(10);
static Map<String, String> orderMap = new HashMap<>();
static {
productMap.put("123456", 100000);
stockMap.put("123456", 100000);
}
private String queryMap(String projectId) {
return "商品限量:" + productMap.get(projectId) +
"还剩下:" + stockMap.get(projectId) + " 份" +
"下单用户数数量:" + orderMap.size();
}
public String querySecKillProductInfo(String projectId) {
return this.queryMap(projectId);
}
@Autowired
private RedissonClient redissonClient;
public void secKillProject(String productId) {
RLock lock = redissonClient.getLock("myLock");
log.info("锁状态:{}", lock.isLocked());
try {
if (lock.isLocked()) {
throw new RuntimeException("当前请求过多");
}
// 加锁
lock.lock();
// 如果库存为0 活动结束
int stockNum = stockMap.get(productId);
if (stockNum == 0) {
throw new RuntimeException("商品已售空,活动结束");
} else {
// 模拟订单
orderMap.put(String.valueOf(System.currentTimeMillis()), productId);
// 扣库存
stockNum -= 1;
stockMap.put(productId, stockNum);
}
} catch (RuntimeException e) {
e.printStackTrace();
} finally {
// 解锁
lock.unlock();
}
}
}
private final JmService jmService;
/**
* 利用Redisson进行秒杀压测 Demo
*
* @param productId
* @return
*/
@GetMapping("/redisson/query/{productId}")
public String redissonQuery(@PathVariable String productId) {
return jmService.querySecKillProductInfo(productId);
}
@GetMapping("/redisson/order/{productId}")
public String redissonSkill(@PathVariable String productId) {
jmService.secKillProject(productId);
return jmService.querySecKillProductInfo(productId);
}
配置
5000个线程同时访问
效果
对比
方式 | 优点 | 缺点 |
---|---|---|
数据库 | 实现简单,易理解 | 数据库压力大 |
Redis | 易理解 | 不支持阻塞 |
Redisson | 提供锁的方法,可以阻塞 |
本文地址:https://blog.csdn.net/qq_38494341/article/details/107359544
上一篇: 当“智能”来敲门 浅谈智能家居生活