init
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
user "github.com/suyiiyii/hertz101/rpc_gen/kitex_gen/user"
|
||||
|
||||
"github.com/cloudwego/kitex/client"
|
||||
"github.com/cloudwego/kitex/client/callopt"
|
||||
)
|
||||
|
||||
type RPCClient interface {
|
||||
KitexClient() userservice.Client
|
||||
Service() string
|
||||
Register(ctx context.Context, Req *user.RegisterResp, callOptions ...callopt.Option) (r *user.RegisterResp, err error)
|
||||
Login(ctx context.Context, Req *user.LoginReq, callOptions ...callopt.Option) (r *user.LoginResp, err error)
|
||||
}
|
||||
|
||||
func NewRPCClient(dstService string, opts ...client.Option) (RPCClient, error) {
|
||||
kitexClient, err := userservice.NewClient(dstService, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cli := &clientImpl{
|
||||
service: dstService,
|
||||
kitexClient: kitexClient,
|
||||
}
|
||||
|
||||
return cli, nil
|
||||
}
|
||||
|
||||
type clientImpl struct {
|
||||
service string
|
||||
kitexClient userservice.Client
|
||||
}
|
||||
|
||||
func (c *clientImpl) Service() string {
|
||||
return c.service
|
||||
}
|
||||
|
||||
func (c *clientImpl) KitexClient() userservice.Client {
|
||||
return c.kitexClient
|
||||
}
|
||||
|
||||
func (c *clientImpl) Register(ctx context.Context, Req *user.RegisterResp, callOptions ...callopt.Option) (r *user.RegisterResp, err error) {
|
||||
return c.kitexClient.Register(ctx, Req, callOptions...)
|
||||
}
|
||||
|
||||
func (c *clientImpl) Login(ctx context.Context, Req *user.LoginReq, callOptions ...callopt.Option) (r *user.LoginResp, err error) {
|
||||
return c.kitexClient.Login(ctx, Req, callOptions...)
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/cloudwego/kitex/client/callopt"
|
||||
"github.com/cloudwego/kitex/pkg/klog"
|
||||
user "github.com/suyiiyii/hertz101/rpc_gen/kitex_gen/user"
|
||||
)
|
||||
|
||||
func Register(ctx context.Context, req *user.RegisterResp, callOptions ...callopt.Option) (resp *user.RegisterResp, err error) {
|
||||
resp, err = defaultClient.Register(ctx, req, callOptions...)
|
||||
if err != nil {
|
||||
klog.CtxErrorf(ctx, "Register call failed,err =%+v", err)
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func Login(ctx context.Context, req *user.LoginReq, callOptions ...callopt.Option) (resp *user.LoginResp, err error) {
|
||||
resp, err = defaultClient.Login(ctx, req, callOptions...)
|
||||
if err != nil {
|
||||
klog.CtxErrorf(ctx, "Login call failed,err =%+v", err)
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/cloudwego/kitex/client"
|
||||
)
|
||||
|
||||
var (
|
||||
// todo edit custom config
|
||||
defaultClient RPCClient
|
||||
defaultDstService = "user"
|
||||
defaultClientOpts = []client.Option{
|
||||
client.WithHostPorts("127.0.0.1:8888"),
|
||||
}
|
||||
once sync.Once
|
||||
)
|
||||
|
||||
func init() {
|
||||
DefaultClient()
|
||||
}
|
||||
|
||||
func DefaultClient() RPCClient {
|
||||
once.Do(func() {
|
||||
defaultClient = newClient(defaultDstService, defaultClientOpts...)
|
||||
})
|
||||
return defaultClient
|
||||
}
|
||||
|
||||
func newClient(dstService string, opts ...client.Option) RPCClient {
|
||||
c, err := NewRPCClient(dstService, opts...)
|
||||
if err != nil {
|
||||
panic("failed to init client: " + err.Error())
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func InitClient(dstService string, opts ...client.Option) {
|
||||
defaultClient = newClient(dstService, opts...)
|
||||
}
|
||||
Reference in New Issue
Block a user