hertz101/rpc_gen/rpc/user/user_init.go
2025-01-17 17:09:32 +08:00

41 lines
754 B
Go

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...)
}