1、引入依赖
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>${redission.starter.version}</version>
</dependency>
2、配置文件
spring.redis.host = redis-svc
spring.redis.port = 6379
spring.redis.password =
spring.redis.timeout = 3000
spring.redis.database = 3
3、在项目中使用
@Configuration
public class RedissonConfig {
@Bean
public RedissonAutoConfigurationCustomizer redissonAutoConfigurationCustomizer() {
return config -> {
if (CharSequenceUtil.isBlank(config.useSingleServer().getPassword())) {
config.useSingleServer().setPassword(null);
}
};
}
}
@Autowired
private RedissonClient redissonClient;
4、分布式锁
RLock lock = redissonClient.getLock(lockKey);
try {
isLocked = lock.tryLock(DEFAULT_WAIT_TIME, TimeUnit.SECONDS);
if(isLocked){
doSomeThing();
}
}finally {
if (lock.isHeldByCurrentThread()) {
lock.unlock();
}
}