redis实现方式:
package cn.ping.redislock.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@RestController
public class StockController {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@GetMapping("/stock/reduceStock")
public String reduceStock(){
String stockKey = "stock_key";
//生成每个线路不一样的uuid值,防止其他线程删除key(key超时redis删除key,其他线程加锁)
String stock_id = UUID.randomUUID().toString();
Integer stock = Integer.parseInt(stringRedisTemplate.opsForValue().get("stock"));
//如果key不存在,则保存到redis返回true,返回不保存返回false
Boolean ifAbsent = stringRedisTemplate.opsForValue().setIfAbsent(stockKey, stock_id, 10, TimeUnit.SECONDS);
if (!ifAbsent){
return "error";
}
try {
if (stock > 0) {
stock = stock - 1;
stringRedisTemplate.opsForValue().set("stock", stock + "");
System.out.println("扣减成功:库存剩余为:" + stock);
} else {
System.out.println("扣减失败:库存不足");
}
}finally {
//如果不判断,执行到超时了,就删除其他线程加的锁
if (stringRedisTemplate.opsForValue().get(stockKey).equals(stock_id)){
//如果刚好到这里超时了??? 给锁的超时续期来解决
stringRedisTemplate.delete(stockKey);
}
}
return "end";
}
}
redisson实现方式:
mvn:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.ping</groupId>
<artifactId>redis-lock</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>redis-lock</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.6.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
bean注入:
@SpringBootApplication
public class RedisLockApplication {
public static void main(String[] args) {
SpringApplication.run(RedisLockApplication.class, args);
}
@Bean
public Redisson redisson(){
Config config = new Config();
//使用单例redis
config.useSingleServer().setAddress("http://localhost:6379").setDatabase(0);
return (Redisson)Redisson.create(config);
}
}
加锁解锁:
package cn.ping.redislock.controller;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@RestController
public class StockController {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private Redisson redisson;
@GetMapping("/stock/reduceStock")
public String reduceStock(){
String stockKey = "stock_key";
Integer stock = Integer.parseInt(stringRedisTemplate.opsForValue().get("stock"));
//获取一个锁对象
RLock lock = redisson.getLock(stockKey);
try {
lock.lock(10,TimeUnit.SECONDS);//加锁
if (stock > 0) {
stock = stock - 1;
stringRedisTemplate.opsForValue().set("stock", stock + "");
System.out.println("扣减成功:库存剩余为:" + stock);
} else {
System.out.println("扣减失败:库存不足");
}
}finally {
lock.unlock();//解锁
}
return "end";
}
}