redis实现分布式锁以及redission使用

编程

分布式环境有很多问题,比如你前一个请求访问的是服务器A,第二个请求访问到了服务器B,就会发生并发重复插入问题,这个时候需要依赖单点来实现分布锁,而redission就是。

第一步、引入redis和redisson依赖

第二步、创建redission工具类且测试

public class RedissonUtils {

private static Logger logger = LoggerFactory.getLogger(RedissonUtils.class);

private static RedissonUtils redissionUtils;

private RedissonUtils (){}

public static RedissonUtils getInstance(){

if(redissionUtils == null)

synchronized(RedissonUtils.class){

if(redissionUtils == null)redissionUtils = new RedissonUtils();

}

return redissionUtils;

}

public RedissonClient getRedisson(Config config){

RedissonClient redisson = Redisson.create(config);

logger.info("成功连接Redis Server");

return redisson;

}

public RedissonClient getRedisson(String ip,String port){

Config config = new Config ();

config.useSingleServer().setAddress(ip+":"+port);

RedissonClient redisson = Redisson.create(config);

logger.info("成功连接Redis server ");

return redisson;

}

public void closeRedisson(RedissonClient redisson){

redisson.shutdown();

logger.info("成功关闭redis Client连接");

}

public <T> RBucket<T> getRBucket(RedissonClient redisson, String objectName){

RBucket<T> bucket = redisson.getBucket(objectName);

return bucket;

}

public <K,V> RMap<K,V> getMap(RedissonClient redisson,String objectName){

RMap<K,V> map = redisson.getMap(objectName);

return map;

}

public <V>RSortedSet<V> getRSortedSet(RedissonClient redisson,String objectName){

RSortedSet<V> sortedSet = redisson.getSortedSet(objectName);

return sortedSet;

}

public <V> RSet<V> getRSet(RedissonClient redisson ,String objectName){

RSet<V> rSet = redisson.getSet(objectName);

return rSet;

}

public <V> RList<V> getList(RedissonClient redisson, String objectName){

RList<V> rList = redisson.getList(objectName);

return rList;

}

public <V> RQueue<V> getQueue(RedissonClient redisson,String objectName){

RQueue<V> rQueue = redisson.getQueue(objectName);

return rQueue;

}

public <V> RDeque<V> getDeque(RedissonClient redisson,String objectName){

RDeque<V> rDeque = redisson.getDeque(objectName);

return rDeque;

}

public RLock getRLock(RedissonClient redisson,String objectName){

RLock rLock = redisson.getLock(objectName);

return rLock;

}

public RAtomicLong getRAtomicLong(RedissonClient redisson,String objectName){

RAtomicLong rAtomicLong = redisson.getAtomicLong(objectName);

return rAtomicLong;

}

public RCountDownLatch getRCountDownLatch(RedissonClient redisson,String objectName){

RCountDownLatch rCountDownLatch = redisson.getCountDownLatch(objectName);

return rCountDownLatch;

}

public <M> RTopic<M> getRTopic (RedissonClient redisson,String objectName){

RTopic<M> rTopic = redisson.getTopic(objectName);

return rTopic;

}

@Test

public void test() throws InterruptedException{

Config config = new Config();

SingleServerConfig singleServerConfig = config.useSingleServer();

singleServerConfig.setAddress("127.0.0.1:6379");

singleServerConfig.setPassword("123456");

RedissonClient redissonClient = RedissonUtils.getInstance().getRedisson(config);

RBucket<Object> rBucket = RedissonUtils.getInstance().getRBucket(redissonClient,"key");

while (true){

RLock lock = redissonClient.getLock("lock");

lock.tryLock(0,1, TimeUnit.SECONDS);

try {

System.out.println("执行");

}finally {

lock.unlock();

}

}

}

 

以上是 redis实现分布式锁以及redission使用 的全部内容, 来源链接: utcz.com/z/515633.html

回到顶部