119 lines
2.7 KiB
Go
119 lines
2.7 KiB
Go
package rpcx
|
|
|
|
import (
|
|
"errors"
|
|
"go_dreamfactory/lego/sys/log"
|
|
"go_dreamfactory/lego/utils/mapstructure"
|
|
|
|
"github.com/smallnest/rpcx/protocol"
|
|
)
|
|
|
|
type RpcxStartType int8
|
|
|
|
const (
|
|
RpcxStartByService RpcxStartType = iota //启动服务端
|
|
RpcxStartByClient //启动客户端
|
|
RpcxStartByAll //服务端客户端都启动
|
|
)
|
|
|
|
type Option func(*Options)
|
|
type Options struct {
|
|
ServiceTag string //集群标签
|
|
ServiceType string //服务类型
|
|
ServiceId string //服务id
|
|
ServiceVersion string //服务版本
|
|
ServiceAddr string //服务地址
|
|
ConsulServers []string //Consul集群服务地址
|
|
RpcxStartType RpcxStartType //Rpcx启动类型
|
|
SerializeType protocol.SerializeType
|
|
Debug bool //日志是否开启
|
|
Log log.ILog
|
|
}
|
|
|
|
func SetServiceTag(v string) Option {
|
|
return func(o *Options) {
|
|
o.ServiceTag = v
|
|
}
|
|
}
|
|
|
|
func SetServiceType(v string) Option {
|
|
return func(o *Options) {
|
|
o.ServiceType = v
|
|
}
|
|
}
|
|
|
|
func SetServiceId(v string) Option {
|
|
return func(o *Options) {
|
|
o.ServiceId = v
|
|
}
|
|
}
|
|
|
|
func SetServiceVersion(v string) Option {
|
|
return func(o *Options) {
|
|
o.ServiceVersion = v
|
|
}
|
|
}
|
|
|
|
func SetServiceAddr(v string) Option {
|
|
return func(o *Options) {
|
|
o.ServiceAddr = v
|
|
}
|
|
}
|
|
|
|
func SetConsulServers(v []string) Option {
|
|
return func(o *Options) {
|
|
o.ConsulServers = v
|
|
}
|
|
}
|
|
|
|
//设置启动类型
|
|
func SetRpcxStartType(v RpcxStartType) Option {
|
|
return func(o *Options) {
|
|
o.RpcxStartType = v
|
|
}
|
|
}
|
|
|
|
func SetDebug(v bool) Option {
|
|
return func(o *Options) {
|
|
o.Debug = v
|
|
}
|
|
}
|
|
func SetLog(v log.ILog) Option {
|
|
return func(o *Options) {
|
|
o.Log = v
|
|
}
|
|
}
|
|
|
|
func newOptions(config map[string]interface{}, opts ...Option) (Options, error) {
|
|
options := Options{
|
|
SerializeType: protocol.MsgPack,
|
|
Debug: true,
|
|
Log: log.Clone(log.SetLoglayer(2)),
|
|
}
|
|
if config != nil {
|
|
mapstructure.Decode(config, &options)
|
|
}
|
|
for _, o := range opts {
|
|
o(&options)
|
|
}
|
|
if len(options.ServiceTag) == 0 || len(options.ServiceType) == 0 || len(options.ServiceId) == 0 || len(options.ConsulServers) == 0 {
|
|
return options, errors.New("[Sys.RPCX] newOptions err: 启动参数异常")
|
|
}
|
|
return options, nil
|
|
}
|
|
|
|
func newOptionsByOption(opts ...Option) (Options, error) {
|
|
options := Options{
|
|
SerializeType: protocol.MsgPack,
|
|
Debug: true,
|
|
Log: log.Clone(log.SetLoglayer(2)),
|
|
}
|
|
for _, o := range opts {
|
|
o(&options)
|
|
}
|
|
if len(options.ServiceTag) == 0 || len(options.ServiceType) == 0 || len(options.ServiceId) == 0 || len(options.ConsulServers) == 0 {
|
|
return options, errors.New("[Sys.RPCX] newOptions err: 启动参数异常")
|
|
}
|
|
return options, nil
|
|
}
|