1、引入依赖
具体依赖
这两个依赖版本如果选择不对的话,会造成依赖冲突
<!--Redis依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.7.16</version>
</dependency>
<!--Redisson依赖-->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>3.18.0</version>
</dependency>
整体依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xu</groupId>
<artifactId>redisson_demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
<name>redisson_demo Maven Plugin</name>
<!-- FIXME change it to the project's website -->
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.3.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--Redis依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.7.16</version>
</dependency>
<!--Redisson依赖-->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>3.18.0</version>
</dependency>
</dependencies>
</project>
2、配置编写(application.yml)
使用 redisson-spring-boot-starter 而不是 redisson 的话,就不需要手动配置RedissonClient类了。会默认使用redis的配置。
server:
port: 8080
spring:
redis:
host: localhost
port: 6379
password: 112233
3、实现分布式锁
package com.xu.service;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@Service
public class RedissonService {
// 直接注入的话,我这边会调用失败,显示没有注入到容器中,下面手动注入就没有问题。
// @Autowired
// private RedissonClient redissonClient;
int size = 0;
private RedissonClient redissonClient;
@Autowired
public RedissonService(RedissonClient redissonClient){
this.redissonClient = redissonClient;
}
public void yourMethod() {
// 获取锁对象
RLock lock = redissonClient.getLock("yourLockKey");
try {
// 尝试加锁,等待时间为15秒,锁的持有时间为30秒
if (lock.tryLock(15, 30, TimeUnit.SECONDS)) {
// 编写业务逻辑的位置
System.out.println("等待中.....");
// 线程睡眠3秒,模拟排队
Thread.sleep(3000);
System.out.println("调用完成,第" + size++ + "调用!!!");
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
// 释放锁
if (lock.isLocked() && lock.isHeldByCurrentThread()) {
lock.unlock();
}
}
}
}
4、结果演示
1. 编写接口
package com.xu.controller;
import com.xu.service.RedissonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoController {
@Autowired
RedissonService redissonService;
@GetMapping("/demo")
public void demo() {
redissonService.yourMethod();
}
}