@Slf4j public class RedisService { /** * Only set the key if it does not already exist. */ private static final String SET_IF_NOT_EXIST = "NX"; /** * Set the specified expire time, in milliseconds. */ private static final String SET_WITH_EXPIRE_MILL_TIME = "PX"; /** * Set the specified expire time, in seconds. */ private static final String SET_WITH_EXPIRE_SECOND_TIME = "EX"; private static final String LOCK_SUCCESS = "OK"; private static final Long RELEASE_SUCCESS = 1L; /** * 设置分布式锁.(过期时间毫秒) * @param lockKey 锁的名称. * @param requestId 持锁人ID. * @param expireTime 锁的超时时间. * @return 是否获取锁. */ public synchronized boolean tryGetDistributeLock(String lockKey, String requestId, int expireTime) { log.debug("lock key: {}, ownerId: {}, expire time: {}", lockKey, requestId, expireTime); String result = jedis.set(lockKey, requestId, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_MILL_TIME, expireTime); if (LOCK_SUCCESS.equals(result)) { return true; } return false; }