I want to connect to Elasticcache for redis in which cluster mode is enabled. I want to connect it from golang (go-redis pkg)

huangapple go评论81阅读模式
英文:

I want to connect to Elasticcache for redis in which cluster mode is enabled. I want to connect it from golang (go-redis pkg)

问题

在Golang中,Go Redis有两个客户端,redis.NewClient和redis.NewClusterClient。

我不确定在连接到Elasticache for Redis时应该使用哪个。

我想使用只连接到集群的一个端点的客户端,并且该端点将确保我获取或设置Redis的键。因为Elasticache中的集群彼此感知。

非常感谢您对此的任何帮助。

英文:

In Golang,Go redis has 2 clients, redis.NewClient and redis.NewClusterClient.

I am not sure which one to use for connecting to elasticcache for redis.

I want to use the client which will only connect to one endoint of cluster and that enpoint will make sure I get or set the keys for redis. As clusters in elasticcache are aware of eachother

Any help on this would be very much appreciated

答案1

得分: 2

我想使用NewClusterClient,针对你的问题

> 我想使用客户端只连接到集群的一个端点,该端点将确保我获取或设置Redis的键

请确保所有这些参数都为false

        ReadOnly:       false,
        RouteRandomly:  false,
        RouteByLatency: false,

示例代码

import (
  goredis "github.com/go-redis/redis/v8"
)

goredis.NewClusterClient(&goredis.ClusterOptions{
		Addrs:        []string{"cluster-configuration-endpoint:6379"},
		Password:     "password",
		PoolSize:     10, 
		MinIdleConns: 10,

		DialTimeout:  5 * time.Second,
		ReadTimeout:  3 * time.Second,
		WriteTimeout: 3 * time.Second,
		PoolTimeout:  4 * time.Second,

		IdleCheckFrequency: 60 * time.Second,
		IdleTimeout:        5 * time.Minute,
		MaxConnAge:         0 * time.Second,

		MaxRetries:      10,
		MinRetryBackoff: 8 * time.Millisecond,
		MaxRetryBackoff: 512 * time.Millisecond,

		TLSConfig: &tls.Config{
			InsecureSkipVerify: true,
		},

		ReadOnly:       false,
		RouteRandomly:  false,
		RouteByLatency: false,
	})
英文:

I would like to use NewClusterClient, for your question

> I want to use the client which will only connect to one endoint of cluster and that enpoint will make sure I get or set the keys for redis

Please make sure all those parameters are false

        ReadOnly:       false,
        RouteRandomly:  false,
        RouteByLatency: false,

Sample codes

import (
  goredis "github.com/go-redis/redis/v8"
)

goredis.NewClusterClient(&goredis.ClusterOptions{
		Addrs:        []string{"cluster-configuration-endpoint:6379"},
		Password:     "password",
		PoolSize:     10, 
		MinIdleConns: 10,

		DialTimeout:  5 * time.Second,
		ReadTimeout:  3 * time.Second,
		WriteTimeout: 3 * time.Second,
		PoolTimeout:  4 * time.Second,

		IdleCheckFrequency: 60 * time.Second,
		IdleTimeout:        5 * time.Minute,
		MaxConnAge:         0 * time.Second,

		MaxRetries:      10,
		MinRetryBackoff: 8 * time.Millisecond,
		MaxRetryBackoff: 512 * time.Millisecond,

		TLSConfig: &tls.Config{
			InsecureSkipVerify: true,
		},

		ReadOnly:       false,
		RouteRandomly:  false,
		RouteByLatency: false,
	})

huangapple
  • 本文由 发表于 2022年9月30日 18:25:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/73907312.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定