码迷,mamicode.com
首页 > 编程语言 > 详细

Redis分布式锁Java实现类

时间:2018-11-27 23:44:49      阅读:403      评论:0      收藏:0      [点我收藏+]

标签:long   false   class   java实现   exp   html   system   ram   分布式   

Redis分布式锁Java实现类

 

package com.utils;

import java.util.Collections;
import java.util.UUID;

import redis.clients.jedis.Jedis;

public class RedisTool {
    
    private static final String LOCK_SUCCESS = "OK";
    private static final String SET_IF_NOT_EXIST = "NX";
    private static final String SET_WITH_EXPIRE_TIME = "PX";
    private static final String SCRIPT = "if redis.call(‘get‘, KEYS[1]) == ARGV[1] then return redis.call(‘del‘, KEYS[1]) else return 0 end";
    private static final Long RELEASE_SUCCESS = 1L;
    
    // test
    private int count = 0;
    
    /**
     * 获取请求Id 
     * @return 返回UUID
     */
    public static String getRequestId(){
        return UUID.randomUUID().toString();
    }
    
    /**
     * 尝试获取分布式锁
     * @param jedis Redis客户端
     * @param lockKey 锁
     * @param requestId 请求标识
     * @param expireTime 超期时间
     * @return 是否获取成功
     */
    public static boolean tryGetDistributedLock(Jedis jedis, String lockKey, String requestId, int expireTime) {
 
        String result = jedis.set(lockKey, requestId, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, expireTime);
 
        if (LOCK_SUCCESS.equals(result)) {
            return true;
        }
        return false;
    }
    
    
    /**
     * 释放分布式锁
     * @param jedis Redis客户端
     * @param lockKey 锁
     * @param requestId 请求标识
     * @return 是否释放成功
     */
    public static boolean releaseDistributedLock(Jedis jedis, String lockKey, String requestId) {
 
        Object result = jedis.eval(SCRIPT, Collections.singletonList(lockKey), Collections.singletonList(requestId));
 
        if (RELEASE_SUCCESS.equals(result)) {
            return true;
        }
        return false;
    }
    
    public static void main(String[] args) {
        RedisTool redisTool = new RedisTool();
        redisTool.testDistributedLock();
    }
    
    public void testDistributedLock() {
        for (int i = 0; i < 10; i++) {
            new Thread(new Runnable() {

                @Override
                public void run() {
                    Jedis jedis = new Jedis("localhost");
                    String lockKey = "lock";
                    String requestId = getRequestId();
                    // 加锁
                    if (tryGetDistributedLock(jedis, lockKey, requestId, 10)) {
                        count++;
                        System.out.println(requestId + ":" + count);
                        // 解锁
                        releaseDistributedLock(jedis, lockKey, requestId);
                    } else {
                        System.out.println(requestId + ": tryGetDistributedLock fail");
                    }

                }
            });
        }
    }
}

 

 

转自:http://www.importnew.com/27477.html

Redis分布式锁Java实现类

标签:long   false   class   java实现   exp   html   system   ram   分布式   

原文地址:https://www.cnblogs.com/harry335/p/10029765.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!