- gitHub:https://github.com/redisson/redisson
- gitHub中文帮助文档:https://github.com/redisson/redisson/wiki/目录
pom:
org.redisson
redisson
3.17.0
配置类:
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;
@Configuration
public class MyRedissonConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private String port;
@Bean
public RedissonClient redissonClient(){
Config config = new Config();
config.useSingleServer().setAddress("redis://"+ host +":"+port);
return Redisson.create(config);
}
}
使用 说明在配置文件中配spring.redis.host和spring.redis.port,分别为redis服务器的主机和端口号
- 以下所有内容均在这个测试类中进行测试:
@SpringBootTest
@Slf4j
class RedissonTests {
@Autowired
RedissonClient redissonClient;
//测试方法...
}
- 其他锁可参考官网:https://github.com/redisson/redisson/wiki/8.-分布式锁和同步器
@Test
void test可重入锁() {
RL rl = new RL();
new Thread(rl).start();
new Thread(rl).start();
new Thread(rl).start();
//spring在测试类中,只要主线程结束,就会结束进程;不加此段,看不到结果
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
}
}
//可重入锁(Reentrant Lock)
class RL implements Runnable {
@Override
public void run() {
RLock lock = redissonClient.getLock("可重入锁");
//加锁
lock.lock();//自动续期
//lock.lock(30, TimeUnit.SECONDS);//不会自动续期,30s后过期
try {
log.info("{}获取到锁", Thread.currentThread());
Thread.sleep(3000);
} catch (Exception e) {
} finally {
//删锁
log.info("{}删除锁", Thread.currentThread());
lock.unlock();
}
}
}
lock()不传参数:
- 阻塞式等待,默认过期时间是30s,也可以通过修改Config.lockWatchdogTimeout来另行指定
- 锁的自动续期,如果业务超时,运行期间会自动给锁续上30s
- 只要占锁成功,就会启动定时任务
- 当时间过去1/3时(10秒),自动续期
lock()传过期时间:
- 发给redis执行脚本
- 不会自动续期
一般指定过期时间,长时间业务都没结束,肯定有问题!
读写锁(ReadWriteLock)使用示例:
@Test
void test读写锁() {
读写锁 rw=new 读写锁();
//读数据
rw.read();
//写数据:
rw.write(UUID.randomUUID().toString());
//新开一线程,读数据
new Thread(rw::read).start();
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
class 读写锁{
private String data;
void write(String data){
RLock writeLock = redissonClient.getReadWriteLock("读写锁").writeLock();
writeLock.lock();
//假设需要花费5秒钟
try {
Thread.sleep(5000);
this.data=data;
log.info("写入数据成功");
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
//解锁
writeLock.unlock();
}
}
String read(){
RLock readLock = redissonClient.getReadWriteLock("读写锁").readLock();
readLock.lock();
try {
log.info("读取数据成功");
return data;
}finally {
readLock.unlock();
}
}
}
运行结果:
- 第一个立即读取到,第二写数据,第三个新开了一个线程读数据,但是第二个写入完成后第三个才读到数据
读是共享锁,写是排他锁:
- 读 -> 读:相当于无锁,但会记录状态
- 读 -> 写:读完才能写
- 写 -> 读:写完才能读
- 写 -> 写:写完才能写
@Test
void test信号量() {
RSemaphore lock = redissonClient.getSemaphore("信号量");
for (int i = 0; i < 10; i++) {
double random = Math.random();
System.out.println(random);
//如果随机数大于0.5则获取,否则释放
if (random > 0.5) {
boolean b = lock.tryAcquire();//尝试获取,如果没有,就不获取
if (b) {
log.info("acquire...");
} else {
log.info("未获取到...");
}
} else {
lock.release();
log.info("release...");
}
}
}
结果:
- 每释放一个,数量加一个
- 每获取一个,数量减一个
- 个数为0,不能再获取
@Test
void test闭锁() {
RCountDownLatch lock = redissonClient.getCountDownLatch("闭锁");
lock.trySetCount(5);
new Thread(() -> {
try {
log.info("等待...");
lock.await();
log.info("成功");
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
new Thread(()->{
for (int i = 1; i < 7; i++) {
lock.countDown();
log.info("第"+i+"个...");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
设置为5个,只要够五个了,就会成功



