SpringBoot集成Redisson
引用redisson start
这里使用最新版
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>3.13.6</version>
</dependency>
配置redis数据源
我这里是单机版,所以基本配置如下
spring:
redis:
port: 6379
host: reids主机地址
password: 密码
# timeout: 3000 // 有些情况下启动不起来,是由于 timeout默认是0
database: 3 // 单机版可以指定数据库名
创建测试方法
我一般学习都是创建测试使用@SpringBootTest,这样就可以将学习使用测试用例和实际操作代码分开。建议大家也这样
@SpringBootTest
class MyredissonApplicationTests {
@Resource
private RedissonClient redissonClient;
@Test
public void lock02() throws Exception {
System.out.println("spring boot run");
//创建所
RLock helloLock = redissonClient.getLock("hello");
//加锁
helloLock.lock();
try {
System.out.println("locked");
Thread.sleep(1000 * 30);
} finally {
System.out.println("begin unlock");
//释放锁
helloLock.unlock();
}
System.out.println("finished");
}
@Test
public void lock03() throws Exception {
System.out.println("spring boot run");
//创建所
RLock helloLock = redissonClient.getLock("hello");
//加锁
helloLock.lock();
System.out.println("locked");
try {
Thread.sleep(1000 * 30);
} finally {
System.out.println("begin unlock");
//释放锁
helloLock.unlock();
}
System.out.println("finished");
}
}
gitee 地址:https://gitee.com/LylYorick/myredission