diff --git a/nacos/resolver.go b/nacos/resolver.go index 180b5b1..b9bb8b0 100644 --- a/nacos/resolver.go +++ b/nacos/resolver.go @@ -21,6 +21,8 @@ import ( "strconv" "strings" + "github.com/nacos-group/nacos-sdk-go/common/constant" + "github.com/cloudwego/hertz/pkg/app/client/discovery" "github.com/hertz-contrib/registry/nacos/common" "github.com/nacos-group/nacos-sdk-go/clients/naming_client" @@ -31,8 +33,9 @@ var _ discovery.Resolver = (*nacosResolver)(nil) type ( resolverOptions struct { - cluster string - group string + cluster string + group string + serverConfig []constant.ServerConfig } // ResolverOption Option is nacos registry option. @@ -58,6 +61,13 @@ func WithResolverGroup(group string) ResolverOption { } } +// WithNacosServersConfig with server config option. +func WithNacosServersConfig(cfg []constant.ServerConfig) ResolverOption { + return func(o *resolverOptions) { + o.serverConfig = cfg + } +} + func (n *nacosResolver) Target(_ context.Context, target *discovery.TargetInfo) string { var metadata strings.Builder @@ -126,7 +136,12 @@ func (n *nacosResolver) Resolve(_ context.Context, desc string) (discovery.Resul } func (n *nacosResolver) Name() string { - return "nacos" + ":" + n.opts.cluster + ":" + n.opts.group + var name strings.Builder + name.WriteString("nacos" + ":" + n.opts.cluster + ":" + n.opts.group) + for _, config := range n.opts.serverConfig { + name.WriteString(":" + config.IpAddr + ":" + strconv.FormatUint(config.Port, 10)) + } + return name.String() } // NewDefaultNacosResolver create a default service resolver using nacos. diff --git a/nacos/v2/resolver.go b/nacos/v2/resolver.go index b7155ba..afd437b 100644 --- a/nacos/v2/resolver.go +++ b/nacos/v2/resolver.go @@ -101,7 +101,12 @@ func (n *nacosResolver) Resolve(_ context.Context, desc string) (discovery.Resul } func (n *nacosResolver) Name() string { - return "nacos" + ":" + n.opts.cluster + ":" + n.opts.group + var name strings.Builder + name.WriteString("nacos" + ":" + n.opts.cluster + ":" + n.opts.group) + for _, config := range n.opts.serverConfig { + name.WriteString(":" + config.IpAddr + ":" + strconv.FormatUint(config.Port, 10)) + } + return name.String() } // NewDefaultNacosResolver create a default service resolver using nacos. diff --git a/nacos/v2/resolver_option.go b/nacos/v2/resolver_option.go index 7d1f58b..7e888e3 100644 --- a/nacos/v2/resolver_option.go +++ b/nacos/v2/resolver_option.go @@ -14,9 +14,12 @@ package nacos +import "github.com/nacos-group/nacos-sdk-go/v2/common/constant" + type resolverOptions struct { - cluster string - group string + cluster string + group string + serverConfig []constant.ServerConfig } // ResolverOption Option is nacos registry option. @@ -35,3 +38,10 @@ func WithResolverGroup(group string) ResolverOption { o.group = group } } + +// WithNacosServersConfig with nacos server config option. +func WithNacosServersConfig(serverConfig []constant.ServerConfig) ResolverOption { + return func(o *resolverOptions) { + o.serverConfig = serverConfig + } +}