Skip to content
Merged
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
29 changes: 23 additions & 6 deletions contrib/registry/nacos/nacos.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ var (

// Registry is nacos registry.
type Registry struct {
client naming_client.INamingClient
clusterName string
groupName string
client naming_client.INamingClient
clusterName string
groupName string
defaultEndpoint string
defaultMetadata map[string]string
}

// Config is the configuration object for nacos client.
Expand Down Expand Up @@ -101,9 +103,10 @@ func NewWithConfig(ctx context.Context, config Config) (reg *Registry, err error
// NewWithClient new the instance with INamingClient
func NewWithClient(client naming_client.INamingClient) *Registry {
r := &Registry{
client: client,
clusterName: "DEFAULT",
groupName: "DEFAULT_GROUP",
client: client,
clusterName: "DEFAULT",
groupName: "DEFAULT_GROUP",
defaultMetadata: make(map[string]string),
}
return r
}
Expand All @@ -119,3 +122,17 @@ func (reg *Registry) SetGroupName(groupName string) *Registry {
reg.groupName = groupName
return reg
}

// SetDefaultEndpoint sets the default endpoint for service registration.
// It overrides the service endpoints when registering if it's not empty.
func (reg *Registry) SetDefaultEndpoint(endpoint string) *Registry {
reg.defaultEndpoint = endpoint
return reg
}

// SetDefaultMetadata sets the default metadata for service registration.
// It will be merged with service's original metadata when registering.
func (reg *Registry) SetDefaultMetadata(metadata map[string]string) *Registry {
reg.defaultMetadata = metadata
return reg
}
12 changes: 12 additions & 0 deletions contrib/registry/nacos/nacos_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,28 @@ import (
func (reg *Registry) Register(ctx context.Context, service gsvc.Service) (registered gsvc.Service, err error) {
metadata := map[string]string{}
endpoints := service.GetEndpoints()

// Apply default endpoint override if configured
if reg.defaultEndpoint != "" {
endpoints = gsvc.Endpoints{gsvc.NewEndpoint(reg.defaultEndpoint)}
}

p := vo.BatchRegisterInstanceParam{
ServiceName: service.GetName(),
GroupName: reg.groupName,
Instances: make([]vo.RegisterInstanceParam, 0, len(endpoints)),
}

// Copy service metadata
for k, v := range service.GetMetadata() {
metadata[k] = gconv.String(v)
}

// Apply default metadata if configured
for k, v := range reg.defaultMetadata {
metadata[k] = v
}

for _, endpoint := range endpoints {
p.Instances = append(p.Instances, vo.RegisterInstanceParam{
Ip: endpoint.Host(),
Expand Down
Loading