Files
FlashSaleSystem/docs/Redis配置指南.md
2025-07-29 22:13:47 +08:00

110 lines
2.3 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Redis配置说明
本项目支持三种Redis部署模式单节点模式、集群模式和哨兵模式。
## 配置方式
### 1. 单节点模式(默认)
默认配置文件 `application.yml` 已配置为单节点模式:
```yaml
spring:
redis:
host: localhost
port: 6379
database: 0
password: your-password
```
### 2. 集群模式
使用 `application-cluster.yml` 配置文件:
```bash
# 运行时指定profile
java -jar FlashSaleSystem.jar --spring.profiles.active=cluster
# 或设置环境变量
export SPRING_PROFILES_ACTIVE=cluster
java -jar FlashSaleSystem.jar
```
### 3. 哨兵模式
`application.yml` 中启用哨兵配置:
```yaml
spring:
redis:
sentinel:
master: mymaster
nodes: localhost:26379,localhost:26380,localhost:26381
password: your-password
```
## 切换Redis模式
### 方式一修改application.yml
直接编辑 `src/main/resources/application.yml`,注释/取消注释相应配置块。
### 方式二使用Spring Profile
1. 本地开发(单节点):
```bash
mvn spring-boot:run -Dspring.profiles.active=dev
```
2. 生产环境(集群):
```bash
mvn spring-boot:run -Dspring.profiles.active=cluster
```
### 方式三:环境变量覆盖
```bash
# 覆盖Redis主机
export SPRING_REDIS_HOST=192.168.1.100
export SPRING_REDIS_PORT=6379
# 覆盖为集群模式
export SPRING_REDIS_CLUSTER_NODES=192.168.1.100:7000,192.168.1.100:7001
java -jar FlashSaleSystem.jar
```
## 配置优先级
1. 命令行参数(最高优先级)
2. 环境变量
3. application-{profile}.yml
4. application.yml最低优先级
## 注意事项
1. **单节点模式**:适合开发和测试环境,不建议生产使用
2. **集群模式**:推荐生产环境使用,提供高可用性
3. **密码配置**:生产环境建议使用环境变量配置密码
4. **连接池**:根据实际负载调整连接池参数
## 验证Redis连接
启动应用后,可以通过以下方式验证:
1. 查看启动日志:
```
初始化Redisson客户端...
配置Redis单机模式: localhost:6379
Redisson客户端初始化完成
```
2. 访问测试接口:
```
GET http://localhost:8080/api/test/redis/connection
```
3. 检查健康状态:
```
GET http://localhost:8080/actuator/health
```