🎨 #1487 开放平台模块三方平台获取token方法支持redis分布式锁

* 三方平台支持redis分布式锁;getComponentAccessToken 加锁

* getAuthorizerAccessToken 加锁
This commit is contained in:
007gzs
2020-04-04 19:28:49 +08:00
committed by GitHub
parent d703f2d35c
commit 04f7d76057
8 changed files with 160 additions and 13 deletions

View File

@@ -118,6 +118,15 @@
<artifactId>dom4j</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>com.github.jedis-lock</groupId>
<artifactId>jedis-lock</artifactId>
</dependency>
</dependencies>
<build>

View File

@@ -0,0 +1,71 @@
package me.chanjar.weixin.common.util.locks;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import redis.clients.jedis.Jedis;
import redis.clients.util.Pool;
/**
* JedisPool 分布式锁
*
* @author <a href="https://github.com/007gzs">007</a>
*/
public class JedisDistributedLock implements Lock {
private final Pool<Jedis> jedisPool;
private final JedisLock lock;
public JedisDistributedLock(Pool<Jedis> jedisPool, String key){
this.jedisPool = jedisPool;
this.lock = new JedisLock(key);
}
@Override
public void lock() {
try (Jedis jedis = jedisPool.getResource()) {
if (!lock.acquire(jedis)) {
throw new RuntimeException("acquire timeouted");
}
} catch (InterruptedException e) {
throw new RuntimeException("lock failed", e);
}
}
@Override
public void lockInterruptibly() throws InterruptedException {
try (Jedis jedis = jedisPool.getResource()) {
if (!lock.acquire(jedis)) {
throw new RuntimeException("acquire timeouted");
}
}
}
@Override
public boolean tryLock() {
try (Jedis jedis = jedisPool.getResource()) {
return lock.acquire(jedis);
} catch (InterruptedException e) {
throw new RuntimeException("lock failed", e);
}
}
@Override
public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
try (Jedis jedis = jedisPool.getResource()) {
return lock.acquire(jedis);
}
}
@Override
public void unlock() {
try (Jedis jedis = jedisPool.getResource()) {
lock.release(jedis);
}
}
@Override
public Condition newCondition() {
throw new RuntimeException("unsupported method");
}
}