From 4f5ed22a395e3aeea4c803b82a049bcaf63706ec Mon Sep 17 00:00:00 2001 From: wanna Date: Thu, 15 Jan 2026 10:27:56 +0800 Subject: [PATCH 1/2] feat(nacos): add SetDefaultEndpoint and SetDefaultMetadata methods --- contrib/registry/nacos/nacos.go | 29 +++++++++++++++++++----- contrib/registry/nacos/nacos_register.go | 12 ++++++++++ 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/contrib/registry/nacos/nacos.go b/contrib/registry/nacos/nacos.go index 799db2a5d40..280fcf73e0d 100644 --- a/contrib/registry/nacos/nacos.go +++ b/contrib/registry/nacos/nacos.go @@ -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. @@ -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 } @@ -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 +} diff --git a/contrib/registry/nacos/nacos_register.go b/contrib/registry/nacos/nacos_register.go index 0b94c97ceca..c4c8ceddb55 100644 --- a/contrib/registry/nacos/nacos_register.go +++ b/contrib/registry/nacos/nacos_register.go @@ -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(), From a49830f8512b5b1777849484d6a4ba05cdf1d8b6 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Thu, 15 Jan 2026 02:31:43 +0000 Subject: [PATCH 2/2] Apply gci import order changes --- contrib/registry/nacos/nacos.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/contrib/registry/nacos/nacos.go b/contrib/registry/nacos/nacos.go index 280fcf73e0d..9ecdd3a6af0 100644 --- a/contrib/registry/nacos/nacos.go +++ b/contrib/registry/nacos/nacos.go @@ -34,11 +34,11 @@ var ( // Registry is nacos registry. type Registry struct { - client naming_client.INamingClient - clusterName string - groupName string - defaultEndpoint string - defaultMetadata map[string]string + client naming_client.INamingClient + clusterName string + groupName string + defaultEndpoint string + defaultMetadata map[string]string } // Config is the configuration object for nacos client.