分享
Golang的Redis客户端,支持单机,哨兵,集群
letsfire · · 1795 次点击 · · 开始浏览这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
基于 gomodule/redigo 的二次封装,提供 stand-alone sentinel cluster 3种部署模式下的统一接口,使得更换 redis 部署模式对业务透明
项目地址
码云: https://gitee.com/letsfire/re...
Github: https://github.com/letsfire/r...
开发进度
| Mode部署模式 | 代码完成度 | 测试完成度 | 依赖包 |
|---|---|---|---|
| alone 单例,Twemproxy,Codis | 100% | 100% | |
| sentinel 哨兵模式 | 100% | 100% | FZambia/sentinel |
| cluster 集群模式 | 100% | 100% | mna/redisc |
方法列表
订阅连接 type SubFunc func(c redis.PubSubConn) (err error)
普通连接 type ExecFunc func(c redis.Conn) (res interface{}, err error)
- Sub(fn SubFunc) (err error)
- Exec(fn ExecFunc) (interface{}, error)
- Int(fn ExecFunc) (int, error)
- Ints(fn ExecFunc) ([]int, error)
- IntMap(fn ExecFunc) (map[string]int, error)
- Int64(fn ExecFunc) (int64, error)
- Int64s(fn ExecFunc) ([]int64, error)
- Int64Map(fn ExecFunc) (map[string]int64, error)
- Uint64(fn ExecFunc) (uint64, error)
- Bool(fn ExecFunc) (bool, error)
- String(fn ExecFunc) (string, error)
- StringMap(fn ExecFunc) (map[string]string, error)
- Strings(fn ExecFunc) ([]string, error)
- Bytes(fn ExecFunc) ([]byte, error)
- ByteSlices(fn ExecFunc) ([][]byte, error)
- Positions(fn ExecFunc) ([]*[2]float64, error)
- Float64(fn ExecFunc) (float64, error)
- Float64s(fn ExecFunc) ([]float64, error)
- Values(fn ExecFunc) ([]interface{}, error)
Alone示例
var echoStr = "hello world"
var aloneMode = alone.New(
alone.Addr("192.168.0.110:6379"),
alone.PoolOpts(
mode.MaxActive(0), // 最大连接数,默认0无限制
mode.MaxIdle(0), // 最多保持空闲连接数,默认2*runtime.GOMAXPROCS(0)
mode.Wait(false), // 连接耗尽时是否等待,默认false
mode.IdleTimeout(0), // 空闲连接超时时间,默认0不超时
mode.MaxConnLifetime(0), // 连接的生命周期,默认0不失效
mode.TestOnBorrow(nil), // 空间连接取出后检测是否健康,默认nil
),
alone.DialOpts(
redis.DialReadTimeout(time.Second), // 读取超时,默认time.Second
redis.DialWriteTimeout(time.Second), // 写入超时,默认time.Second
redis.DialConnectTimeout(time.Second), // 连接超时,默认500*time.Millisecond
redis.DialPassword(""), // 鉴权密码,默认空
redis.DialDatabase(0), // 数据库号,默认0
redis.DialKeepAlive(time.Minute*5), // 默认5*time.Minute
redis.DialNetDial(nil), // 自定义dial,默认nil
redis.DialUseTLS(false), // 是否用TLS,默认false
redis.DialTLSSkipVerify(false), // 服务器证书校验,默认false
redis.DialTLSConfig(nil), // 默认nil,详见tls.Config
),
)
var instance = redigo.New(aloneMode)
res, err := instance.String(func(c redis.Conn) (res interface{}, err error) {
return c.Do("ECHO", echoStr)
})
if err != nil {
log.Fatal(err)
} else if res != echoStr {
log.Fatalf("unexpected result, expect = %s, but = %s", echoStr, res)
}
Sentinel示例
var echoStr = "hello world"
var sentinelMode = sentinel.New(
sentinel.Addrs([]string{"192.168.0.110:26379"}),
sentinel.PoolOpts(
mode.MaxActive(0), // 最大连接数,默认0无限制
mode.MaxIdle(0), // 最多保持空闲连接数,默认2*runtime.GOMAXPROCS(0)
mode.Wait(false), // 连接耗尽时是否等待,默认false
mode.IdleTimeout(0), // 空闲连接超时时间,默认0不超时
mode.MaxConnLifetime(0), // 连接的生命周期,默认0不失效
mode.TestOnBorrow(nil), // 空间连接取出后检测是否健康,默认nil
),
sentinel.DialOpts(
redis.DialReadTimeout(time.Second), // 读取超时,默认time.Second
redis.DialWriteTimeout(time.Second), // 写入超时,默认time.Second
redis.DialConnectTimeout(time.Second), // 连接超时,默认500*time.Millisecond
redis.DialPassword(""), // 鉴权密码,默认空
redis.DialDatabase(0), // 数据库号,默认0
redis.DialKeepAlive(time.Minute*5), // 默认5*time.Minute
redis.DialNetDial(nil), // 自定义dial,默认nil
redis.DialUseTLS(false), // 是否用TLS,默认false
redis.DialTLSSkipVerify(false), // 服务器证书校验,默认false
redis.DialTLSConfig(nil), // 默认nil,详见tls.Config
),
// 连接哨兵配置,用法于sentinel.DialOpts()一致
// 默认未配置的情况则直接使用sentinel.DialOpts()的配置
// sentinel.SentinelDialOpts()
)
var instance = redigo.New(sentinelMode)
res, err := instance.String(func(c redis.Conn) (res interface{}, err error) {
return c.Do("ECHO", echoStr)
})
if err != nil {
log.Fatal(err)
} else if res != echoStr {
log.Fatalf("unexpected result, expect = %s, but = %s", echoStr, res)
}
Cluster示例
var echoStr = "hello world"
var clusterMode = cluster.New(
cluster.Nodes([]string{
"192.168.0.110:30001", "192.168.0.110:30002", "192.168.0.110:30003",
"192.168.0.110:30004", "192.168.0.110:30005", "192.168.0.110:30006",
}),
cluster.PoolOpts(
mode.MaxActive(0), // 最大连接数,默认0无限制
mode.MaxIdle(0), // 最多保持空闲连接数,默认2*runtime.GOMAXPROCS(0)
mode.Wait(false), // 连接耗尽时是否等待,默认false
mode.IdleTimeout(0), // 空闲连接超时时间,默认0不超时
mode.MaxConnLifetime(0), // 连接的生命周期,默认0不失效
mode.TestOnBorrow(nil), // 空间连接取出后检测是否健康,默认nil
),
cluster.DialOpts(
redis.DialReadTimeout(time.Second), // 读取超时,默认time.Second
redis.DialWriteTimeout(time.Second), // 写入超时,默认time.Second
redis.DialConnectTimeout(time.Second), // 连接超时,默认500*time.Millisecond
redis.DialPassword(""), // 鉴权密码,默认空
redis.DialDatabase(0), // 数据库号,默认0
redis.DialKeepAlive(time.Minute*5), // 默认5*time.Minute
redis.DialNetDial(nil), // 自定义dial,默认nil
redis.DialUseTLS(false), // 是否用TLS,默认false
redis.DialTLSSkipVerify(false), // 服务器证书校验,默认false
redis.DialTLSConfig(nil), // 默认nil,详见tls.Config
),
)
var instance = redigo.New(clusterMode)
res, err := instance.String(func(c redis.Conn) (res interface{}, err error) {
return c.Do("ECHO", echoStr)
})
if err != nil {
log.Fatal(err)
} else if res != echoStr {
log.Fatalf("unexpected result, expect = %s, but = %s", echoStr, res)
}
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信1795 次点击
添加一条新回复
(您需要 后才能回复 没有账号 ?)
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
基于 gomodule/redigo 的二次封装,提供 stand-alone sentinel cluster 3种部署模式下的统一接口,使得更换 redis 部署模式对业务透明
项目地址
码云: https://gitee.com/letsfire/re...
Github: https://github.com/letsfire/r...
开发进度
| Mode部署模式 | 代码完成度 | 测试完成度 | 依赖包 |
|---|---|---|---|
| alone 单例,Twemproxy,Codis | 100% | 100% | |
| sentinel 哨兵模式 | 100% | 100% | FZambia/sentinel |
| cluster 集群模式 | 100% | 100% | mna/redisc |
方法列表
订阅连接 type SubFunc func(c redis.PubSubConn) (err error)
普通连接 type ExecFunc func(c redis.Conn) (res interface{}, err error)
- Sub(fn SubFunc) (err error)
- Exec(fn ExecFunc) (interface{}, error)
- Int(fn ExecFunc) (int, error)
- Ints(fn ExecFunc) ([]int, error)
- IntMap(fn ExecFunc) (map[string]int, error)
- Int64(fn ExecFunc) (int64, error)
- Int64s(fn ExecFunc) ([]int64, error)
- Int64Map(fn ExecFunc) (map[string]int64, error)
- Uint64(fn ExecFunc) (uint64, error)
- Bool(fn ExecFunc) (bool, error)
- String(fn ExecFunc) (string, error)
- StringMap(fn ExecFunc) (map[string]string, error)
- Strings(fn ExecFunc) ([]string, error)
- Bytes(fn ExecFunc) ([]byte, error)
- ByteSlices(fn ExecFunc) ([][]byte, error)
- Positions(fn ExecFunc) ([]*[2]float64, error)
- Float64(fn ExecFunc) (float64, error)
- Float64s(fn ExecFunc) ([]float64, error)
- Values(fn ExecFunc) ([]interface{}, error)
Alone示例
var echoStr = "hello world"
var aloneMode = alone.New(
alone.Addr("192.168.0.110:6379"),
alone.PoolOpts(
mode.MaxActive(0), // 最大连接数,默认0无限制
mode.MaxIdle(0), // 最多保持空闲连接数,默认2*runtime.GOMAXPROCS(0)
mode.Wait(false), // 连接耗尽时是否等待,默认false
mode.IdleTimeout(0), // 空闲连接超时时间,默认0不超时
mode.MaxConnLifetime(0), // 连接的生命周期,默认0不失效
mode.TestOnBorrow(nil), // 空间连接取出后检测是否健康,默认nil
),
alone.DialOpts(
redis.DialReadTimeout(time.Second), // 读取超时,默认time.Second
redis.DialWriteTimeout(time.Second), // 写入超时,默认time.Second
redis.DialConnectTimeout(time.Second), // 连接超时,默认500*time.Millisecond
redis.DialPassword(""), // 鉴权密码,默认空
redis.DialDatabase(0), // 数据库号,默认0
redis.DialKeepAlive(time.Minute*5), // 默认5*time.Minute
redis.DialNetDial(nil), // 自定义dial,默认nil
redis.DialUseTLS(false), // 是否用TLS,默认false
redis.DialTLSSkipVerify(false), // 服务器证书校验,默认false
redis.DialTLSConfig(nil), // 默认nil,详见tls.Config
),
)
var instance = redigo.New(aloneMode)
res, err := instance.String(func(c redis.Conn) (res interface{}, err error) {
return c.Do("ECHO", echoStr)
})
if err != nil {
log.Fatal(err)
} else if res != echoStr {
log.Fatalf("unexpected result, expect = %s, but = %s", echoStr, res)
}
Sentinel示例
var echoStr = "hello world"
var sentinelMode = sentinel.New(
sentinel.Addrs([]string{"192.168.0.110:26379"}),
sentinel.PoolOpts(
mode.MaxActive(0), // 最大连接数,默认0无限制
mode.MaxIdle(0), // 最多保持空闲连接数,默认2*runtime.GOMAXPROCS(0)
mode.Wait(false), // 连接耗尽时是否等待,默认false
mode.IdleTimeout(0), // 空闲连接超时时间,默认0不超时
mode.MaxConnLifetime(0), // 连接的生命周期,默认0不失效
mode.TestOnBorrow(nil), // 空间连接取出后检测是否健康,默认nil
),
sentinel.DialOpts(
redis.DialReadTimeout(time.Second), // 读取超时,默认time.Second
redis.DialWriteTimeout(time.Second), // 写入超时,默认time.Second
redis.DialConnectTimeout(time.Second), // 连接超时,默认500*time.Millisecond
redis.DialPassword(""), // 鉴权密码,默认空
redis.DialDatabase(0), // 数据库号,默认0
redis.DialKeepAlive(time.Minute*5), // 默认5*time.Minute
redis.DialNetDial(nil), // 自定义dial,默认nil
redis.DialUseTLS(false), // 是否用TLS,默认false
redis.DialTLSSkipVerify(false), // 服务器证书校验,默认false
redis.DialTLSConfig(nil), // 默认nil,详见tls.Config
),
// 连接哨兵配置,用法于sentinel.DialOpts()一致
// 默认未配置的情况则直接使用sentinel.DialOpts()的配置
// sentinel.SentinelDialOpts()
)
var instance = redigo.New(sentinelMode)
res, err := instance.String(func(c redis.Conn) (res interface{}, err error) {
return c.Do("ECHO", echoStr)
})
if err != nil {
log.Fatal(err)
} else if res != echoStr {
log.Fatalf("unexpected result, expect = %s, but = %s", echoStr, res)
}
Cluster示例
var echoStr = "hello world"
var clusterMode = cluster.New(
cluster.Nodes([]string{
"192.168.0.110:30001", "192.168.0.110:30002", "192.168.0.110:30003",
"192.168.0.110:30004", "192.168.0.110:30005", "192.168.0.110:30006",
}),
cluster.PoolOpts(
mode.MaxActive(0), // 最大连接数,默认0无限制
mode.MaxIdle(0), // 最多保持空闲连接数,默认2*runtime.GOMAXPROCS(0)
mode.Wait(false), // 连接耗尽时是否等待,默认false
mode.IdleTimeout(0), // 空闲连接超时时间,默认0不超时
mode.MaxConnLifetime(0), // 连接的生命周期,默认0不失效
mode.TestOnBorrow(nil), // 空间连接取出后检测是否健康,默认nil
),
cluster.DialOpts(
redis.DialReadTimeout(time.Second), // 读取超时,默认time.Second
redis.DialWriteTimeout(time.Second), // 写入超时,默认time.Second
redis.DialConnectTimeout(time.Second), // 连接超时,默认500*time.Millisecond
redis.DialPassword(""), // 鉴权密码,默认空
redis.DialDatabase(0), // 数据库号,默认0
redis.DialKeepAlive(time.Minute*5), // 默认5*time.Minute
redis.DialNetDial(nil), // 自定义dial,默认nil
redis.DialUseTLS(false), // 是否用TLS,默认false
redis.DialTLSSkipVerify(false), // 服务器证书校验,默认false
redis.DialTLSConfig(nil), // 默认nil,详见tls.Config
),
)
var instance = redigo.New(clusterMode)
res, err := instance.String(func(c redis.Conn) (res interface{}, err error) {
return c.Do("ECHO", echoStr)
})
if err != nil {
log.Fatal(err)
} else if res != echoStr {
log.Fatalf("unexpected result, expect = %s, but = %s", echoStr, res)
}