Ai
1 Star 0 Fork 0

saxon134/go-utils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
init.client.go 1.25 KB
一键复制 编辑 原始数据 按行查看 历史
saxon134 提交于 2023-10-03 16:56 +08:00 . init
package saMicro
import (
"context"
"gitee.com/saxon134/go-utils/saMicro/proto"
"github.com/go-kit/kit/endpoint"
grpctransport "github.com/go-kit/kit/transport/grpc"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
)
func InitClient(address string) (cl Service, err error) {
var conn *grpc.ClientConn
conn, err = grpc.Dial(address, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return
}
defer conn.Close()
cl = NewGRPCClient(conn)
return
}
func NewGRPCClient(conn *grpc.ClientConn) Service {
options := []grpctransport.ClientOption{
grpctransport.ClientBefore(func(ctx context.Context, md *metadata.MD) context.Context {
ctx = metadata.NewOutgoingContext(context.Background(), *md)
return ctx
}),
}
var apiEndpoint endpoint.Endpoint
{
apiEndpoint = grpctransport.NewClient(
conn,
"proto.Api",
"RpcApi",
EncodeRequest,
DecodeResponse,
proto.Response{},
options...).Endpoint()
}
return ApiEndPoint{
EndPoint: apiEndpoint,
}
}
func EncodeRequest(_ context.Context, request interface{}) (interface{}, error) {
return request, nil
}
func DecodeResponse(_ context.Context, response interface{}) (interface{}, error) {
return response, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/saxon134/go-utils.git
git@gitee.com:saxon134/go-utils.git
saxon134
go-utils
go-utils
b2602dc8c3fc

搜索帮助