add sentinel password
This commit is contained in:
parent
ee6659ccb1
commit
4089e4d6b7
@ -15,15 +15,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Debug bool `toml:"debug"` //调试开关(会在日志打印REDIS语句)
|
Debug bool `toml:"debug"` //调试开关(会在日志打印REDIS语句)
|
||||||
Username string `toml:"username"` //REDIS连接用户名
|
MasterName string `toml:"mastername"` //REDIS主名称(一个哨兵集群可管理多个REDIS主从结构)
|
||||||
Password string `toml:"password"` //REDIS连接用户密码
|
Username string `toml:"username"` //REDIS连接用户名
|
||||||
MasterName string `toml:"mastername"` //REDIS主名称(一个哨兵集群可管理多个REDIS主从结构)
|
Password string `toml:"password"` //REDIS连接用户密码
|
||||||
Sentinels string `toml:"sentinels"` //哨兵节点列表,逗号分隔,一般配置三个
|
Sentinels string `toml:"sentinels"` //哨兵节点列表,逗号分隔,一般配置三个
|
||||||
Wait *bool `toml:"wait"`
|
SentinelUsername string `toml:"sentinel_username"` //哨兵节点连接用户名,不填默认default
|
||||||
MaxIdle *int `toml:"max_idle"`
|
SentinelPassword string `toml:"sentinel_password"` //哨兵节点连接密码,不填认为集群无密码
|
||||||
MaxActive *int `toml:"max_active"`
|
Wait *bool `toml:"wait"` //当无可用连接时,是否等待
|
||||||
IdleTimeout *int `toml:"idle_timeout"`
|
MaxIdle *int `toml:"max_idle"` //最大空闲连接数
|
||||||
|
MaxActive *int `toml:"max_active"` //最大活跃连接数
|
||||||
|
IdleTimeout *int `toml:"idle_timeout"` //空闲超时时间
|
||||||
}
|
}
|
||||||
|
|
||||||
type Sentinel struct {
|
type Sentinel struct {
|
||||||
@ -52,9 +54,12 @@ type Sentinel struct {
|
|||||||
|
|
||||||
func NewPool(cfg Config) *redis.Pool {
|
func NewPool(cfg Config) *redis.Pool {
|
||||||
var (
|
var (
|
||||||
masterName = cfg.MasterName
|
masterName = cfg.MasterName
|
||||||
masterPassword = cfg.Password
|
masterUsername = cfg.Username
|
||||||
sntnl = &Sentinel{
|
masterPassword = cfg.Password
|
||||||
|
sentinelUsername = cfg.SentinelUsername
|
||||||
|
sentinelPassword = cfg.SentinelPassword
|
||||||
|
sntnl = &Sentinel{
|
||||||
Addrs: strings.Split(cfg.Sentinels, ","),
|
Addrs: strings.Split(cfg.Sentinels, ","),
|
||||||
MasterName: masterName,
|
MasterName: masterName,
|
||||||
Dial: func(addr string) (redis.Conn, error) {
|
Dial: func(addr string) (redis.Conn, error) {
|
||||||
@ -63,6 +68,18 @@ func NewPool(cfg Config) *redis.Pool {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
var okstr = "OK"
|
||||||
|
if sentinelPassword != "" && sentinelUsername != "" {
|
||||||
|
okstr, err = redis.String(c.Do("AUTH", sentinelUsername, sentinelPassword))
|
||||||
|
} else if sentinelPassword != "" {
|
||||||
|
okstr, err = redis.String(c.Do("AUTH", sentinelPassword))
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("redis sentinel AUTH failed: <%s>", err.Error())
|
||||||
|
} else if okstr != "OK" {
|
||||||
|
return nil, fmt.Errorf("redis sentinel AUTH failed: <%s>", okstr)
|
||||||
|
}
|
||||||
return c, nil
|
return c, nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -116,7 +133,13 @@ func NewPool(cfg Config) *redis.Pool {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
okstr, err := redis.String(c.Do("AUTH", masterPassword))
|
var okstr = "OK"
|
||||||
|
if masterPassword != "" && masterUsername != "" {
|
||||||
|
okstr, err = redis.String(c.Do("AUTH", masterUsername, masterPassword))
|
||||||
|
} else if masterPassword != "" {
|
||||||
|
okstr, err = redis.String(c.Do("AUTH", masterPassword))
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("redis master AUTH failed: <%s>", err.Error())
|
return nil, fmt.Errorf("redis master AUTH failed: <%s>", err.Error())
|
||||||
} else if okstr != "OK" {
|
} else if okstr != "OK" {
|
||||||
@ -125,7 +148,10 @@ func NewPool(cfg Config) *redis.Pool {
|
|||||||
|
|
||||||
if !TestRole(c, "master") {
|
if !TestRole(c, "master") {
|
||||||
c.Close()
|
c.Close()
|
||||||
return nil, fmt.Errorf("%s is not redis master", masterAddr)
|
err = fmt.Errorf(
|
||||||
|
"master(%s) got by name '%s' is not redis master",
|
||||||
|
masterAddr, masterName)
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.Debug {
|
if cfg.Debug {
|
||||||
|
Loading…
Reference in New Issue
Block a user