Skip to content

Commit

Permalink
fix: get error resolver when user uses different nacos in few seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
Skyenought committed Feb 5, 2024
1 parent d07355f commit f09dad1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
21 changes: 18 additions & 3 deletions nacos/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand All @@ -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

Expand Down Expand Up @@ -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.
Expand Down
7 changes: 6 additions & 1 deletion nacos/v2/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 12 additions & 2 deletions nacos/v2/resolver_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
}

0 comments on commit f09dad1

Please sign in to comment.