分布式锁原理和使用网上一堆,直接上代码
package com.test;
import java.util.Collections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
@Service("redisService")
public class RedisServiceImpl {
private static final Logger logger = LoggerFactory.getLogger(RedisServiceImpl.class);
private static final String SUCCESS = "OK";
private static final Long RELEASE_SUCCESS = 1L;
@Autowired
private JedisPool jedisPool;
public JedisPool getJedisPool() {
return jedisPool;
}
public Jedis getJedis() {
return jedisPool.getResource();
}
public String set(final String key, final String value, final String nxxx, final String expx,
final int time) {
try(Jedis jedis = getJedis()){
String r