add apis
CI / changes (push) Successful in 1m31s
CI / ad (push) Successful in 0s
CI / admin (push) Successful in 1s
CI / bff (push) Successful in 27s
CI / chore (push) Successful in 31s
CI / equipment (push) Successful in 30s
CI / express (push) Successful in 0s
CI / factory (push) Successful in 0s
CI / message (push) Successful in 0s
CI / order (push) Successful in 1s
CI / payment (push) Successful in 0s
CI / percutaneous (push) Successful in 0s
CI / product (push) Successful in 0s
CI / record (push) Successful in 0s
CI / sale (push) Successful in 0s
CI / support (push) Successful in 0s
CI / task (push) Successful in 0s
CI / user (push) Successful in 1s
CI / water (push) Successful in 0s
CI / wecom (push) Successful in 0s
CI / changes (push) Successful in 1m31s
CI / ad (push) Successful in 0s
CI / admin (push) Successful in 1s
CI / bff (push) Successful in 27s
CI / chore (push) Successful in 31s
CI / equipment (push) Successful in 30s
CI / express (push) Successful in 0s
CI / factory (push) Successful in 0s
CI / message (push) Successful in 0s
CI / order (push) Successful in 1s
CI / payment (push) Successful in 0s
CI / percutaneous (push) Successful in 0s
CI / product (push) Successful in 0s
CI / record (push) Successful in 0s
CI / sale (push) Successful in 0s
CI / support (push) Successful in 0s
CI / task (push) Successful in 0s
CI / user (push) Successful in 1s
CI / water (push) Successful in 0s
CI / wecom (push) Successful in 0s
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"context"
|
||||
store "lone-services/rpc/store/pb"
|
||||
user "lone-services/rpc/user/pb"
|
||||
"lone-services/services/equipment/internal/dao"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
func GetUser(id int64, name string) dao.UserInfo {
|
||||
var userInfo dao.UserInfo
|
||||
cli, err := GetRpcClient(name)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return userInfo
|
||||
}
|
||||
|
||||
client := user.NewUserClient(cli.Conn())
|
||||
info, err := client.InternalGetUserInfo(context.Background(), &user.InternalGetUserInfoReq{UserId: id})
|
||||
if err != nil {
|
||||
logx.Errorf("get user err: %v", err)
|
||||
return userInfo
|
||||
}
|
||||
|
||||
userInfo.Id = info.Id
|
||||
|
||||
return userInfo
|
||||
}
|
||||
|
||||
func GetStore(id int64, name string) dao.StoreInfo {
|
||||
var storeInfo dao.StoreInfo
|
||||
cli, err := GetRpcClient(name)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return storeInfo
|
||||
}
|
||||
|
||||
client := store.NewStoreClient(cli.Conn())
|
||||
info, err := client.GetInfoById(context.Background(), &store.IdRequest{Id: id})
|
||||
if err != nil {
|
||||
logx.Errorf("get store err: %v", err)
|
||||
return storeInfo
|
||||
}
|
||||
|
||||
storeInfo.Id = info.Id
|
||||
|
||||
return storeInfo
|
||||
}
|
||||
@@ -1,23 +1,98 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"lone-services/pkg/discovery"
|
||||
"lone-services/pkg/utils"
|
||||
"lone-services/services/equipment/internal/config"
|
||||
"net"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type rpcClientCacheEntry struct {
|
||||
cli zrpc.Client
|
||||
target string
|
||||
expireAt time.Time
|
||||
}
|
||||
|
||||
const rpcClientCacheTTL = 30 * time.Second //缓存过期时间
|
||||
|
||||
var (
|
||||
rpcCacheLock sync.Mutex
|
||||
rpcCache = make(map[string]*rpcClientCacheEntry)
|
||||
)
|
||||
|
||||
func GetRpcClient(serviceName string) (zrpc.Client, error) {
|
||||
if serviceName == utils.StringEmpty {
|
||||
err := fmt.Errorf("rpc serviceName is empty")
|
||||
logx.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cacheKey := serviceName
|
||||
|
||||
rpcCacheLock.Lock()
|
||||
entry, ok := rpcCache[cacheKey]
|
||||
if ok && time.Now().Before(entry.expireAt) {
|
||||
rpcCacheLock.Unlock()
|
||||
return entry.cli, nil
|
||||
}
|
||||
delete(rpcCache, cacheKey)
|
||||
rpcCacheLock.Unlock()
|
||||
|
||||
inst, err := discovery.Pick(serviceName)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("discovery pick %s failed: %w", serviceName, err)
|
||||
logx.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
target := net.JoinHostPort(inst.IP, strconv.FormatUint(inst.Port, utils.NumberTen))
|
||||
|
||||
cli := zrpc.MustNewClient(zrpc.RpcClientConf{
|
||||
Target: target,
|
||||
Timeout: utils.RpcTimeOut, //rpc调用超时5s
|
||||
})
|
||||
|
||||
rpcCacheLock.Lock()
|
||||
rpcCache[cacheKey] = &rpcClientCacheEntry{
|
||||
cli: cli,
|
||||
target: target,
|
||||
expireAt: time.Now().Add(rpcClientCacheTTL),
|
||||
}
|
||||
rpcCacheLock.Unlock()
|
||||
|
||||
return cli, nil
|
||||
}
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
DB *gorm.DB
|
||||
Prefix string
|
||||
Config config.Config
|
||||
DB *gorm.DB
|
||||
Prefix string
|
||||
StoreSvcName string
|
||||
UserSvcName string
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config, db *gorm.DB) *ServiceContext {
|
||||
|
||||
storeSvc := utils.GetConfigString("services.store")
|
||||
if storeSvc == utils.StringEmpty {
|
||||
logx.Error("config services.store empty")
|
||||
}
|
||||
userSvc := utils.GetConfigString("services.user")
|
||||
if userSvc == utils.StringEmpty {
|
||||
logx.Error("config services.user empty")
|
||||
}
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
DB: db,
|
||||
Prefix: utils.GetConfigString("mysql.prefix"),
|
||||
Config: c,
|
||||
DB: db,
|
||||
Prefix: utils.GetConfigString("mysql.prefix"),
|
||||
StoreSvcName: storeSvc,
|
||||
UserSvcName: userSvc,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user