Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: get error resolver when user uses different nacos in few seconds #108

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
Loading