一、单机版
SpringBootRedisApplication
package com.example.demo;
import org.redisson.Redisson;
import org.redisson.config.Config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class SpringBootRedisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootRedisApplication.class, args);
}
@Bean
public Redisson redisson(){
Config config = new Config();
config.useSingleServer().setAddress("redis://*.*.*.*:6379").setDatabase(0);
return (Redisson)Redisson.create(config);
}
}
GoodControllerSimple
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GoodControllerSimple {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Value("${server.port}")
private String serverPort;
@GetMapping("/deduct_stock")
public String deduceStock(){
try {
synchronized (this){
int stock = Integer.parseInt(stringRedisTemplate.opsForValue().get("stock"));
if (stock > 0) {
int realStock = stock -1;
stringRedisTemplate.opsForValue().set("stock",String.valueOf(realStock));
System.out.println("扣减成功,库存还剩下: " + realStock);
}else{
System.out.println("扣减成功,库存不足!");
}
}
}catch(Exception e){
e.printStackTrace();
}finally {
return "商品已经售完/活动结束/调用超时,欢迎下次光临";
}
}
}
二、单机版超卖问题解决方案1
GoodControllerIndex
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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 GoodControllerIndex {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Value("${server.port}")
private String serverPort;
@GetMapping("/killGoodsIndex")
public String killGoods(){
String lockKey = "product_001";
String clientId = UUID.randomUUID().toString();//定义唯一的当前线程标志,用于最后删除标志,防止误删别的锁
Boolean result = stringRedisTemplate.opsForValue().setIfAbsent(lockKey,clientId,10L, TimeUnit.SECONDS);//setnx功能
if(!result){
return "商品抢购失败】";
}
try {
String stock = stringRedisTemplate.opsForValue().get("stock");//获取库存量
int amount = Integer.parseInt(stock);
if (amount > 0) {
int realStock = amount - 1;
stringRedisTemplate.opsForValue().set("stock", String.valueOf(realStock));//扣减库存后,将真实剩余库存存入
System.out.println("扣减成功,库存还剩下: " + realStock);
}else{
System.out.println("扣减成功,库存不足!");
}
}finally {
if (clientId.equals(stringRedisTemplate.opsForValue().get(lockKey))){//只删除本线程锁lockKey
stringRedisTemplate.delete(lockKey);
}
}
return "商品已经售完/活动结束/调用超时,欢迎下次光临";
}
}
三、超卖问题解决方案2
GoodRedissonController
package com.example.demo.controller;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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 GoodRedissonController {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Value("${server.port}")
private String serverPort;
@Autowired
private Redisson redisson;
@GetMapping("/killGoodsRedisson")
public String killGoods(){
String lockKey = "product_001";
RLock redissonLock = redisson.getLock(lockKey);
try {
redissonLock.lock();
String stock = stringRedisTemplate.opsForValue().get("stock");//获取库存量
int amount = stock == null ? 0 : Integer.parseInt(stock);
if (amount > 0) {
int realAmount = amount - 1;
stringRedisTemplate.opsForValue().set("stock", String.valueOf(realAmount));//扣减库存后,将真实剩余库存存入
System.out.println("扣减成功,库存还剩下: " + realAmount);
}else{
System.out.println("扣减成功,库存不足!");
}
}catch(Exception e){
e.printStackTrace();
}finally {
redissonLock.unlock();
}
return "商品已经售完/活动结束/调用超时,欢迎下次光临";
}
}
四、Jmeter压测
首先在redis中创建库存stock为200
结果如下,最后扣减为0