其他分享
首页 > 其他分享> > 从单体应用的JVM锁到分布式锁

从单体应用的JVM锁到分布式锁

作者:互联网

@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();
        }
    }
}

 

标签:key,product,String,lock,锁到,productId,JVM,分布式,stock
来源: https://blog.csdn.net/qq_16557637/article/details/114694857