From ea8ebb21b74656ed0fdd4a44d9b40afb0adf825a Mon Sep 17 00:00:00 2001 From: sevennt Date: Wed, 8 Nov 2023 11:04:31 +0800 Subject: [PATCH] add redis sentinel password and username --- config.go | 49 +++++++++++++++++++++++++------------------------ container.go | 25 +++++++++++++------------ go.mod | 2 +- go.sum | 6 ------ 4 files changed, 39 insertions(+), 43 deletions(-) diff --git a/config.go b/config.go index e2ac7b8..45ac228 100644 --- a/config.go +++ b/config.go @@ -19,31 +19,32 @@ const ( // config for redis, contains RedisStubConfig, RedisClusterConfig and RedisSentinelConfig type config struct { - Addrs []string // Addrs 实例配置地址 - Addr string // Addr stubConfig 实例配置地址 - Mode string // Mode Redis模式 cluster|stub|sentinel - MasterName string // MasterName 哨兵主节点名称,sentinel模式下需要配置此项 - Password string // Password 密码 - DB int // DB,默认为0, 一般应用不推荐使用DB分片 - PoolSize int // PoolSize 集群内每个节点的最大连接池限制 - MaxRetries int // MaxRetries 网络相关的错误最大重试次数 默认8次 - MinIdleConns int // MinIdleConns 最小空闲连接数 - DialTimeout time.Duration // DialTimeout 拨超时时间 - ReadTimeout time.Duration // ReadTimeout 读超时 默认3s - WriteTimeout time.Duration // WriteTimeout 读超时 默认3s - IdleTimeout time.Duration // IdleTimeout 连接最大空闲时间,默认60s, 超过该时间,连接会被主动关闭 - Debug bool // Debug开关, 是否开启调试,默认不开启,开启后并加上export EGO_DEBUG=true,可以看到每次请求,配置名、地址、耗时、请求数据、响应数据 - ReadOnly bool // ReadOnly 集群模式 在从属节点上启用读模式 - SlowLogThreshold time.Duration // 慢日志门限值,超过该门限值的请求,将被记录到慢日志中 - OnFail string // OnFail panic|error - EnableMetricInterceptor bool // 是否开启监控,默认开启 - EnableTraceInterceptor bool // 是否开启链路,默认开启 - EnableAccessInterceptor bool // 是否开启,记录请求数据 - EnableAccessInterceptorReq bool // 是否开启记录请求参数 - EnableAccessInterceptorRes bool // 是否开启记录响应参数 + Addrs []string // Addrs cluster|sentinel 模式下实例配置地址 + Addr string // Addr stub 模式下实例配置地址 + Mode string // Mode Redis模式 cluster|stub|sentinel + MasterName string // MasterName 哨兵主节点名称,sentinel模式下需要配置此项 + SentinelUsername string // SentinelUsername sentinel 模式下用户密码 + SentinelPassword string // SentinelPassword sentinel 模式下密码 + Password string // Password cluster|stub 模式下密码 + DB int // DB,默认为0, 一般应用不推荐使用DB分片 + PoolSize int // PoolSize 集群内每个节点的最大连接池限制 + MaxRetries int // MaxRetries 网络相关的错误最大重试次数 默认8次 + MinIdleConns int // MinIdleConns 最小空闲连接数 + DialTimeout time.Duration // DialTimeout 拨超时时间 + ReadTimeout time.Duration // ReadTimeout 读超时 默认3s + WriteTimeout time.Duration // WriteTimeout 读超时 默认3s + IdleTimeout time.Duration // IdleTimeout 连接最大空闲时间,默认60s, 超过该时间,连接会被主动关闭 + Debug bool // Debug 开关, 是否开启调试,默认不开启,开启后并加上export EGO_DEBUG=true,可以看到每次请求,配置名、地址、耗时、请求数据、响应数据 + ReadOnly bool // ReadOnly 集群模式 在从属节点上启用读模式 + SlowLogThreshold time.Duration // SlowLogThreshold 慢日志门限值,超过该门限值的请求,将被记录到慢日志中 + OnFail string // OnFail panic|error + EnableMetricInterceptor bool // EnableMetricInterceptor 是否开启监控,默认开启 + EnableTraceInterceptor bool // EnableTraceInterceptor 是否开启链路,默认开启 + EnableAccessInterceptor bool // EnableAccessInterceptor 是否开启,记录请求数据 + EnableAccessInterceptorReq bool // EnableAccessInterceptorReq 是否开启记录请求参数 + EnableAccessInterceptorRes bool // EnableAccessInterceptorRes 是否开启记录响应参数 + Authentication Authentication // Authentication TLS 参数支持 interceptors []redis.Hook - // TLS 参数支持 - Authentication Authentication } // DefaultConfig default config ... diff --git a/container.go b/container.go index 8de0089..1248959 100644 --- a/container.go +++ b/container.go @@ -140,18 +140,19 @@ func (c *Container) buildCluster() *redis.ClusterClient { func (c *Container) buildSentinel() *redis.Client { sentinelClient := redis.NewFailoverClient(&redis.FailoverOptions{ - MasterName: c.config.MasterName, - SentinelAddrs: c.config.Addrs, - Password: c.config.Password, - DB: c.config.DB, - MaxRetries: c.config.MaxRetries, - DialTimeout: c.config.DialTimeout, - ReadTimeout: c.config.ReadTimeout, - WriteTimeout: c.config.WriteTimeout, - PoolSize: c.config.PoolSize, - MinIdleConns: c.config.MinIdleConns, - IdleTimeout: c.config.IdleTimeout, - TLSConfig: c.config.Authentication.TLSConfig(), + MasterName: c.config.MasterName, + SentinelAddrs: c.config.Addrs, + SentinelPassword: c.config.SentinelPassword, + Password: c.config.Password, + DB: c.config.DB, + MaxRetries: c.config.MaxRetries, + DialTimeout: c.config.DialTimeout, + ReadTimeout: c.config.ReadTimeout, + WriteTimeout: c.config.WriteTimeout, + PoolSize: c.config.PoolSize, + MinIdleConns: c.config.MinIdleConns, + IdleTimeout: c.config.IdleTimeout, + TLSConfig: c.config.Authentication.TLSConfig(), }) for _, incpt := range c.config.interceptors { diff --git a/go.mod b/go.mod index 5187c8a..00c62ad 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/go-redis/redis/extra/rediscmd/v8 v8.11.4 github.com/go-redis/redis/v8 v8.11.4 github.com/gotomicro/ego v1.0.3 + github.com/json-iterator/go v1.1.12 github.com/spf13/cast v1.3.1 github.com/stretchr/testify v1.7.0 go.opentelemetry.io/otel v1.4.1 @@ -29,7 +30,6 @@ require ( github.com/google/pprof v0.0.0-20200708004538-1a94d8640e99 // indirect github.com/google/uuid v1.1.2 // indirect github.com/gotomicro/logrotate v0.0.0-20211108024517-45d1f9a03ff5 // indirect - github.com/json-iterator/go v1.1.12 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect github.com/mitchellh/mapstructure v1.3.2 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect diff --git a/go.sum b/go.sum index 68569ff..43a0906 100644 --- a/go.sum +++ b/go.sum @@ -234,12 +234,6 @@ github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2z github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= -github.com/gotomicro/ego v1.0.0 h1:2v7im+ovFMyZDGCG90+fQb8wXySA9fbZGHw3VnCActU= -github.com/gotomicro/ego v1.0.0/go.mod h1:Pjt+5kIVGMqzpKYMxeN4s/Dc2TrNO9R58yQwt9zoXck= -github.com/gotomicro/ego v1.0.1 h1:TId194gdnP43RNwh4xBU/MC8Tc6R8nJ5v3BZtf6movE= -github.com/gotomicro/ego v1.0.1/go.mod h1:Pjt+5kIVGMqzpKYMxeN4s/Dc2TrNO9R58yQwt9zoXck= -github.com/gotomicro/ego v1.0.2 h1:ScGEaP1MVT4w6kPa8AB8sqRdDiCWN4p1g7AwBCzrU1s= -github.com/gotomicro/ego v1.0.2/go.mod h1:Pjt+5kIVGMqzpKYMxeN4s/Dc2TrNO9R58yQwt9zoXck= github.com/gotomicro/ego v1.0.3 h1:wSuS/AI3HV8r5Gro5oG2qlCAfQ/skDDs6GXedEqJRj4= github.com/gotomicro/ego v1.0.3/go.mod h1:Pjt+5kIVGMqzpKYMxeN4s/Dc2TrNO9R58yQwt9zoXck= github.com/gotomicro/logrotate v0.0.0-20211108024517-45d1f9a03ff5 h1:y9nw0S0zlla/SBt1GGaTNNCF+781epJ62MntVVbekqQ=