亚洲成a人片在线观看69,中文字幕免费无线观看,日韩精品无码一区二区三区四区,92精品国产自产在线观看直播,亚洲精品无码不卡在线观看屁,亚洲成av人片在www色猫咪

Redis實(shí)現分布式鎖

Redis被經(jīng)常用來(lái)實(shí)現分布式鎖,本文主要講述redis如何實(shí)現一個(gè)分布式鎖的demo。

獲取鎖

redis分布式鎖的實(shí)現主要是通過(guò)redis的set命令實(shí)現:

SET key value nx PX milliseconds
  • nx : 當key存在的時(shí)候才將set對應的key,value寫(xiě)入到redis數據庫里面。如果設置成功之后,就返回OK,已經(jīng)存在就返回nil,當前set命令也是一個(gè)原子操作,可以作為分布式鎖。
  • px milliseconds: 為當前key設置過(guò)期時(shí)間,當時(shí)間過(guò)了之后會(huì )將當前key刪除。主要是方式獲取鎖之后未正常釋放,導致死鎖,一直無(wú)法獲取鎖。超時(shí)時(shí)間需要合理設置,需要保證當前業(yè)務(wù)能夠處理完。
private static final String LOCK_SUCCESS = "OK";

  private static final Long UNLOCK_SUCCESS = 1L;

  //
  private static final String UNLOCK_SCRIPT = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";

  private final JedisCluster cluster;

  private final long lockTimeout;

  private final String lockKey;

  public RedisDistributedLockImpl(JedisCluster cluster, String lockKey, long lockTimeout) {

    this.cluster = cluster;
    this.lockTimeout = lockTimeout;
    this.lockKey = lockKey;
  }

  @Override
  public String lock() {
    long end = System.currentTimeMillis() + lockTimeout;
    String requireToken = String.valueOf(System.currentTimeMillis());
    int expireTime = 300 * 1000;
    SetParams params = new SetParams().nx().px(expireTime);
    while (System.currentTimeMillis() < end) {
      try {
        String result = cluster.set(lockKey, requireToken, params);
        if (LOCK_SUCCESS.equals(result)) {
          System.out.println("lock success");
          return requireToken;
        }
        System.out.println("lock failed trying");
        Thread.sleep(100);
      } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
      } catch (Exception e) {
        e.printStackTrace();
        return null;
      }
    }
    return requireToken;
  }

釋放鎖

釋放鎖,主要是需要通過(guò)原子操作將鎖對應的key刪除,可以通過(guò)下面lua腳本刪除。

if redis.call('get', KEYS[1]) == ARGV[1] then 
   return redis.call('del', KEYS[1]) 
else 
    return 0 
end

釋放鎖的詳細代碼:

@Override
  public boolean unlock(String identify) {
    if (identify == null || identify.trim().length() == 0) {
      return false;
    }
    Object obj = new Object();
    try {
      obj = cluster.eval(UNLOCK_SCRIPT, Collections.singletonList(lockKey), Collections.singletonList(identify));
      if (UNLOCK_SUCCESS.equals(obj)) {
        System.out.println("release lock success, requestToken:" + identify);
        return true;
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    System.out.println("release lock failed, requestToken:" + identify + ", result:" + obj);
    return false;
  }

測試分布式鎖

使用下面代碼模擬秒殺場(chǎng)景:

public class RedisDistributedLockTest {

  static int n = 500;

  public static void secKill() {
    System.out.println("商品數量:" + --n);
  }

  public static void main(String[] args) {
    Runnable runnable = () -> {
      RedisDistributedLockImpl lock = null;
      String unLockIdentify = null;
      try {
        Set<HostAndPort> set = new HashSet<>();
        set.add(new HostAndPort("127.0.0.1", 30001));
        JedisPoolConfig config = new JedisPoolConfig();
        JedisCluster cluster = new JedisCluster(set, config);
        lock = new RedisDistributedLockImpl(cluster, "test1", 20000);
        unLockIdentify = lock.lock();
        System.out.println(Thread.currentThread().getName() + "正在運行");
        secKill();
      } finally {
        if (lock != null) {
          lock.unlock(unLockIdentify);
        }
      }
    };
    System.out.println("------------------開(kāi)始--------------");
    for (int i = 0; i < 100; i++) {
      Thread t = new Thread(runnable);
      t.start();
    }
  }

}

執行結果如下:

image.png



標 題:《Redis實(shí)現分布式鎖
作 者:zeekling
提 示:轉載請注明文章轉載自個(gè)人博客:浪浪山旁那個(gè)村

評論

取消
亚洲成a人片在线观看69,中文字幕免费无线观看,日韩精品无码一区二区三区四区,92精品国产自产在线观看直播,亚洲精品无码不卡在线观看屁,亚洲成av人片在www色猫咪 欧亚精品视频一区二区三区| 国产激情无码一区二区app| 亚洲精品AV中文字幕| 中文字幕日韩精品有码视频| 亚洲成a人片在线观看| 亚洲AV久久久久久久无码| 亚洲综合色自拍一区| 亚洲综合色区无码一区爱av| 永久免费av无码网站韩国毛片| 国产熟女AA级毛片www| 无遮挡a级毛片免费看| 国产aV佳作麻豆成人| 亚洲精品中文字幕乱码无线| 亚洲精品AV无码重口另类| 亚洲七久久之综合七久久| 亚洲自拍偷拍第一| 精品樱空桃一区二区三区| 亚洲人成网站观看在线播放| 精品亚洲av二无码国产一区| 中甩老太卖婬HD播放| 国产高清精品一区二区| 亚洲图片理伦小说视頻| 亚洲天堂一区二区三区| 亚洲欧美日韩另类中文字幕组| 亚洲AV永久无码5G| 免费A级毛片在线播放不收费| 中虹钢膜建筑科技【官网】| 中文无码精品a∨在线观看不卡| 亚洲第一区第二区在线| 亚洲是精品1区2区3区久| 亚洲一区二区三区成人| 性欧美xxxxx乱极品少妇| 久久久久亚洲aV无码专区喷水| 亚洲A无码综合A国产AV中文| 亚洲色AV性色在线| 一级毛片特黄视频免费观看| 亚洲精品成人区一区二久久| 一区免费在线观看| 亚洲成a人片在线观看老师| 国产二区三区四区精品毛片| 亚洲日韩国产一区二区蜜桃| http://cambridgecinemas.com http://gxmvip.com http://cqfdkf.com http://cklt88.com http://jianshenhao.com http://baoshidamall.com