- 引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- lettuce pool连接池 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>5.6.5</version>
</dependency>
- 配置yml
spring:
application:
name: redis-demo
redis:
cluster:
nodes:
- 192.168.1.8:6379
- 192.168.1.8:6380
- 192.168.1.8:6381
- 192.168.1.8:6382
- 192.168.1.8:6383
- 192.168.1.8:6384
lettuce:
pool:
min-idle: 0 #最小空闲连接数
max-idle: 200 #最大空闲连接数
max-wait: 10000
max-active: 3000 # 最大连接数
shutdown-timeout: 1000
connect-timeout: 5000 # 连接超时时间
password: 123456
- 配置bean
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RedissonConfig {
@Bean
public RedissonClient redissonClient() {
Config config = new Config();
config.useClusterServers().setPassword("123456").addNodeAddress("redis://192.168.1.8:6379","redis://192.168.1.8:6380","redis://192.168.1.8:6381",
"redis://192.168.1.8:6382","redis://192.168.1.8:6383","redis://192.168.1.8:6384");
return Redisson.create(config);
}
}
- 使用
import org.junit.jupiter.api.Test;
import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class RedissonTest {
@Autowired
RedissonClient redissonClient;
// 简单测试
@Test
public void test() throws InterruptedException {
RLock rLock = redissonClient.getLock("rLock");
try {
rLock.tryLock();
Thread.sleep(10000);
} finally {
rLock.unlock();
}
}
// 读锁
@Test
public void test2() throws InterruptedException {
RReadWriteLock rLock = redissonClient.getReadWriteLock("rLock");
RLock readLock = rLock.readLock();
try {
readLock.tryLock();
System.out.println("abcabc");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
} finally {
readLock.unlock();
};
}
// 写锁
@Test
public void test3() throws InterruptedException {
RReadWriteLock rLock = redissonClient.getReadWriteLock("rLock");
RLock writeLock = rLock.writeLock();
try {
writeLock.tryLock();
System.out.println(Thread.currentThread().getName());
Thread.sleep(10000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
writeLock.unlock();
}
}
// 追加获取加锁超时时间
@Test
public void test() throws InterruptedException {
System.out.println("--------------------");
RLock rLock = redissonClient.getLock("rLock");
try {
rLock.tryLock(10,TimeUnit.SECONDS);
System.out.println(Thread.currentThread().getName());
Thread.sleep(1000);
System.out.println("最后一行");
} finally {
System.out.println(Thread.currentThread().getName() + "释放锁");
rLock.unlock();
System.out.println("释放锁成功");
}
System.out.println("--------------------");
}
}