在 redis 集群模式下,即使其中一个注册的 master 关闭,也会执行重定向并且应用程序可以正常工作。
package main
import (
"fmt"
"github.com/go-redis/redis"
"log"
"strconv"
"time"
)
func main() {
c := redis.NewClient(&redis.Options{
Addr: "127.0.0.1:6379",
Password: "",
DB: 0,
})
if err := c.Ping().Err(); err != nil {
panic("Unable to connect to redis " + err.Error())
}
c.FlushDB()
c.FlushAll()
for i := 1; i <= 7; i++ {
key := "key" + strconv.Itoa(i)
ret, err := c.Set(key, "value" + strconv.Itoa(i), 0).Result()
if err != nil || ret != "OK" {
fmt.Println(err)
panic("redis set failed")
}
}
for true {
for i := 1; i <= 7; i++ {
key := "key" + strconv.Itoa(i)
val, err := c.Get(key).Result()
log.Println(key + " : " + val)
if err == redis.Nil {
continue
}
if err != nil {
log.Println(err)
continue
}
}
fmt.Println()
time.Sleep(1 * time.Second)
}
}
在单Redis master-replication配置中,运行上述代码后,如果关闭master Redis,会出现如下错误。
dial tcp 127.0.0.1:6379: connectex: No connection could be made because the target machine actively refused it.
在使用 go-redis 的单个 Redis 主复制配置中,即使主服务器关闭,是否有办法将应用程序连接到复制 redis 而不会出错?