feat(redis): 集成 Redisson 并统一配置管理
- 添加 Redisson 依赖并配置自动装配 - 创建 application-common.yml 统一管理 Redis 配置 - 在 application.yml 中引入公共配置文件 - 修改生产环境配置以引用公共 Redis 配置 - 替换原生 RedisTemplate 为 Redisson 客户端 - 实现基于 Redisson 的分布式锁和缓存功能
This commit is contained in:
parent
e5f01458a1
commit
83fc577186
14
pom.xml
14
pom.xml
|
|
@ -12,6 +12,7 @@
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
<spring-boot.version>3.2.2</spring-boot.version>
|
<spring-boot.version>3.2.2</spring-boot.version>
|
||||||
|
<redisson.version>3.23.5</redisson.version>
|
||||||
</properties>
|
</properties>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|
@ -55,12 +56,6 @@
|
||||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!--spring security
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-security</artifactId>
|
|
||||||
</dependency> -->
|
|
||||||
|
|
||||||
<!--commons-lang3 扩展工具包,如文件上传-->
|
<!--commons-lang3 扩展工具包,如文件上传-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.commons</groupId>
|
<groupId>org.apache.commons</groupId>
|
||||||
|
|
@ -248,6 +243,13 @@
|
||||||
<version>3.3.5</version>
|
<version>3.3.5</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Redisson 分布式锁和缓存 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.redisson</groupId>
|
||||||
|
<artifactId>redisson-spring-boot-starter</artifactId>
|
||||||
|
<version>${redisson.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,47 @@
|
||||||
|
package com.kexue.skills.config;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Redisson配置类
|
||||||
|
*
|
||||||
|
* @author 王志维
|
||||||
|
* @since 2026-01-28
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class RedissonConfig {
|
||||||
|
|
||||||
|
@Value("${common.redis.host}")
|
||||||
|
private String host;
|
||||||
|
|
||||||
|
@Value("${common.redis.port}")
|
||||||
|
private int port;
|
||||||
|
|
||||||
|
@Value("${common.redis.password}")
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
@Value("${common.redis.database}")
|
||||||
|
private int database;
|
||||||
|
|
||||||
|
@Bean(destroyMethod = "shutdown")
|
||||||
|
public RedissonClient redissonClient() {
|
||||||
|
Config config = new Config();
|
||||||
|
|
||||||
|
// 单节点模式
|
||||||
|
config.useSingleServer()
|
||||||
|
.setAddress("redis://" + host + ":" + port)
|
||||||
|
.setPassword(password)
|
||||||
|
.setDatabase(database)
|
||||||
|
.setConnectionMinimumIdleSize(5)
|
||||||
|
.setConnectionPoolSize(20)
|
||||||
|
.setTimeout(10000);
|
||||||
|
|
||||||
|
return Redisson.create(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -20,6 +20,8 @@ import com.kexue.skills.utils.MD5Util;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.dromara.sms4j.api.SmsBlend;
|
import org.dromara.sms4j.api.SmsBlend;
|
||||||
import org.dromara.sms4j.core.factory.SmsFactory;
|
import org.dromara.sms4j.core.factory.SmsFactory;
|
||||||
|
import org.redisson.api.RBucket;
|
||||||
|
import org.redisson.api.RedissonClient;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
|
@ -46,6 +48,9 @@ public class SysUserServiceImpl implements SysUserService {
|
||||||
@Resource
|
@Resource
|
||||||
private org.springframework.data.redis.core.RedisTemplate<String, String> redisTemplate;
|
private org.springframework.data.redis.core.RedisTemplate<String, String> redisTemplate;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private RedissonClient redissonClient;
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private CaptchaConfig captchaConfig;
|
private CaptchaConfig captchaConfig;
|
||||||
|
|
||||||
|
|
@ -627,7 +632,7 @@ public class SysUserServiceImpl implements SysUserService {
|
||||||
String code = generateSmsCode();
|
String code = generateSmsCode();
|
||||||
|
|
||||||
// 存储验证码到Redis,有效期5分钟
|
// 存储验证码到Redis,有效期5分钟
|
||||||
redisTemplate.opsForValue().set("sms_code:" + phone, code, 300, java.util.concurrent.TimeUnit.SECONDS);
|
redissonClient.getBucket("sms_code:" + phone).set(code, 300, java.util.concurrent.TimeUnit.SECONDS);
|
||||||
|
|
||||||
// 获取默认的短信发送器
|
// 获取默认的短信发送器
|
||||||
SmsBlend sms = SmsFactory.getSmsBlend();
|
SmsBlend sms = SmsFactory.getSmsBlend();
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
# 公共配置
|
||||||
|
common:
|
||||||
|
# Redis配置
|
||||||
|
redis:
|
||||||
|
host: 43.248.97.33
|
||||||
|
port: 16380
|
||||||
|
password: 5Qsd1rTx3S7rKs0A
|
||||||
|
database: 1
|
||||||
|
|
@ -12,7 +12,19 @@ spring:
|
||||||
hikari:
|
hikari:
|
||||||
pool-name: ProdHikariPool
|
pool-name: ProdHikariPool
|
||||||
maximum-pool-size: 30
|
maximum-pool-size: 30
|
||||||
|
# Redis配置,引用公共配置
|
||||||
|
redis:
|
||||||
|
host: ${common.redis.host}
|
||||||
|
port: ${common.redis.port}
|
||||||
|
password: ${common.redis.password}
|
||||||
|
database: ${common.redis.database}
|
||||||
|
timeout: 10000
|
||||||
|
lettuce:
|
||||||
|
pool:
|
||||||
|
max-active: 8
|
||||||
|
max-wait: -1
|
||||||
|
max-idle: 8
|
||||||
|
min-idle: 0
|
||||||
|
|
||||||
# Sa-Token配置
|
# Sa-Token配置
|
||||||
sa-token:
|
sa-token:
|
||||||
|
|
@ -32,16 +44,16 @@ sa-token:
|
||||||
is-header: true
|
is-header: true
|
||||||
# 是否使用Redis存储token
|
# 是否使用Redis存储token
|
||||||
is-redis: true
|
is-redis: true
|
||||||
# Redis配置
|
# Redis配置,引用公共配置
|
||||||
redis:
|
redis:
|
||||||
# Redis主机地址
|
# Redis主机地址
|
||||||
host: 43.248.97.19
|
host: ${common.redis.host}
|
||||||
# Redis端口
|
# Redis端口
|
||||||
port: 16379
|
port: ${common.redis.port}
|
||||||
# Redis密码
|
# Redis密码
|
||||||
password: 654321
|
password: ${common.redis.password}
|
||||||
# Redis数据库索引
|
# Redis数据库索引
|
||||||
database: 1
|
database: ${common.redis.database}
|
||||||
|
|
||||||
# 验证码配置
|
# 验证码配置
|
||||||
captcha:
|
captcha:
|
||||||
|
|
@ -75,10 +87,10 @@ jetcache:
|
||||||
minIdle: 5
|
minIdle: 5
|
||||||
maxIdle: 20
|
maxIdle: 20
|
||||||
maxTotal: 50
|
maxTotal: 50
|
||||||
host: 43.248.97.19
|
host: ${common.redis.host}
|
||||||
port: 16379
|
port: ${common.redis.port}
|
||||||
password: 654321
|
password: ${common.redis.password}
|
||||||
database: 1
|
database: ${common.redis.database}
|
||||||
# 全局过期时间配置
|
# 全局过期时间配置
|
||||||
global:
|
global:
|
||||||
# 全局默认超时时间(毫秒)
|
# 全局默认超时时间(毫秒)
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,10 @@ spring:
|
||||||
max-request-size: 20MB
|
max-request-size: 20MB
|
||||||
max-file-size: 20MB
|
max-file-size: 20MB
|
||||||
|
|
||||||
|
# 包含公共配置文件
|
||||||
|
spring.config.import:
|
||||||
|
- classpath:application-common.yml
|
||||||
|
|
||||||
springdoc:
|
springdoc:
|
||||||
api-docs:
|
api-docs:
|
||||||
enabled: true
|
enabled: true
|
||||||
|
|
@ -68,4 +72,3 @@ sms:
|
||||||
# 短信模板变量名,对应验证码的变量
|
# 短信模板变量名,对应验证码的变量
|
||||||
template-param-name: code
|
template-param-name: code
|
||||||
template-cache: true
|
template-cache: true
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue