go-redis cluster 使用demo

package main

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

func main() {
	client := redis.NewClusterClient(&redis.ClusterOptions{
		Addrs:    []string{"redis-cluster.test.com:port"}, //set redis cluster url
		Password: "mypassword",                    //set password 
	})
	
	pong, err := client.Ping().Result()
	fmt.Println(pong, err)
	fmt.Println("pool state init state:", client.PoolStats())
	for i := 0; i < 1000; i++ {
		k := fmt.Sprintf("key:%d", i)
		v := k
		val, err := client.Set(k, v, 60*time.Second).Result()
		if err != nil {
			panic(err)
		}

		val, err = client.Get(k).Result()
		if err != nil {
			panic(err)
		}
		fmt.Println("key:", val)
	}
	fmt.Println("pool state final state:", client.PoolStats()) //获取客户端连接池相关信息

}

 总结:go-redis的连接池无需单独管理,在初始化时会自动根据cpu个数分配,本demo中跑了个循环,所以设置的key会分别打到不同的node上去,连接池会自动增加

posted @ 2018-12-27 15:31  liuyidiao  阅读(5154)  评论(0编辑  收藏  举报