Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: update Redis doc #528

Merged
merged 2 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pkg/client/redis/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,17 @@ func (config *Config) MustSingleton() *Client {
if val, ok := singleton.Load(constant.ModuleClientRedis, config.name); ok && val != nil {
return val.(*Client)
}
cc := config.MustBuild()
singleton.Store(constant.ModuleClientRedis, config.name, cc)
return cc
}

// MustBuild ..
func (config *Config) MustBuild() *Client {
cc, err := config.Build()
if err != nil {
config.logger.Panic("redis:"+err.Error(), xlog.FieldExtMessage(config))
}
singleton.Store(constant.ModuleClientRedis, config.name, cc)
return cc
}

Expand Down
40 changes: 19 additions & 21 deletions website/docs/jupiter/4.4clientredis.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,37 @@

## 4.4.1 简介

client/redis 包是对go-redis 进行二次封装,并提供redisStub和redisClusterStub
两种访问reids方式的客户端
client/redis 包是对go-redis v8版本的二次封装,提供主从节点的访问方式,后续视需求提供集群节点的访问方式

## 4.4.2 配置规范

[配置说明](http://jupiter.douyu.com/jupiter/6.8clientredis.html)

## 4.4.3 用法

[访问redis示例](https://github.com/douyu/jupiter-examples/tree/main/client/redis)
[访问redis示例](https://github.com/douyu/jupiter-layout/tree/main/internal/pkg/redis)

```go
// run: go run main.go --config=config.toml
package main

import (
"context"
"time"

"github.com/douyu/jupiter"
"github.com/douyu/jupiter/pkg/client/redis"
"github.com/douyu/jupiter/pkg/xlog"
)

type Engine struct {
jupiter.Application
}

func NewEngine() *Engine {
eng := &Engine{}
if err := eng.Startup(
eng.exampleForRedisStub,
eng.exampleForRedisClusterStub,
eng.exampleForRedis,
); err != nil {
xlog.Panic("startup", xlog.Any("err", err))
}
Expand All @@ -37,28 +46,17 @@ func main() {
}
}

func (eng *Engine) exampleForRedisStub() (err error) {
func (eng *Engine) exampleForRedis() (err error) {
//build redisStub
redisStub := redis.StdRedisConfig("myredis").Build()
redisClient := redis.StdConfig("test").MustSingleton()
// set string
setRes := redisStub.Set("jupiter-redis", "redisStub", time.Second*5)
xlog.Info("redisStub set string", xlog.Any("res", setRes))
setRes, _ := redisClient.CmdOnMaster().Set(context.Background(), "jupiter-redis", "hello", time.Second).Result()
xlog.Info("redis set string", xlog.Any("res", setRes))
// get string
getRes := redisStub.Get("jupiter-redis")
getRes, _ := redisClient.CmdOnSlave().Get(context.Background(), "jupiter-redis").Result()
xlog.Info("redisStub get string", xlog.Any("res", getRes))
return
}
func (eng *Engine) exampleForRedisClusterStub() (err error) {
//build redisClusterStub
redisStub := redis.StdRedisClusterConfig("myredis").Build()
// set string
setRes := redisStub.Set("jupiter-redisCluster", "redisClusterStub", time.Second*5)
xlog.Info("redisClusterStub set string", xlog.Any("res", setRes))
// get string
getRes := redisStub.Get("jupiter-redisCluster")
xlog.Info("redisClusterStub get string", xlog.Any("res", getRes))
return
}

```

Expand Down
74 changes: 35 additions & 39 deletions website/docs/jupiter/6.8clientredis.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,53 +10,49 @@

| 名称 | 类型 | 描述 |
| :------------: | :-----------: | :----------------------------: |
| `addr` | string | 连接地址 |
| `password` | string | 密码 |
| `master.addr` | string | 主节点dns连接地址,包含用户名密码 |
| `slaves.addr` | []string | 从节点dns连接地址 包含用户名密码 |
| `db` | int | 默认为0, 一般应用不推荐使用DB分片 |
| `poolSize` | int | 集群内每个节点的最大连接池限制 默认每个CPU10个连接 |
| `maxRetries` | int | 网络相关的错误最大重试次数 默认8次 |
| `minIdleConns` | int | 最小空闲连接数 |
| `dialTimeout` | time.Duration | 拨超时时间 |
| `readTimeout` | time.Duration | 读超时 默认3s |
| `writeTimeout` | time.Duration | 写超时 默认3s |
| `idleTimeout` | int | 连接最大空闲时间,默认60s, 超过该时间,连接会被主动关闭 |
| `debug` | bool | 是否开启debug |
| `poolSize` | int | 每个节点的最大连接池限制 默认200个连接 |
| `maxRetries` | int | 网络相关的错误最大重试次数,默认0次 |
| `minIdleConns` | int | 最小空闲连接数,默认20 |
| `dialTimeout` | time.Duration | 拨超时时间,默认3s |
| `readTimeout` | time.Duration | 读超时,默认1s |
| `writeTimeout` | time.Duration | 写超时,默认1s |
| `idleTimeout` | int | 连接最大空闲时间,默认60s, 超过该时间,连接会被主动关闭 |
| `readOnMaster` | bool | 是否开启主节点读,默认true |
| `debug` | bool | 是否开启debug,默认false |
| `slowThreshold` | bool | 慢日志记录阈值,默认250ms |
| `enableMetric` | bool | 是否开启metric上报,默认true |
| `enableTrace` | bool | 是否开启trace,默认true |
| `enableAccessLog` | bool | 是否开启access记录,默认false |
| .... | | |

#### 示例

```toml
[jupiter.redis.myredis.stub]
addr = "ip:port"
password = "xxxxxxxx"
[jupiter.redis]
[jupiter.redis.test]
[jupiter.redis.test.stub]
dialTimeout="2s"
readTimeout="5s"
idleTimeout="60s"
debug=true
[jupiter.redis.test.stub.master]
addr="redis://username:password@127.0.0.1:6379" # password可包含:
[jupiter.redis.test.stub.slaves]
addr=[
"redis://username:password@127.0.0.2:6379",# password可包含:
]
```

addr是dns连接包含用户名密码
- 无密码写法
`redis://127.0.0.2:6379`
- 无用户名有密码写法
`redis://:password@127.0.0.2:6379` password前的`:`不可缺省
- 有用户名有密码写法
`redis://username:password@127.0.0.2:6379`
### RedisClusterConfig

#### 配置项

| 名称 | 类型 | 描述 |
| :------------: | :-----------: | :----------------------------: |
| `addrs` | \[\]string | 连接地址 |
| `password` | string | 密码 |
| `db` | int | 默认为0, 一般应用不推荐使用DB分片 |
| `poolSize` | int | 集群内每个节点的最大连接池限制 默认每个CPU10个连接 |
| `maxRetries` | int | 网络相关的错误最大重试次数 默认8次 |
| `minIdleConns` | int | 最小空闲连接数 |
| `dialTimeout` | time.Duration | 拨超时时间 |
| `readTimeout` | time.Duration | 读超时 默认3s |
| `writeTimeout` | time.Duration | 写超时 默认3s |
| `idleTimeout` | int | 连接最大空闲时间,默认60s, 超过该时间,连接会被主动关闭 |
| `debug` | bool | 是否开启debug |
| `idleTimeout` | int | 连接最大空闲时间,默认60s, 超过该时间,连接会被主动关闭 |
| `readOnly` | bool | 集群模式 在从属节点上启用读模式 |
| .... | | |

#### 示例

```toml
[jupiter.redis.myredis.cluster]
addrs =["ip:port","ip:port","ip:port"]
password = "xxxxxxxx"
```
暂不支持,后面有需要再加
Binary file modified website/docs/static/jupiter/client-redis.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.