yujiasun / distributed-kit Goto Github PK
View Code? Open in Web Editor NEWDistributed lock implementation using Redis and Zookeeper.基于redis和zookeeper分布式工具集-包括:分布式锁实现,分布式速率限制器,分布式序列等
License: Apache License 2.0
Distributed lock implementation using Redis and Zookeeper.基于redis和zookeeper分布式工具集-包括:分布式锁实现,分布式速率限制器,分布式序列等
License: Apache License 2.0
public static void main(String[] args) throws Exception {
JedisPool jedisPool=new JedisPool("127.0.0.1",6379);//实际应用时可通过spring注入
RedisReentrantLock lock=new RedisReentrantLock(jedisPool,"订单流水号");
try {
if (lock.tryLock(5000L, TimeUnit.MILLISECONDS)) {//获取锁超时时间为5秒
//TODO 获得锁后要做的事
}else{
//TODO 获得锁超时后要做的事
}
}finally {
lock.unlock();
}
}
应该把try finally写在获得锁成功时的分支里吧?不然即使没获取锁也会释放锁,把别的线程的锁释放了
private String createRedisKey(String lockId) {
Jedis jedis = null;
boolean broken = false;
try {
String value=lockId+randomId(1);
jedis = jedisPool.getResource();
String luaScript = ""
+ "\nlocal r = tonumber(redis.call('SETNX', KEYS[1],ARGV[1]));"
+ "\nredis.call('PEXPIRE',KEYS[1],ARGV[2]);"
+ "\nreturn r";
List keys = new ArrayList();
keys.add(lockId);
List args = new ArrayList();
args.add(value);
args.add(lockTimeout+"");
Long ret = (Long) jedis.eval(luaScript, keys, args);
if( new Long(1).equals(ret)){
return value;
}
}finally {
if(jedis!=null) jedis.close();
}
return null;
}
在tryRedisLock的while循环中,每次调用createRedisKey时都会将锁的过期时间延长,此处是否有意如此?
编译出错:Distributed-Kit/src/main/java/com/distributed/lock/zk/ZkReentrantLock.java:[110,59] 错误: 编码UTF-8的不可映射字符
InterProcessMutex本身创建的节点就是临时有序的,断开连接会自动删除的,请问是出于什么考虑要自己写定时器ZkReentrantLockCleanerTask清除呢?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.