springcloud+redis分布式锁

分布式锁

参考连接:https://blog.51cto.com/u_16099349/6687847

public class RedisAbsentLock implements Closeable {

    private static final Logger log = LoggerFactory.getLogger(RedisAbsentLock.class);

    private final RedisTemplate<String, Object> redisTemplate;
    private final String lockKey;
    private final String lockValue;
    private final int expireTime;


    public RedisAbsentLock(RedisTemplate<String, Object> redisTemplate, String lockKey, String lockValue, int expireTime){
        this.redisTemplate = redisTemplate;
        //redis key
        this.lockKey = lockKey;
        //redis value
        this.lockValue = lockValue;
        //过期时间 单位:s
        this.expireTime = expireTime;
    }

    /**
     * 获取分布式锁
     */
    public boolean getLock(){
        //获取锁的操作
        return Boolean.TRUE.equals(redisTemplate.opsForValue().setIfAbsent(lockKey, lockValue, expireTime, TimeUnit.SECONDS));
    }

    /**
     * 获取分布式锁
     */
    public void getLock(BizException bizException){
        //获取锁的操作
        boolean result =  this.getLock();
        if (Boolean.FALSE.equals(result)){
            throw bizException;
        }
    }

    /**
     * 获取分布式锁
     */
    public void getLock(BizException bizException, String warnMsg){
        //获取锁的操作
        boolean result =  this.getLock();
        if (Boolean.FALSE.equals(result)){
            log.warn(warnMsg);
            throw bizException;
        }
    }


    /**
     * 自动释放锁
     */
    @Override
    public void close() throws IOException {
        Boolean result = Boolean.TRUE.equals(redisTemplate.delete(lockKey));
        if (Boolean.FALSE.equals(result)){
            log.warn("解锁失败锁已失效, key:{}", lockKey);
        }
    }
}

如何使用

try (RedisAbsentLock lock =
                     new RedisAbsentLock(
                             redisTemplate,
                             "rediskey",
                             "管理员",
                             0)
        ){
            // 加锁
            lock.getLock(
                    new BizException(ERR_5020.getCode(), "当前有人正在操作该笔订单,请稍后重试"),
                    MessageFormat.format("当前有人正在操作该笔订单,请稍后重试, OrderNumber:{0}", orderManage.getOrderNumber()));

       
        }

closeable

https://www.jianshu.com/p/fd5c05320645
https://blog.csdn.net/u011149152/article/details/132564038
在java 7.0j时引入了java.lang.AutoCloseable,并且java.io.Closeable接口继承自 java.lang.AutoCloseable。很多资源类都直接或间接的实现了此接口。其实这个接口与try-with-resources语法是密切相关的。

从AutoCloseable的注释可知它的出现是为了更好的管理资源,准确说是资源的释放,当一个资源类实现了该接口close方法,在使用try-with-resources语法创建的资源抛出异常后,JVM会自动调用close 方法进行资源释放,当没有抛出异常正常退出try-block时候也会调用close方法。

try-with-resources结构

https://www.cnblogs.com/kelelipeng/p/16640431.html
作用:try-with-resources 来代替try-catch-finally

适用范围(资源的定义): 任何实现 java.lang.AutoCloseable或者 java.io.Closeable 的对象

关闭资源和 final 的执行顺序: 在 try-with-resources 语句中,任何 catch 或 finally 块在声明的资源关闭后运行

Java 中类似于InputStream、OutputStream 、Scanner 、PrintWriter等的资源都需要我们调用close()方法来手动关闭,一般情况下我们都是通过try-catch-finally语句来实现这个需求,


try (Scanner scanner = new Scanner(new File("test.txt"))) {
   while (scanner.hasNext()) {
       System.out.println(scanner.nextLine());
  }
} catch (FileNotFoundException fnfe) {
   fnfe.printStackTrace();
}

你可能感兴趣的:(springboot,spring,cloud,redis,分布式)