feat: en redis and logs
This commit is contained in:
@@ -2,6 +2,18 @@ Name: product-service
|
||||
ListenOn: 0.0.0.0:10100
|
||||
Mode: dev
|
||||
|
||||
Log:
|
||||
ServiceName: product-service
|
||||
Mode: file # console | file | volume
|
||||
Encoding: plain
|
||||
Level: info
|
||||
Path: logs
|
||||
KeepDays: 7
|
||||
Compress: false
|
||||
# Rotation: daily
|
||||
# MaxSize: 100
|
||||
# MaxBackups: 10
|
||||
|
||||
Nacos:
|
||||
Hosts:
|
||||
- rnacos:8848
|
||||
@@ -17,8 +29,23 @@ Mysql:
|
||||
Database: dms-product
|
||||
Charset: utf8mb4
|
||||
Prefix:
|
||||
# ReadHost: mysql-slave # 必填才会启用读写分离
|
||||
# ReadPort: 3306 # 可省略,默认用主库 Port
|
||||
# ReadUser: root # 可省略,默认用主库 User
|
||||
# ReadPassword: "123123" # 可省略,默认用主库 Password
|
||||
# ReadDatabase: dms-product # 可省略,默认用主库 Database
|
||||
# ReadHost: mysql-slave
|
||||
# ReadPort: 3306
|
||||
# ReadUser: root
|
||||
# ReadPassword: "123123"
|
||||
# ReadDatabase: dms-product
|
||||
|
||||
BizRedis:
|
||||
Host: redis
|
||||
Port: 6379
|
||||
Password: ""
|
||||
DB: 0
|
||||
|
||||
AppLog:
|
||||
Path: logs
|
||||
InfoFile: info.log
|
||||
ErrorFile: error.log
|
||||
FatalFile: fatal.log
|
||||
MaxSize: 100
|
||||
MaxBackups: 10
|
||||
MaxAge: 30
|
||||
|
||||
+3
-2
@@ -4,7 +4,9 @@ go 1.24.3
|
||||
|
||||
require (
|
||||
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.6-20250717165733-d22d418d82d8.1
|
||||
github.com/redis/go-redis/v9 v9.19.0
|
||||
github.com/zeromicro/go-zero v1.10.2
|
||||
go.uber.org/zap v1.24.0
|
||||
google.golang.org/grpc v1.80.0
|
||||
google.golang.org/protobuf v1.36.11
|
||||
gorm.io/gorm v1.31.2
|
||||
@@ -59,7 +61,6 @@ require (
|
||||
github.com/prometheus/client_model v0.6.2 // indirect
|
||||
github.com/prometheus/common v0.66.1 // indirect
|
||||
github.com/prometheus/procfs v0.16.1 // indirect
|
||||
github.com/redis/go-redis/v9 v9.19.0 // indirect
|
||||
github.com/spaolacci/murmur3 v1.1.0 // indirect
|
||||
github.com/stoewer/go-strcase v1.3.0 // indirect
|
||||
github.com/titanous/json5 v1.0.0 // indirect
|
||||
@@ -82,7 +83,6 @@ require (
|
||||
go.uber.org/automaxprocs v1.6.0 // indirect
|
||||
go.uber.org/mock v0.6.0 // indirect
|
||||
go.uber.org/multierr v1.9.0 // indirect
|
||||
go.uber.org/zap v1.24.0 // indirect
|
||||
go.yaml.in/yaml/v2 v2.4.2 // indirect
|
||||
go.yaml.in/yaml/v3 v3.0.4 // indirect
|
||||
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 // indirect
|
||||
@@ -96,6 +96,7 @@ require (
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260128011058-8636f8732409 // indirect
|
||||
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
|
||||
gopkg.in/inf.v0 v0.9.1 // indirect
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
gorm.io/driver/mysql v1.6.0 // indirect
|
||||
|
||||
@@ -283,6 +283,8 @@ gopkg.in/h2non/gock.v1 v1.1.2 h1:jBbHXgGBK/AoPVfJh5x4r/WxIrElvbLel8TCZkkZJoY=
|
||||
gopkg.in/h2non/gock.v1 v1.1.2/go.mod h1:n7UGz/ckNChHiK05rDoiC4MYSunEC/lyaUm2WWaDva0=
|
||||
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
|
||||
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
|
||||
gopkg.in/sourcemap.v1 v1.0.5 h1:inv58fC9f9J3TK2Y2R1NPntXEn3/wjWHkonhIUODNTI=
|
||||
gopkg.in/sourcemap.v1 v1.0.5/go.mod h1:2RlvNNSMglmRrcvhfuzp4hQHwOtjxlbjX7UPY/GXb78=
|
||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
|
||||
@@ -4,8 +4,10 @@ import "github.com/zeromicro/go-zero/zrpc"
|
||||
|
||||
type Config struct {
|
||||
zrpc.RpcServerConf
|
||||
Nacos NacosConf
|
||||
Mysql MysqlConf
|
||||
Nacos NacosConf
|
||||
Mysql MysqlConf
|
||||
BizRedis RedisConf
|
||||
AppLog LogConf
|
||||
}
|
||||
|
||||
type NacosConf struct {
|
||||
@@ -30,3 +32,20 @@ type MysqlConf struct {
|
||||
ReadPassword string `json:",optional"`
|
||||
ReadDatabase string `json:",optional"`
|
||||
}
|
||||
|
||||
type RedisConf struct {
|
||||
Host string
|
||||
Port int `json:",default=6379"`
|
||||
Password string `json:",optional"`
|
||||
DB int `json:",optional"`
|
||||
}
|
||||
|
||||
type LogConf struct {
|
||||
Path string `json:",default=logs"`
|
||||
InfoFile string `json:",default=info.log"`
|
||||
ErrorFile string `json:",default=error.log"`
|
||||
FatalFile string `json:",default=fatal.log"`
|
||||
MaxSize int `json:",default=100"`
|
||||
MaxBackups int `json:",default=10"`
|
||||
MaxAge int `json:",default=30"`
|
||||
}
|
||||
|
||||
@@ -11,7 +11,9 @@ import (
|
||||
"product/internal/svc"
|
||||
"product/product"
|
||||
|
||||
"pkg.local/log"
|
||||
"pkg.local/modelbase"
|
||||
"pkg.local/redis"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"google.golang.org/grpc/codes"
|
||||
@@ -34,6 +36,11 @@ func NewAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLogic {
|
||||
}
|
||||
|
||||
func (l *AddLogic) Add(in *product.AddReq) (*product.AddResp, error) {
|
||||
l.Info("zero logx 方式 ... ")
|
||||
log.Info("原始 zap 方式 ... ")
|
||||
|
||||
redis.Client.Get(l.ctx, redis.GetRedisKey("product:test"))
|
||||
|
||||
if in.GetType() == dao.ProductTypeNormal {
|
||||
if in.GetAgentPrice() == 0 || in.GetStorePrice() == 0 ||
|
||||
in.GetSalePrice() == 0 || in.GetSharePrice() == 0 ||
|
||||
@@ -56,7 +63,7 @@ func (l *AddLogic) Add(in *product.AddReq) (*product.AddResp, error) {
|
||||
Eq: map[string]string{"name": in.GetName()},
|
||||
}, &check)
|
||||
if err != nil {
|
||||
l.Errorf("check product name: %v", err)
|
||||
log.Errorf("check product name: %v", err)
|
||||
return nil, status.Error(codes.Internal, "查询失败")
|
||||
}
|
||||
if check.Id > 0 {
|
||||
@@ -150,10 +157,18 @@ func (l *AddLogic) Add(in *product.AddReq) (*product.AddResp, error) {
|
||||
return model.VerifyModel{}.Init().WithTX(tx).Create(&verify)
|
||||
})
|
||||
if err != nil {
|
||||
l.Errorf("create product: %v", err)
|
||||
log.Errorf("create product: %v", err)
|
||||
return nil, status.Error(codes.Internal, "创建失败")
|
||||
}
|
||||
|
||||
// redis 用法示例
|
||||
key := redis.GetRedisKey("product:last_add")
|
||||
if err := redis.Client.Set(l.ctx, key, in.GetName(), time.Hour).Err(); err != nil {
|
||||
log.Errorf("redis set %s: %v", key, err)
|
||||
} else {
|
||||
log.Infof("add product success id=%d", data.Id)
|
||||
}
|
||||
|
||||
return &product.AddResp{}, nil
|
||||
}
|
||||
|
||||
|
||||
+32
-5
@@ -3,8 +3,8 @@ package main
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"product/internal/config"
|
||||
@@ -13,11 +13,14 @@ import (
|
||||
"product/product"
|
||||
|
||||
"pkg.local/discovery"
|
||||
"pkg.local/log"
|
||||
"pkg.local/modelbase"
|
||||
localmysql "pkg.local/mysql"
|
||||
"pkg.local/mysql"
|
||||
"pkg.local/redis"
|
||||
"pkg.local/validate"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/conf"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/core/service"
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
"google.golang.org/grpc"
|
||||
@@ -32,6 +35,19 @@ func main() {
|
||||
var c config.Config
|
||||
conf.MustLoad(*configFile, &c)
|
||||
|
||||
if err := log.Init(log.Config{
|
||||
Path: c.AppLog.Path,
|
||||
InfoFile: c.AppLog.InfoFile,
|
||||
ErrorFile: c.AppLog.ErrorFile,
|
||||
FatalFile: c.AppLog.FatalFile,
|
||||
MaxSize: c.AppLog.MaxSize,
|
||||
MaxBackups: c.AppLog.MaxBackups,
|
||||
MaxAge: c.AppLog.MaxAge,
|
||||
}); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "log init: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if err := discovery.Init(discovery.Config{
|
||||
Hosts: c.Nacos.Hosts,
|
||||
NamespaceId: c.Nacos.NamespaceId,
|
||||
@@ -55,11 +71,11 @@ func main() {
|
||||
}
|
||||
defer func() {
|
||||
if err := discovery.Deregister(); err != nil {
|
||||
log.Printf("nacos deregister: %v", err)
|
||||
log.Errorf("nacos deregister: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
db, err := localmysql.New(localmysql.Config{
|
||||
db, err := mysql.New(mysql.Config{
|
||||
Host: c.Mysql.Host,
|
||||
Port: c.Mysql.Port,
|
||||
User: c.Mysql.User,
|
||||
@@ -77,6 +93,15 @@ func main() {
|
||||
log.Fatalf("mysql init: %v", err)
|
||||
}
|
||||
|
||||
if err := redis.Init(redis.Config{
|
||||
Host: c.BizRedis.Host,
|
||||
Port: c.BizRedis.Port,
|
||||
Password: c.BizRedis.Password,
|
||||
DB: c.BizRedis.DB,
|
||||
}); err != nil {
|
||||
log.Fatalf("redis init: %v", err)
|
||||
}
|
||||
|
||||
// modelbase uses Table(prefix+name); keep gorm TablePrefix for Model(&T{}) paths.
|
||||
modelbase.Init(db, modelbase.Config{Prefix: c.Mysql.Prefix})
|
||||
|
||||
@@ -91,9 +116,11 @@ func main() {
|
||||
})
|
||||
defer s.Stop()
|
||||
|
||||
logx.AddWriter(logx.NewWriter(os.Stdout))
|
||||
|
||||
s.AddUnaryInterceptors(validate.UnaryServerInterceptor(validate.MustNew()))
|
||||
|
||||
fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)
|
||||
log.Infof("Starting rpc server at %s...", c.ListenOn)
|
||||
s.Start()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user