1、依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>3.15.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
2、application.yml配置
spring:
redis:
database: 1
timeout: 60000
password: admin1234
host: localhost
port: 6379
server:
port: 8081
3、Redission配置类
@Configuration
public class MyRedissonConfig {
@Value(value = "${spring.redis.host}")
private String host;
@Value(value = "${spring.redis.port}")
private int port;
@Value(value = "${spring.redis.database}")
private int database;
@Value(value = "${spring.redis.password}")
private String password;
@Bean(destroyMethod = "shutdown")
RedissonClient redisson() {
Config config = new Config();
SingleServerConfig singleServerConfig = config.useSingleServer();
String address = "redis://" + host + ":" + port;
singleServerConfig.setAddress(address);
singleServerConfig.setDatabase(database);
singleServerConfig.setPassword(password);
return Redisson.create(config);
}
}
4、Controller
@RestController
@RequestMapping("/redission")
public class RedissionController {
@Resource
private RedissonClient redissonClient;
private final static String LOCK="I_AM_LOCK";
@GetMapping("/list")
public String getString(){
RLock lock = redissonClient.getLock(LOCK);
lock.lock(10, TimeUnit.NANOSECONDS);
try {
System.out.println(Thread.currentThread().getName()+":\t 获得锁");
Thread.sleep(50000);
} catch (Exception e) {
e.printStackTrace();
}finally {
lock.unlock();
System.out.println(Thread.currentThread().getName()+":\t 释放锁锁");
}
return "123";
}
@GetMapping("write")
public String writeValue(){
RReadWriteLock myLock = redissonClient.getReadWriteLock ("my_lock");
RLock rLock = myLock.writeLock ( );
rLock.lock ();
String s = "";
try {
System.out.println ("写锁加锁成功..."+Thread.currentThread ().getId () );
s= UUID.randomUUID ().toString ();
TimeUnit.SECONDS.sleep (5);
} catch (Exception e) {
e.printStackTrace ( );
}
finally {
System.out.println ("写锁解锁成功..."+Thread.currentThread ().getId () );
rLock.unlock ();
}
return s;
}
@GetMapping("read")
public String readValue(){
RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("my_lock");
RLock rLock = readWriteLock.readLock();
rLock.lock ();
String s = "";
try {
System.out.println ("读锁加锁成功..."+Thread.currentThread ().getId () );
s = "345";
Thread.sleep(3000);
} catch (Exception e) {
e.printStackTrace ( );
}finally {
System.out.println ("读锁解锁成功..."+Thread.currentThread ().getId () );
rLock.unlock ();
}
return s;
}
}
5、启动类
@SpringBootApplication
public class RedissionDemoApplication {
public static void main(String[] args) {
SpringApplication.run(RedissionDemoApplication.class, args);
}
}
6、测试