关闭

Redis分布式锁

瞎溜达 1年前 ⋅ 159 阅读
  1. 自定义分布式锁:
        //加锁
        public boolean lock(String lockName, String lockTime) {
            //如果key值不存在,则返回 true,且设置 value
            if (redisTemplate.opsForValue().setIfAbsent(lockName, lockTime)) {
                return true;
            }
            //获取lockName的值
            String curVal = (String) redisTemplate.opsForValue().get(lockName);
            //判断是是否超时
            if (!StringUtils.isEmpty(curVal) && Long.parseLong(curVal) < System.currentTimeMillis()) {
                //获得之前的key值,同时设置当前的传入的过期时间lockTime。
                String savedLockTime = (String) redisTemplate.opsForValue().getAndSet(lockName, lockTime);
                //判断存入的lockTime是否是当前传入的lockTime
                if (!StringUtils.isEmpty(savedLockTime) && savedLockTime.equals(curVal)) {
                    return true;
                }
            }
            return false;
        }​
    
        //解锁
        public void unlock(String lockName, String lockTime) {
            try {
                String curVal = (String) redisTemplate.opsForValue().get(lockTime);
                if (!StringUtils.isEmpty(curVal) && curVal.equals(lockName)) {
                    redisTemplate.opsForValue().getOperations().delete(lockTime);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

     

    加锁代码:
            try {
                String lockName = "lock" + userId;
                String lockTime = String.valueOf(System.currentTimeMillis() + 5000);
                if (lock(lockName, lockTime)) {
                    //TODO
                }
            } finally {
                unlock(lockName, lockTime);
            }​
  2. spring-integration-redis中的分布式锁:
    1. maven需要引入
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-redis</artifactId>
      </dependency>
      
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-integration</artifactId>
      </dependency>
      
      <dependency>
        <groupId>org.springframework.integration</groupId>
        <artifactId>spring-integration-redis</artifactId>
      </dependency>​
    2. 添加RedisLock配置

      @Bean
      public RedisLockRegistry redisLockRegistry(RedisConnectionFactory redisConnectionFactory) {
          return new RedisLockRegistry(redisConnectionFactory, "redis-lock");
      }
    3. 使用代码
              String key = "lock:" + fieldName + "_" + value;
              Lock lock = redisLockRegistry.obtain(key);
              try {
                  //尝试获取锁,如果获取不到会继续尝试
                  boolean b1 = lock.tryLock(5L, TimeUnit.SECONDS);
                  if (!b1) {
                      return ResponseResult.argumentError("system busy");
                  }
                  //TODO
      
              } catch (Throwable ex) {
                  ex.printStackTrace();
              } finally {
                  lock.unlock();
              }​

全部评论: 0

    我有话说: