@RestController
public class distributedLockController {
@Autowired
private StringRedistemplate redistemplate;
@Autowired
private RedissonClient redissonClient;
private static final reentrantlock reentrantlock = new reentrantlock();
private static final String product_stock_key = "product_stock_%s"; //库存key
private static final String lockKey = "lock_key_%s"; //分布式锁key
@GetMapping("/java/lock/{productId}")
public String jvmlock(@PathVariable("productId") Integer productId) {
synchronized (this) {
String key = String.format(product_stock_key, productId);
Integer product_stock = Integer.parseInt(redistemplate.opsForValue().get(key));
if (product_stock > 0) {
product_stock--;
redistemplate.opsForValue().set(key, String.valueOf(product_stock));
return "current stock is " + product_stock;
}else {
return "当前业务繁忙,请稍后再试";
}
}
}
//单机应用
@GetMapping("/java/lock/{productId}")
public String reentrantJavaLock(@PathVariable("productId") Integer productId) {
try {
String key = String.format(product_stock_key, productId);
reentrantlock.lock();
Integer product_stock = Integer.parseInt(redistemplate.opsForValue().get(key));
if (product_stock > 0) {
product_stock--;
redistemplate.opsForValue().set(key, String.valueOf(product_stock));
return "current stock is " + product_stock;
} else {
return "当前业务繁忙,请稍后再试";
}
} finally {
reentrantlock.unlock();
}
}
@GetMapping("/nx/lock/{productId}")
public String setNXLock(@PathVariable("productId") Integer productId) {
String value = UUID.randomUUID().toString() + Thread.currentThread().getName();
String stock_key = String.format(product_stock_key, productId);
String lock_Key = String.format(lockKey, productId);
try {
Boolean success = redistemplate.opsForValue().setIfAbsent(lock_Key, value, 10, TimeUnit.SECONDS);
if (!success)
return "当前业务繁忙,请稍后再试";
Integer current_product_stock = Integer.parseInt(redistemplate.opsForValue().get(stock_key));
if (current_product_stock > 0) {
current_product_stock--;
redistemplate.opsForValue().set(stock_key, String.valueOf(current_product_stock));
return "current stock is " + current_product_stock;
} else {
return "抱歉!当前无库存";
}
} finally {
if (redistemplate.opsForValue().get(lock_Key).equals(value)) //是自己的锁
redistemplate.delete(lock_Key); //释放当前的锁
}
}
@GetMapping("/redisson/lock/{productId}")
public String redissonLock(@PathVariable("productId") Integer productId) {
String stock_key = String.format(product_stock_key, productId);
String lock_key = String.format(lockKey, productId);
RLock lock = redissonClient.getLock(lock_key);
try {
lock.lock();
Integer current_product_stock = Integer.parseInt(redistemplate.opsForValue().get(stock_key));
if (current_product_stock > 0) {
current_product_stock--;
redistemplate.opsForValue().set(stock_key, String.valueOf(current_product_stock));
return "current stock is " + current_product_stock;
} else {
return "抱歉!当前无库存";
}
} finally {
if (lock.isLocked() && lock.isHeldByCurrentThread()) //必须是锁的且是自己持有的锁
lock.unlock();
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。