外观
Redis配置说明
Redis是一个开源的内存数据结构存储系统,在wueasy-cloud中主要用于缓存、会话存储和消息队列等功能。本文档详细说明了Redis的配置参数和使用方法。
基础配置
yaml
redis:
# Redis服务器地址,支持单机和集群模式
addrs: 127.0.0.1:6379
# Redis密码,如果未设置密码可以省略
password: "123456"
# Redis数据库索引,默认为0
db: 0
客户端类型选择
Redis客户端会根据配置自动选择合适的客户端类型:
- 如果指定了
master-name
选项,则返回哨兵客户端 - 如果
addrs
配置了2个或以上的地址,则返回集群客户端 - 其他情况,返回 单节点客户端
示例配置:
yaml
# 哨兵模式配置
redis:
addrs: 127.0.0.1:26379,127.0.0.1:26380
master-name: "mymaster"
password: "123456"
# 集群模式配置
redis:
addrs: 127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381
password: "123456"
# 单节点配置
redis:
addrs: 127.0.0.1:6379
password: "123456"
高级配置
yaml
redis:
# 基础配置
addrs: 127.0.0.1:6379
password: "123456"
db: 0
username: "default" # Redis用户名,如果未设置可以省略
# 连接池配置
pool-size: 10 # 连接池大小,默认为10
min-idle-conns: 5 # 最小空闲连接数,默认为0
max-idle-conns: 10 # 最大空闲连接数,默认为0
pool-timeout: 4s # 连接池超时时间,默认为4秒
# 超时配置
dial-timeout: 5s # 连接超时时间,默认为5秒
read-timeout: 3s # 读取超时时间,默认为3秒
write-timeout: 3s # 写入超时时间,默认为3秒
# 重试配置
max-retries: 3 # 最大重试次数,默认为3次
min-retry-backoff: 8ms # 最小重试间隔,默认为8毫秒
max-retry-backoff: 512ms # 最大重试间隔,默认为512毫秒
# 集群配置
max-redirects: 8 # 最大重定向次数,默认为8次
read-only: false # 是否只读模式,默认为false
route-by-latency: false # 是否按延迟路由,默认为false
route-randomly: false # 是否随机路由,默认为false
# 哨兵配置
master-name: "mymaster" # 主节点名称,用于哨兵模式
配置说明
基础配置参数
参数名 | 类型 | 必填 | 默认值 | 说明 |
---|---|---|---|---|
addrs | string | 是 | - | Redis服务器地址,多个逗号分隔 |
password | string | 否 | - | Redis密码,如果未设置密码可以省略 |
db | int | 否 | 0 | Redis数据库索引 |
username | string | 否 | - | Redis用户名 |
连接池配置
参数名 | 类型 | 必填 | 默认值 | 说明 |
---|---|---|---|---|
pool-size | int | 否 | 10 | 连接池大小 |
min-idle-conns | int | 否 | 0 | 最小空闲连接数 |
max-idle-conns | int | 否 | 0 | 最大空闲连接数 |
pool-timeout | string | 否 | 4s | 连接池超时时间 |
超时配置
参数名 | 类型 | 必填 | 默认值 | 说明 |
---|---|---|---|---|
dial-timeout | Duration | 否 | 5s | 连接超时时间 |
read-timeout | Duration | 否 | 3s | 读取超时时间 |
write-timeout | Duration | 否 | 3s | 写入超时时间 |
重试配置
参数名 | 类型 | 必填 | 默认值 | 说明 |
---|---|---|---|---|
max-retries | int | 否 | 3 | 最大重试次数 |
min-retry-backoff | Duration | 否 | 8ms | 最小重试间隔 |
max-retry-backoff | Duration | 否 | 512ms | 最大重试间隔 |
集群配置
参数名 | 类型 | 必填 | 默认值 | 说明 |
---|---|---|---|---|
max-redirects | int | 否 | 8 | 最大重定向次数 |
read-only | boolean | 否 | false | 是否只读模式 |
route-by-latency | boolean | 否 | false | 是否按延迟路由 |
route-randomly | boolean | 否 | false | 是否随机路由 |
哨兵配置
参数名 | 类型 | 必填 | 默认值 | 说明 |
---|---|---|---|---|
master-name | string | 否 | - | 主节点名称,用于哨兵模式 |
使用建议
- 生产环境建议使用集群模式,提高可用性
- 密码建议使用强密码,并定期更换
- 连接池大小建议根据实际业务负载调整
- 超时时间建议根据业务特点设置合适的值
- 重试策略建议根据业务容忍度设置
注意事项
- 确保Redis服务器防火墙已开放对应端口
- 生产环境建议启用密码认证
- 集群模式下需要确保所有节点配置一致
- 建议定期监控Redis连接池状态
- 重要数据建议开启持久化