redigo的redis.Pool 配置参数调优

本文介绍了redigo的redis.Pool结构,包括连接池获取连接、关闭连接的方法,并详细探讨了MaxIdle、MaxActive和IdleTimeout三个关键配置参数的调优策略。针对高频和低频调用的不同场景,提出了不同的配置建议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

reids.Pool结构介绍

// github.com/garyburd/redigo/redis/pool.go
type Pool struct {

    // Dial()方法返回一个连接,从在需要创建连接到的时候调用
    Dial func() (Conn, error)

    // TestOnBorrow()方法是一个可选项,该方法用来诊断一个连接的健康状态
    TestOnBorrow func(c Conn, t time.Time) error

    // 最大空闲连接数
    MaxIdle int

    // 一个pool所能分配的最大的连接数目
    // 当设置成0的时候,该pool连接数没有限制
    MaxActive int

    // 空闲连接超时时间,超过超时时间的空闲连接会被关闭。
    // 如果设置成0,空闲连接将不会被关闭
    // 应该设置一个比redis服务端超时时间更短的时间
    IdleTimeout time.Duration

    // 如果Wait被设置成true,则Get()方法将会阻塞
    Wait bool

    // mu protects fields defined below.
    mu     sync.Mutex
    cond   *sync.Cond
    closed bool
    active int

    // 空闲连接队列
    idle list.List
}

从连接池中获取连接

// get prunes stale connections and returns a connection from the idle list or
// creates a new connection.
func (p *Pool) get() (Conn, error) {
    p.mu.Lock()

    //修剪i
#Matser的ip地址 #redis.host=127.0.0.1 ##端口号 #redis.port=6379 #如果有密码 #redis.password=123456 # 数据库索引 redis.database=0 #客户端超时时间单位是毫秒 默认是2000 redis.timeout=10000 #最大空闲数 redis.pool.maxIdle=300 #连接池的最大数据库连接数。设为0表示无限制,如果是jedis 2.4以后用redis.maxTotal #redis.pool.maxActive=600 #控制一个pool可分配多少个jedis实例,用来替换上面的redis.maxActive,如果是jedis 2.4以后用该属性 redis.pool.maxTotal=1000 #最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。 redis.pool.maxWaitMillis=1000 #连接的最小空闲时间 默认1800000毫秒(30分钟) redis.pool.minEvictableIdleTimeMillis=300000 #每次释放连接的最大数目,默认3 redis.pool.numTestsPerEvictionRun=1024 #逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1 redis.pool.timeBetweenEvictionRunsMillis=30000 #是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个 redis.pool.testOnBorrow=true #在空闲时检查有效性, 默认false redis.pool.testWhileIdle=true #redis集群配置 # spring.redis.spool.cluster.nodes=192.168.177.128:7001,192.168.177.128:7002,192.168.177.128:7003,192.168.177.128:7004,192.168.177.128:7005,192.168.177.128:7006 # spring.redis.pool.cluster.max-redirects=3 #单机版本模式(顺丰redis工具暂不支持绝对单机版本) #redis.master=mymaster #redis.sentinel.nodes=127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381 #redis.connection.password= #使用公司的链接方式当 master = 时候,使用单机模式 哨兵模式 redis.master=ESG_ERS_CORE_Dr0W352j_CLUSTER01 redis.sentinel.nodes=Dr0W352j-1.cachesit.sfcloud.local:8001,Dr0W352j-2.cachesit.sfcloud.local:8001,Dr0W352j-3.cachesit.sfcloud.local:8001 redis.connection.password=XfpPitLd08Fr redis.lettuce.masters=ESG_ERS_CORE_Dr0W352j_CLUSTER01 redis.lettuce.servers=Dr0W352j-1.cachesit.sfcloud.local:8001,Dr0W352j-2.cachesit.sfcloud.local:8001,Dr0W352j-3.cachesit.sfcloud.local:8001 redis.lettuce.type=SENTINEL redis.lettuce.password=XfpPitLd08Fr redis.lettuce.ttl=86400 redis.lettuce.connectTimeout=60 redis.lettuce.cacheName=ERS 我应该怎么配置 连上redis哨兵模式
03-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值