wuxibin89 / redis-go-cluster

redis cluster client implementation in Go
Apache License 2.0
488 stars 145 forks source link

I can't connect Redis cluster #35

Open soulshuangchen opened 4 years ago

soulshuangchen commented 4 years ago

I use this code to create cluster, but it has a problem:

cluster, err := clust.NewCluster(
        &clust.Options{
            StartNodes:   []string{"10.151.3.24:6379", "10.151.3.24:6383", "10.151.3.24:6381"},
            ConnTimeout:  5 * time.Second,
            ReadTimeout:  5 * time.Second,
            WriteTimeout: 5 * time.Second,
            KeepAlive:    16,
            AliveTime:    60 * time.Second,
        })

NewCluster: no valid node in [10.151.3.24:6379 10.151.3.24:6383 10.151.3.24:6381]

but when I use "github.com/gomodule/redigo/redis" to create Redis.conn it was worked. this is codes:

var RedisPool *redis.Pool
var ip = "10.151.3.24:6381"
func NewRedisPool(ip string) *redis.Pool {
    return &redis.Pool{
        MaxIdle:     6,
        IdleTimeout: 240 * time.Second,
        Dial: func() (redis.Conn, error) {
            c, err := redis.Dial("tcp", ip)
            if err != nil {
                return nil, err
            }
            return c, nil
        },
        TestOnBorrow: func(c redis.Conn, t time.Time) error {
            if time.Since(t) < time.Minute {
                return nil
            }
            _, err := c.Do("PING")
            return err
        },
    }
}
RedisPool = NewRedisPool(ip)
redisConn := RedisPool.Get()
defer redisConn.Close()

redisConn.Do("AUTH", "123456")
reply, err := redisConn.Do("set", "demo_key", "666")

I don't know what cause this problem. What Redis cluster need? I use Virtualmachine+Docker to create Redis cluster.What should I do to handle this problem. Please help me.Thanks

LicaSterian commented 4 years ago

I get the same error: NewCluster: no valid node in [localhost:6379] Also tried connecting to redis with github.com/garyburd/redigo/redis and it works just fine, Dial, PING, SET, GET commands are working just fine. I'm just unable to get it working with this repo.

sxpujs commented 4 years ago

Redis version: 5.0.7

I got the similar error:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x10ef11a]

goroutine 1 [running]:
github.com/chasex/redis-go-cluster.(*redisConn).shutdown(...)
        /Users/hao/codes/go/pkg/mod/github.com/chasex/redis-go-cluster@v1.0.0/node.go:146
github.com/chasex/redis-go-cluster.(*redisNode).do(0xc0000ad200, 0x113f0d1, 0x3, 0xc00000e1c0, 0x2, 0x2, 0x20, 0x20, 0xc00000e1c0, 0xc0000b5e80)
        /Users/hao/codes/go/pkg/mod/github.com/chasex/redis-go-cluster@v1.0.0/node.go:192 +0x23a
github.com/chasex/redis-go-cluster.(*redisCluster).Do(0xc0000aa000, 0x113f0d1, 0x3, 0xc00000e1c0, 0x2, 0x2, 0xc000053f90, 0xc00009a000, 0xc000053ef0, 0x10ee196)
        /Users/hao/codes/go/pkg/mod/github.com/chasex/redis-go-cluster@v1.0.0/cluster.go:254 +0x20f
main.main()
        /Users/hao/codes/go/src/github.com/sxpujs/go-example/redis/redis_cluster.go:24 +0x211

Process finished with exit code 2

source code:

package main

import (
    "fmt"
    "github.com/chasex/redis-go-cluster"
    "os"
    "time"
)

func main() {
    cluster, err := redis.NewCluster(
        &redis.Options{
            StartNodes: []string{"127.0.0.1:30001", "127.0.0.1:30002", "127.0.0.1:30003"},
            ConnTimeout: 500 * time.Millisecond,
            ReadTimeout: 500 * time.Millisecond,
            WriteTimeout: 500 * time.Millisecond,
            KeepAlive: 16,
            AliveTime: 60 * time.Second,
        })
    if err != nil {
        fmt.Println("err=", err)
        os.Exit(1)
    }
    cluster.Do("SET", "foo", "bar")
    cluster.Do("INCR", "mycount", 1)
    cluster.Do("LPUSH", "mylist", "foo", "bar")
    cluster.Do("HMSET", "myhash", "f1", "foo", "f2", "bar")
}

redis-server cluster is running normally.

> c ps -ef|grep redis
  501 40677     1   0 10:38下午 ??         0:28.61 ../../src/redis-server *:30001 [cluster]
  501 40679     1   0 10:38下午 ??         0:28.79 ../../src/redis-server *:30002 [cluster]
  501 40681     1   0 10:38下午 ??         0:28.72 ../../src/redis-server *:30003 [cluster]
  501 40683     1   0 10:38下午 ??         0:28.44 ../../src/redis-server *:30004 [cluster]
  501 40685     1   0 10:38下午 ??         0:28.47 ../../src/redis-server *:30005 [cluster]
  501 40687     1   0 10:38下午 ??         0:28.26 ../../src/redis-server *:30006 [cluster]
liqing455 commented 3 years ago

this problem caused by errcode "ECONNTIMEOUT", you should close cluster conn after use cluster defer cluster.Close()