Closed AlvinQinwen closed 2 years ago
import (
"log"
"time"
"github.com/gomodule/redigo/redis"
"github.com/mna/redisc"
)
var RedisClient redis.Conn
func InitRedisConnect() {
// create the cluster
cluster := redisc.Cluster{
StartupNodes: []string{
"172.19.182.6:6379",
"172.19.182.7:6379",
"172.19.182.8:6379",
"172.19.182.9:6379",
"172.19.182.10:6379",
"172.19.182.11:6379",
},
DialOptions: []redis.DialOption{
redis.DialConnectTimeout(5 * time.Second),
redis.DialPassword("Admin,123"),
},
CreatePool: createPool,
}
//defer cluster.Close()
// initialize its mapping
if err := cluster.Refresh(); err != nil {
log.Fatalf("Refresh failed: %v", err)
}
conn := cluster.Get()
rc, err := redisc.RetryConn(conn, 3, 100*time.Millisecond)
if err != nil {
log.Fatalf("RetryConn failed: %v", err)
}
RedisClient = rc
}
// createPool 创建一个连接池
func createPool(addr string, opts ...redis.DialOption) (*redis.Pool, error) {
return &redis.Pool{
MaxIdle: 5,
MaxActive: 10,
IdleTimeout: time.Minute,
Dial: func() (redis.Conn, error) {
return redis.Dial("tcp", addr, opts...)
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
}, nil
}
// GetRedisConn 获取一个新的redis链接句柄 todo 待测试
func GetRedisConn(dbNo int) redis.Conn {
conn := RedisClient
conn.Do("SELECT", dbNo)
return conn
}
please helpe me tell How to change db