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