Feat:增加对redis集群模式、哨兵模式的支持 (#965)
* 修复go plugin相关错误 * Feat:增加对redis集群模式、哨兵模式的支持 Co-authored-by: tanxiao <tanxiao@asiainfo.com>
This commit is contained in:
parent
e22a4394f7
commit
7f92e921b4
|
@ -116,13 +116,17 @@ BasicAuthPass = "ibex"
|
||||||
Timeout = 3000
|
Timeout = 3000
|
||||||
|
|
||||||
[Redis]
|
[Redis]
|
||||||
# address, ip:port
|
# address, ip:port or ip1:port,ip2:port for cluster and sentinel(SentinelAddrs)
|
||||||
Address = "127.0.0.1:6379"
|
Address = "127.0.0.1:6379"
|
||||||
# Username = ""
|
# Username = ""
|
||||||
# Password = ""
|
# Password = ""
|
||||||
# DB = 0
|
# DB = 0
|
||||||
# UseTLS = false
|
# UseTLS = false
|
||||||
# TLSMinVersion = "1.2"
|
# TLSMinVersion = "1.2"
|
||||||
|
# standalone cluster sentinel
|
||||||
|
RedisType = "standalone"
|
||||||
|
# Mastername for sentinel type
|
||||||
|
# MasterName = "mymaster"
|
||||||
|
|
||||||
[DB]
|
[DB]
|
||||||
# postgres: host=%s port=%s user=%s dbname=%s password=%s sslmode=%s
|
# postgres: host=%s port=%s user=%s dbname=%s password=%s sslmode=%s
|
||||||
|
|
|
@ -142,13 +142,17 @@ Phone = "phone_number"
|
||||||
Email = "email"
|
Email = "email"
|
||||||
|
|
||||||
[Redis]
|
[Redis]
|
||||||
# address, ip:port
|
# address, ip:port or ip1:port,ip2:port for cluster and sentinel(SentinelAddrs)
|
||||||
Address = "127.0.0.1:6379"
|
Address = "127.0.0.1:6379"
|
||||||
# Username = ""
|
# Username = ""
|
||||||
# Password = ""
|
# Password = ""
|
||||||
# DB = 0
|
# DB = 0
|
||||||
# UseTLS = false
|
# UseTLS = false
|
||||||
# TLSMinVersion = "1.2"
|
# TLSMinVersion = "1.2"
|
||||||
|
# standalone cluster sentinel
|
||||||
|
RedisType = "standalone"
|
||||||
|
# Mastername for sentinel type
|
||||||
|
# MasterName = "mymaster"
|
||||||
|
|
||||||
[DB]
|
[DB]
|
||||||
DSN="root:1234@tcp(127.0.0.1:3306)/n9e_v5?charset=utf8mb4&parseTime=True&loc=Local&allowNativePasswords=true"
|
DSN="root:1234@tcp(127.0.0.1:3306)/n9e_v5?charset=utf8mb4&parseTime=True&loc=Local&allowNativePasswords=true"
|
||||||
|
|
|
@ -4,10 +4,13 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"github.com/go-redis/redis/v8"
|
"strings"
|
||||||
"gorm.io/gorm"
|
"time"
|
||||||
|
|
||||||
"github.com/didi/nightingale/v5/src/pkg/ormx"
|
"github.com/didi/nightingale/v5/src/pkg/ormx"
|
||||||
"github.com/didi/nightingale/v5/src/pkg/tls"
|
"github.com/didi/nightingale/v5/src/pkg/tls"
|
||||||
|
"github.com/go-redis/redis/v8"
|
||||||
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RedisConfig struct {
|
type RedisConfig struct {
|
||||||
|
@ -17,6 +20,8 @@ type RedisConfig struct {
|
||||||
DB int
|
DB int
|
||||||
UseTLS bool
|
UseTLS bool
|
||||||
tls.ClientConfig
|
tls.ClientConfig
|
||||||
|
RedisType string
|
||||||
|
MasterName string
|
||||||
}
|
}
|
||||||
|
|
||||||
var DB *gorm.DB
|
var DB *gorm.DB
|
||||||
|
@ -29,26 +34,81 @@ func InitDB(cfg ormx.DBConfig) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var Redis *redis.Client
|
var Redis interface {
|
||||||
|
Del(ctx context.Context, keys ...string) *redis.IntCmd
|
||||||
|
Get(ctx context.Context, key string) *redis.StringCmd
|
||||||
|
Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.StatusCmd
|
||||||
|
HGetAll(ctx context.Context, key string) *redis.StringStringMapCmd
|
||||||
|
HSet(ctx context.Context, key string, values ...interface{}) *redis.IntCmd
|
||||||
|
HDel(ctx context.Context, key string, fields ...string) *redis.IntCmd
|
||||||
|
Close() error
|
||||||
|
Ping(ctx context.Context) *redis.StatusCmd
|
||||||
|
Publish(ctx context.Context, channel string, message interface{}) *redis.IntCmd
|
||||||
|
}
|
||||||
|
|
||||||
func InitRedis(cfg RedisConfig) (func(), error) {
|
func InitRedis(cfg RedisConfig) (func(), error) {
|
||||||
redisOptions := &redis.Options{
|
switch cfg.RedisType {
|
||||||
Addr: cfg.Address,
|
case "standalone":
|
||||||
Username: cfg.Username,
|
redisOptions := &redis.Options{
|
||||||
Password: cfg.Password,
|
Addr: cfg.Address,
|
||||||
DB: cfg.DB,
|
Username: cfg.Username,
|
||||||
}
|
Password: cfg.Password,
|
||||||
|
DB: cfg.DB,
|
||||||
if cfg.UseTLS {
|
|
||||||
tlsConfig, err := cfg.TLSConfig()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("failed to init redis tls config:", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
redisOptions.TLSConfig = tlsConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
Redis = redis.NewClient(redisOptions)
|
if cfg.UseTLS {
|
||||||
|
tlsConfig, err := cfg.TLSConfig()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("failed to init redis tls config:", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
redisOptions.TLSConfig = tlsConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
Redis = redis.NewClient(redisOptions)
|
||||||
|
|
||||||
|
case "cluster":
|
||||||
|
redisOptions := &redis.ClusterOptions{
|
||||||
|
Addrs: strings.Split(cfg.Address, ","),
|
||||||
|
Username: cfg.Username,
|
||||||
|
Password: cfg.Password,
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.UseTLS {
|
||||||
|
tlsConfig, err := cfg.TLSConfig()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("failed to init redis tls config:", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
redisOptions.TLSConfig = tlsConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
Redis = redis.NewClusterClient(redisOptions)
|
||||||
|
|
||||||
|
case "sentinel":
|
||||||
|
redisOptions := &redis.FailoverOptions{
|
||||||
|
MasterName: cfg.MasterName,
|
||||||
|
SentinelAddrs: strings.Split(cfg.Address, ","),
|
||||||
|
Username: cfg.Username,
|
||||||
|
Password: cfg.Password,
|
||||||
|
DB: cfg.DB,
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.UseTLS {
|
||||||
|
tlsConfig, err := cfg.TLSConfig()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("failed to init redis tls config:", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
redisOptions.TLSConfig = tlsConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
Redis = redis.NewFailoverClient(redisOptions)
|
||||||
|
|
||||||
|
default:
|
||||||
|
fmt.Println("failed to init redis , redis type is illegal:", cfg.RedisType)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
err := Redis.Ping(context.Background()).Err()
|
err := Redis.Ping(context.Background()).Err()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue