1 Star 0 Fork 0

csingo / cRpc

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Type.go 3.67 KB
一键复制 编辑 原始数据 按行查看 历史
joe 提交于 2023-12-15 10:09 . update
package cRpc
import (
"fmt"
"sync"
"google.golang.org/grpc/credentials/insecure"
"gitee.com/csingo/cLog"
"github.com/gin-gonic/gin"
"google.golang.org/grpc"
)
//const (
// Header_Rpc_App = "Rpc-App"
// Header_Rpc_Service = "Rpc-Service"
// Header_Rpc_Method = "Rpc-Method"
//)
type DecryptFunc func(ctx *gin.Context) bool
type EncryptFunc func(ctx *gin.Context) string
type ParseHostFunc func(ctx *gin.Context, app, service string) (host, uri string) // 自定义路由策略
type RpcServiceInterface interface {
RpcServiceName() (app, service string)
}
type RpcContainer struct {
Lock *sync.Mutex
DecryptFunc DecryptFunc
Instances map[string]interface{}
}
func (i *RpcContainer) Save(instance interface{}) {
i.Lock.Lock()
defer i.Lock.Unlock()
if IsRpcService(instance) {
app, service := instance.(RpcServiceInterface).RpcServiceName()
name := fmt.Sprintf("%s.%s", app, service)
i.Instances[name] = instance
}
}
func (i *RpcContainer) Get(app, service string) interface{} {
i.Lock.Lock()
defer i.Lock.Unlock()
name := fmt.Sprintf("%s.%s", app, service)
if _, ok := i.Instances[name]; ok {
return i.Instances[name]
}
return nil
}
func (i *RpcContainer) SetDecrypt(f DecryptFunc) {
i.Lock.Lock()
defer i.Lock.Unlock()
i.DecryptFunc = f
}
var container = &RpcContainer{
Lock: &sync.Mutex{},
Instances: make(map[string]interface{}),
}
type RpcClientInterface interface {
RpcClientName() (pkg, app, service string)
}
type RpcClientContainer struct {
Lock *sync.Mutex
Signature string
Hosts map[string]string
HostFunc ParseHostFunc
EncryptFunc EncryptFunc
GrpcConn map[string]grpc.ClientConnInterface
}
func (i *RpcClientContainer) Save(client *RpcConf_Client) {
i.Lock.Lock()
defer i.Lock.Unlock()
if client == nil || len(client.Services) == 0 {
return
}
var conn grpc.ClientConnInterface
var err error
if client.Type == GRPCClient {
cLog.WithContext(nil, map[string]interface{}{
"host": client.Host,
}).Info("GRPC 客户端连接初始化")
conn, err = grpc.Dial(client.Host, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
cLog.WithContext(nil, map[string]interface{}{
"client": client,
"err": err.Error(),
}).Error("GRPC 客户端初始化连接失败")
}
}
for _, service := range client.Services {
name := fmt.Sprintf("%s.%s.%s", client.Package, service.App, service.Service)
i.Hosts[name] = client.Host
if client.Type == GRPCClient {
i.GrpcConn[name] = conn
}
}
}
func (i *RpcClientContainer) GetGrpcConn(pkg, app, service string) (conn grpc.ClientConnInterface, err error) {
var ok bool
name := fmt.Sprintf("%s.%s.%s", pkg, app, service)
if conn, ok = i.GrpcConn[name]; !ok {
err = fmt.Errorf("GRPC 客户端连接不存在")
cLog.WithContext(nil, map[string]interface{}{
"conn": i.GrpcConn,
"name": name,
}).Error("GRPC 客户端连接不存在")
}
return
}
func (i *RpcClientContainer) GetHost(pkg, app, service string) string {
i.Lock.Lock()
defer i.Lock.Unlock()
name := fmt.Sprintf("%s.%s.%s", pkg, app, service)
if _, ok := i.Hosts[name]; ok {
return i.Hosts[name]
}
return ""
}
func (i *RpcClientContainer) SetHostFunc(hostFunc ParseHostFunc) {
i.Lock.Lock()
defer i.Lock.Unlock()
i.HostFunc = hostFunc
}
func (i *RpcClientContainer) SetSignature(key string) {
i.Lock.Lock()
defer i.Lock.Unlock()
i.Signature = key
}
func (i *RpcClientContainer) SetEncrypt(f EncryptFunc) {
i.Lock.Lock()
defer i.Lock.Unlock()
i.EncryptFunc = f
}
var clientContainer = &RpcClientContainer{
Lock: &sync.Mutex{},
Signature: "",
Hosts: make(map[string]string),
HostFunc: nil,
GrpcConn: map[string]grpc.ClientConnInterface{},
}
Go
1
https://gitee.com/csingo/cRpc.git
git@gitee.com:csingo/cRpc.git
csingo
cRpc
cRpc
v0.3.6

搜索帮助