Skip to content

Commit

Permalink
feat: NewRedisClient.
Browse files Browse the repository at this point in the history
  • Loading branch information
tx7do committed Jun 12, 2023
1 parent 885b275 commit 610ef14
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 55 deletions.
20 changes: 3 additions & 17 deletions blog-backend/app/admin/service/internal/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package data

import (
"context"
"github.com/go-redis/redis/extra/redisotel/v8"
"github.com/go-redis/redis/v8"

authnEngine "github.com/tx7do/kratos-authn/engine"
Expand Down Expand Up @@ -52,7 +51,8 @@ func NewData(redisClient *redis.Client,
linkClient contentV1.LinkServiceClient,
postClient contentV1.PostServiceClient,
tagClient contentV1.TagServiceClient,
logger log.Logger) (*Data, func(), error) {
logger log.Logger,
) (*Data, func(), error) {
l := log.NewHelper(log.With(logger, "module", "data/admin-service"))

d := &Data{
Expand Down Expand Up @@ -80,21 +80,7 @@ func NewData(redisClient *redis.Client,
// NewRedisClient 创建Redis客户端
func NewRedisClient(cfg *conf.Bootstrap, logger log.Logger) *redis.Client {
l := log.NewHelper(log.With(logger, "module", "redis/data/admin-service"))

rdb := redis.NewClient(&redis.Options{
Addr: cfg.Data.Redis.Addr,
Password: cfg.Data.Redis.Password,
DB: int(cfg.Data.Redis.Db),
DialTimeout: cfg.Data.Redis.DialTimeout.AsDuration(),
WriteTimeout: cfg.Data.Redis.WriteTimeout.AsDuration(),
ReadTimeout: cfg.Data.Redis.ReadTimeout.AsDuration(),
})
if rdb == nil {
l.Fatalf("failed opening connection to redis")
}
rdb.AddHook(redisotel.NewTracingHook())

return rdb
return bootstrap.NewRedisClient(cfg, l)
}

// NewDiscovery 创建服务发现客户端
Expand Down
25 changes: 6 additions & 19 deletions blog-backend/app/core/service/internal/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"

"github.com/go-kratos/kratos/v2/log"
"github.com/go-redis/redis/extra/redisotel/v8"
"github.com/go-redis/redis/v8"

_ "github.com/go-sql-driver/mysql"
Expand All @@ -14,6 +13,8 @@ import (
"kratos-cms/app/core/service/internal/data/ent"
"kratos-cms/app/core/service/internal/data/ent/migrate"
"kratos-cms/gen/api/go/common/conf"

"kratos-cms/pkg/bootstrap"
)

// Data .
Expand All @@ -25,7 +26,7 @@ type Data struct {

// NewData .
func NewData(entClient *ent.Client, redisClient *redis.Client, logger log.Logger) (*Data, func(), error) {
l := log.NewHelper(log.With(logger, "module", "data/comment-service"))
l := log.NewHelper(log.With(logger, "module", "data/core-service"))

d := &Data{
db: entClient,
Expand All @@ -46,27 +47,13 @@ func NewData(entClient *ent.Client, redisClient *redis.Client, logger log.Logger

// NewRedisClient 创建Redis客户端
func NewRedisClient(cfg *conf.Bootstrap, logger log.Logger) *redis.Client {
l := log.NewHelper(log.With(logger, "module", "redis/data/comment-service"))

rdb := redis.NewClient(&redis.Options{
Addr: cfg.Data.Redis.Addr,
Password: cfg.Data.Redis.Password,
DB: int(cfg.Data.Redis.Db),
DialTimeout: cfg.Data.Redis.DialTimeout.AsDuration(),
WriteTimeout: cfg.Data.Redis.WriteTimeout.AsDuration(),
ReadTimeout: cfg.Data.Redis.ReadTimeout.AsDuration(),
})
if rdb == nil {
l.Fatalf("failed opening connection to redis")
}
rdb.AddHook(redisotel.NewTracingHook())

return rdb
l := log.NewHelper(log.With(logger, "module", "redis/data/core-service"))
return bootstrap.NewRedisClient(cfg, l)
}

// NewEntClient 创建数据库客户端
func NewEntClient(cfg *conf.Bootstrap, logger log.Logger) *ent.Client {
l := log.NewHelper(log.With(logger, "module", "ent/data/comment-service"))
l := log.NewHelper(log.With(logger, "module", "ent/data/core-service"))

client, err := ent.Open(
cfg.Data.Database.Driver,
Expand Down
20 changes: 3 additions & 17 deletions blog-backend/app/front/service/internal/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package data
import (
"context"

"github.com/go-redis/redis/extra/redisotel/v8"
"github.com/go-redis/redis/v8"

authnEngine "github.com/tx7do/kratos-authn/engine"
Expand Down Expand Up @@ -53,7 +52,8 @@ func NewData(redisClient *redis.Client,
linkClient contentV1.LinkServiceClient,
postClient contentV1.PostServiceClient,
tagClient contentV1.TagServiceClient,
logger log.Logger) (*Data, func(), error) {
logger log.Logger,
) (*Data, func(), error) {
l := log.NewHelper(log.With(logger, "module", "data/front-service"))

d := &Data{
Expand Down Expand Up @@ -81,21 +81,7 @@ func NewData(redisClient *redis.Client,
// NewRedisClient 创建Redis客户端
func NewRedisClient(cfg *conf.Bootstrap, logger log.Logger) *redis.Client {
l := log.NewHelper(log.With(logger, "module", "redis/data/front-service"))

rdb := redis.NewClient(&redis.Options{
Addr: cfg.Data.Redis.Addr,
Password: cfg.Data.Redis.Password,
DB: int(cfg.Data.Redis.Db),
DialTimeout: cfg.Data.Redis.DialTimeout.AsDuration(),
WriteTimeout: cfg.Data.Redis.WriteTimeout.AsDuration(),
ReadTimeout: cfg.Data.Redis.ReadTimeout.AsDuration(),
})
if rdb == nil {
l.Fatalf("failed opening connection to redis")
}
rdb.AddHook(redisotel.NewTracingHook())

return rdb
return bootstrap.NewRedisClient(cfg, l)
}

// NewDiscovery 创建服务发现客户端
Expand Down
4 changes: 2 additions & 2 deletions blog-backend/pkg/bootstrap/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// NewRedisClient 创建Redis客户端
func NewRedisClient(cfg *conf.Bootstrap) *redis.Client {
func NewRedisClient(cfg *conf.Bootstrap, logger *log.Helper) *redis.Client {
rdb := redis.NewClient(&redis.Options{
Addr: cfg.Data.Redis.Addr,
Password: cfg.Data.Redis.Password,
Expand All @@ -20,7 +20,7 @@ func NewRedisClient(cfg *conf.Bootstrap) *redis.Client {
ReadTimeout: cfg.Data.Redis.ReadTimeout.AsDuration(),
})
if rdb == nil {
log.Fatalf("failed opening connection to redis")
logger.Fatalf("failed opening connection to redis")
return nil
}
rdb.AddHook(redisotel.NewTracingHook())
Expand Down

0 comments on commit 610ef14

Please sign in to comment.