登录
首页 >  Golang >  Go问答

使用golang的go-redis pkg连接启用了集群模式的Elasticache for redis

来源:stackoverflow

时间:2024-02-08 23:06:22 310浏览 收藏

哈喽!今天心血来潮给大家带来了《使用golang的go-redis pkg连接启用了集群模式的Elasticache for redis》,想必大家应该对Golang都不陌生吧,那么阅读本文就都不会很困难,以下内容主要涉及到,若是你正在学习Golang,千万别错过这篇文章~希望能帮助到你!

问题内容

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

我不确定使用哪一个来连接到 ElasticCache for Redis。

我想使用仅连接到集群的一个端点的客户端,并且该端点将确保我获取或设置 Redis 的密钥。由于elasticcache中的集群彼此了解

对此的任何帮助将非常感激


正确答案


我想使用 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,
    })

好了,本文到此结束,带大家了解了《使用golang的go-redis pkg连接启用了集群模式的Elasticache for redis》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>