需要的jar包
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>3.17.0</version>
</dependency>
配置: 哨兵,主从,集群模式的配置可以去看官网,地址:
这里使用单节点,配置如下,注意redis的地址写法,要加redis:://
@Configuration
public class MyRedissonConfig {
@Bean
// (destroyMethod = "shutdown")
public RedissonClient redissonClient() {
Config config = new Config();
config.useSingleServer().setAddress("redis://192.168.1.6:6379").setDatabase(4);
return Redisson.create(config);
}
}
锁的基本介绍:
可重入锁,公平锁,闭锁,连锁,红锁,信号量等八锁等,具体介绍看官方文档,ps:有中文文档
锁的用法:
RLock lock = redisson.getLock("anyLock");
// 最常见的使用方法
lock.lock();
上述八锁,基本用法都比较简单,获取一把锁,然后执行操作,解锁,分布式锁只要锁的名字一样,那么就是同一把锁,redisson让juc包下的八锁变得更容易使用和理解,多看官方文档,少百度。
see you!