Skip to content

Commit 381172c

Browse files
feat: support Redis Sentinel and Redis Cluster (#1952)
* feature: support Redis Sentinel and Redis Cluster * chore: update implementation --------- Co-authored-by: JustSong <[email protected]>
1 parent 59eae18 commit 381172c

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ docker-compose ps
222222
3. 所有从服务器必须设置 `NODE_TYPE``slave`,不设置则默认为主服务器。
223223
4. 设置 `SYNC_FREQUENCY` 后服务器将定期从数据库同步配置,在使用远程数据库的情况下,推荐设置该项并启用 Redis,无论主从。
224224
5. 从服务器可以选择设置 `FRONTEND_BASE_URL`,以重定向页面请求到主服务器。
225-
6. 从服务器上**分别**装好 Redis,设置好 `REDIS_CONN_STRING`,这样可以做到在缓存未过期的情况下数据库零访问,可以减少延迟。
225+
6. 从服务器上**分别**装好 Redis,设置好 `REDIS_CONN_STRING`,这样可以做到在缓存未过期的情况下数据库零访问,可以减少延迟(Redis 集群或者哨兵模式的支持请参考环境变量说明)
226226
7. 如果主服务器访问数据库延迟也比较高,则也需要启用 Redis,并设置 `SYNC_FREQUENCY`,以定期从数据库同步配置。
227227

228228
环境变量的具体使用方法详见[此处](#环境变量)。
@@ -351,6 +351,11 @@ graph LR
351351
1. `REDIS_CONN_STRING`:设置之后将使用 Redis 作为缓存使用。
352352
+ 例子:`REDIS_CONN_STRING=redis://default:redispw@localhost:49153`
353353
+ 如果数据库访问延迟很低,没有必要启用 Redis,启用后反而会出现数据滞后的问题。
354+
+ 如果需要使用哨兵或者集群模式:
355+
+ 则需要把该环境变量设置为节点列表,例如:`localhost:49153,localhost:49154,localhost:49155`
356+
+ 除此之外还需要设置以下环境变量:
357+
+ `REDIS_PASSWORD`:Redis 集群或者哨兵模式下的密码设置。
358+
+ `REDIS_MASTER_NAME`:Redis 哨兵模式下主节点的名称。
354359
2. `SESSION_SECRET`:设置之后将使用固定的会话密钥,这样系统重新启动后已登录用户的 cookie 将依旧有效。
355360
+ 例子:`SESSION_SECRET=random_string`
356361
3. `SQL_DSN`:设置之后将使用指定数据库而非 SQLite,请使用 MySQL 或 PostgreSQL。

common/redis.go

+21-9
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package common
22

33
import (
44
"context"
5-
"github.com/go-redis/redis/v8"
6-
"github.com/songquanpeng/one-api/common/logger"
75
"os"
6+
"strings"
87
"time"
8+
9+
"github.com/go-redis/redis/v8"
10+
"github.com/songquanpeng/one-api/common/logger"
911
)
1012

11-
var RDB *redis.Client
13+
var RDB redis.Cmdable
1214
var RedisEnabled = true
1315

1416
// InitRedisClient This function is called after init()
@@ -23,13 +25,23 @@ func InitRedisClient() (err error) {
2325
logger.SysLog("SYNC_FREQUENCY not set, Redis is disabled")
2426
return nil
2527
}
26-
logger.SysLog("Redis is enabled")
27-
opt, err := redis.ParseURL(os.Getenv("REDIS_CONN_STRING"))
28-
if err != nil {
29-
logger.FatalLog("failed to parse Redis connection string: " + err.Error())
28+
redisConnString := os.Getenv("REDIS_CONN_STRING")
29+
if os.Getenv("REDIS_MASTER_NAME") == "" {
30+
logger.SysLog("Redis is enabled")
31+
opt, err := redis.ParseURL(redisConnString)
32+
if err != nil {
33+
logger.FatalLog("failed to parse Redis connection string: " + err.Error())
34+
}
35+
RDB = redis.NewClient(opt)
36+
} else {
37+
// cluster mode
38+
logger.SysLog("Redis cluster mode enabled")
39+
RDB = redis.NewUniversalClient(&redis.UniversalOptions{
40+
Addrs: strings.Split(redisConnString, ","),
41+
Password: os.Getenv("REDIS_PASSWORD"),
42+
MasterName: os.Getenv("REDIS_MASTER_NAME"),
43+
})
3044
}
31-
RDB = redis.NewClient(opt)
32-
3345
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
3446
defer cancel()
3547

0 commit comments

Comments
 (0)