2

In redis cluster mode, even if one of the registered masters shuts down, redirection is performed and the application works without any problem.


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)
    }
}

In a single Redis master-replication configuration, after running the above code, if you shut down master Redis, the following error occurs.

dial tcp 127.0.0.1:6379: connectex: No connection could be made because the target machine actively refused it.

In a single Redis master-replication configuration using go-redis, is there a way to connect the application to replication redis without error even if the master is shut down?

battlecook
  • 471
  • 6
  • 17

0 Answers0