parent
85c49116df
commit
b68b199dd2
12 changed files with 318 additions and 192 deletions
@ -0,0 +1,46 @@ |
||||
package org.springblade.lims.config; |
||||
|
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.redisson.Redisson; |
||||
import org.redisson.api.RedissonClient; |
||||
import org.redisson.config.Config; |
||||
import org.springframework.beans.factory.annotation.Value; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
|
||||
/** |
||||
* redisson配置类 |
||||
* @author ytl |
||||
* @since 2022-10-13 15:11 |
||||
*/ |
||||
@Slf4j |
||||
@Configuration |
||||
public class RedissonConfig { |
||||
@Value("${spring.redis.host}") |
||||
private String host; |
||||
|
||||
@Value("${spring.redis.port}") |
||||
private String port; |
||||
|
||||
@Value("${spring.redis.password}") |
||||
private String password; |
||||
|
||||
@Bean |
||||
public RedissonClient redissonClient(){ |
||||
Config config = new Config(); |
||||
|
||||
config.useSingleServer() |
||||
.setAddress("redis://" + host + ":" + port) |
||||
.setDatabase(0) |
||||
.setPingConnectionInterval(2000); |
||||
config.useSingleServer().setPassword(password); |
||||
config.setLockWatchdogTimeout(10000L); |
||||
try{ |
||||
return Redisson.create(config); |
||||
}catch (Exception e){ |
||||
log.error("创建redisson连接错误"); |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,79 @@ |
||||
package org.springblade.lims.tools; |
||||
|
||||
import org.redisson.api.RLock; |
||||
import org.redisson.api.RedissonClient; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import java.util.concurrent.TimeUnit; |
||||
|
||||
@Component |
||||
@ConditionalOnBean(RedissonClient.class) |
||||
public class RedisLock { |
||||
@Autowired |
||||
private RedissonClient redissonClient; |
||||
|
||||
/** |
||||
* lock(), 拿不到lock就不罢休,不然线程就一直block |
||||
*/ |
||||
public RLock lock(String lockKey) { |
||||
RLock lock = redissonClient.getLock(lockKey); |
||||
lock.lock(); |
||||
return lock; |
||||
} |
||||
|
||||
/** |
||||
* leaseTime为加锁时间,单位为秒 |
||||
*/ |
||||
public RLock lock(String lockKey, long leaseTime) { |
||||
RLock lock = redissonClient.getLock(lockKey); |
||||
lock.lock(leaseTime, TimeUnit.SECONDS); |
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* timeout为加锁时间,时间单位由unit确定 |
||||
*/ |
||||
public RLock lock(String lockKey, TimeUnit unit, long timeout) { |
||||
RLock lock = redissonClient.getLock(lockKey); |
||||
lock.lock(timeout, unit); |
||||
return lock; |
||||
} |
||||
|
||||
/** |
||||
* @param lockKey 锁 key |
||||
* @param unit 单位 |
||||
* @param waitTime 等待时间 |
||||
* @param leaseTime 锁有效时间 |
||||
* @return 加锁成功? true:成功 false: 失败 |
||||
*/ |
||||
public boolean tryLock(String lockKey, TimeUnit unit, long waitTime, long leaseTime) { |
||||
|
||||
RLock lock = redissonClient.getLock(lockKey); |
||||
try { |
||||
return lock.tryLock(waitTime, leaseTime, unit); |
||||
} catch (InterruptedException e) { |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* unlock |
||||
*/ |
||||
public void unlock(String lockKey) { |
||||
RLock lock = redissonClient.getLock(lockKey); |
||||
if (lock.isLocked() && lock.isHeldByCurrentThread()) { |
||||
lock.unlock(); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* unlock |
||||
* @param lock 锁 |
||||
*/ |
||||
public void unlock(RLock lock) { |
||||
lock.unlock(); |
||||
} |
||||
} |
||||
|
||||
Loading…
Reference in new issue