@RequestMapping("redisLock")
public String redis(){
//JVM进程级别锁--允许情况下只在JVM内部有效(集群主从架构-多个JVM,锁失效,下面可以解决)
synchronized (this){
int i = Integer.parseInt(stringRedisTemplate.opsForValue().get("suwei"));
if (i>0){
i = i-1;
stringRedisTemplate.opsForValue().set("suwei",i+"");
System.out.println("购买成功,剩余库存"+i);
}else{
System.out.println("购买失败,库存不足");
}
}
return "OK";
}