|
|
@ -1,6 +1,7 @@
|
|
|
|
package client
|
|
|
|
package client
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"git.oa00.com/supply-chain/service/config"
|
|
|
|
"git.oa00.com/supply-chain/service/config"
|
|
|
@ -23,20 +24,41 @@ func init() {
|
|
|
|
basePkgPath = pkgPath[:len(pkgPath)-6]
|
|
|
|
basePkgPath = pkgPath[:len(pkgPath)-6]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type RpcClient struct {
|
|
|
|
|
|
|
|
baseName string
|
|
|
|
|
|
|
|
serviceName string
|
|
|
|
|
|
|
|
client client.XClient
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Call @Title 调用接口
|
|
|
|
|
|
|
|
func (r RpcClient) Call(ctx context.Context, serviceMethod string, args interface{}, reply interface{}) (err error) {
|
|
|
|
|
|
|
|
if config.RpcConfig.BeforeHandel != nil {
|
|
|
|
|
|
|
|
ctx, err = config.RpcConfig.BeforeHandel(ctx, r.baseName, r.serviceName, serviceMethod, args, reply)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 调用请求
|
|
|
|
|
|
|
|
err = r.client.Call(ctx, serviceMethod, args, reply)
|
|
|
|
|
|
|
|
if config.RpcConfig.AfterHandel != nil {
|
|
|
|
|
|
|
|
err = config.RpcConfig.AfterHandel(ctx, r.baseName, r.serviceName, serviceMethod, args, reply, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetClient @Title 获取RPC客户的
|
|
|
|
// GetClient @Title 获取RPC客户的
|
|
|
|
func GetClient(s interface{}) (client.XClient, error) {
|
|
|
|
func GetClient(s interface{}) (*RpcClient, error) {
|
|
|
|
path := strings.TrimPrefix(reflect.ValueOf(s).Elem().Type().PkgPath(), basePkgPath)
|
|
|
|
path := strings.TrimPrefix(reflect.ValueOf(s).Elem().Type().PkgPath(), basePkgPath)
|
|
|
|
actionName := reflect.ValueOf(s).Elem().Type().Name()
|
|
|
|
actionName := reflect.ValueOf(s).Elem().Type().Name()
|
|
|
|
key := path + "/" + actionName
|
|
|
|
key := path + "/" + actionName
|
|
|
|
|
|
|
|
split := strings.SplitN(key, "/", 2)
|
|
|
|
|
|
|
|
basePath := split[0]
|
|
|
|
|
|
|
|
servicePath := split[1]
|
|
|
|
xClient, ok := mClient.Load(key)
|
|
|
|
xClient, ok := mClient.Load(key)
|
|
|
|
if !ok {
|
|
|
|
if !ok {
|
|
|
|
mutex.Lock()
|
|
|
|
mutex.Lock()
|
|
|
|
xClient, ok = mClient.Load(key)
|
|
|
|
xClient, ok = mClient.Load(key)
|
|
|
|
if !ok {
|
|
|
|
if !ok {
|
|
|
|
split := strings.SplitN(key, "/", 2)
|
|
|
|
|
|
|
|
basePath := split[0]
|
|
|
|
|
|
|
|
servicePath := split[1]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
d, err := client.NewConsulDiscovery(basePath, servicePath, config.RpcConfig.RegistryServer, nil)
|
|
|
|
d, err := client.NewConsulDiscovery(basePath, servicePath, config.RpcConfig.RegistryServer, nil)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("系统异常")
|
|
|
|
return nil, errors.New("系统异常")
|
|
|
@ -51,11 +73,11 @@ func GetClient(s interface{}) (client.XClient, error) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mutex.Unlock()
|
|
|
|
mutex.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return xClient.(client.XClient), nil
|
|
|
|
return &RpcClient{client: xClient.(client.XClient), baseName: basePath, serviceName: servicePath}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetClientName @Title 根据服务名获取客户端
|
|
|
|
// GetClientName @Title 根据服务名获取客户端
|
|
|
|
func GetClientName(base, service string) (client.XClient, error) {
|
|
|
|
func GetClientName(base, service string) (*RpcClient, error) {
|
|
|
|
key := fmt.Sprintf("%s/%s", base, service)
|
|
|
|
key := fmt.Sprintf("%s/%s", base, service)
|
|
|
|
xClient, ok := mClient.Load(key)
|
|
|
|
xClient, ok := mClient.Load(key)
|
|
|
|
if !ok {
|
|
|
|
if !ok {
|
|
|
@ -77,5 +99,5 @@ func GetClientName(base, service string) (client.XClient, error) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mutex.Unlock()
|
|
|
|
mutex.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return xClient.(client.XClient), nil
|
|
|
|
return &RpcClient{client: xClient.(client.XClient), baseName: base, serviceName: service}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|