Skip to content

Commit 6219da7

Browse files
wangle201210github-actions[bot]houseme
authored
feat(‎contrib/registry/nacos): add SetDefaultEndpoint and SetDefaultMetadata methods (gogf#4608)
Add configurable default endpoint and metadata support to nacos Registry, providing a more flexible alternative to hardcoded environment variable reads. - Add defaultEndpoint and defaultMetadata fields to Registry struct - Add SetDefaultEndpoint method to override service endpoints - Add SetDefaultMetadata method to merge extra metadata - Update Register method to use configured defaults --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: houseme <housemecn@gmail.com>
1 parent c600f3a commit 6219da7

2 files changed

Lines changed: 35 additions & 6 deletions

File tree

contrib/registry/nacos/nacos.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ var (
3434

3535
// Registry is nacos registry.
3636
type Registry struct {
37-
client naming_client.INamingClient
38-
clusterName string
39-
groupName string
37+
client naming_client.INamingClient
38+
clusterName string
39+
groupName string
40+
defaultEndpoint string
41+
defaultMetadata map[string]string
4042
}
4143

4244
// Config is the configuration object for nacos client.
@@ -101,9 +103,10 @@ func NewWithConfig(ctx context.Context, config Config) (reg *Registry, err error
101103
// NewWithClient new the instance with INamingClient
102104
func NewWithClient(client naming_client.INamingClient) *Registry {
103105
r := &Registry{
104-
client: client,
105-
clusterName: "DEFAULT",
106-
groupName: "DEFAULT_GROUP",
106+
client: client,
107+
clusterName: "DEFAULT",
108+
groupName: "DEFAULT_GROUP",
109+
defaultMetadata: make(map[string]string),
107110
}
108111
return r
109112
}
@@ -119,3 +122,17 @@ func (reg *Registry) SetGroupName(groupName string) *Registry {
119122
reg.groupName = groupName
120123
return reg
121124
}
125+
126+
// SetDefaultEndpoint sets the default endpoint for service registration.
127+
// It overrides the service endpoints when registering if it's not empty.
128+
func (reg *Registry) SetDefaultEndpoint(endpoint string) *Registry {
129+
reg.defaultEndpoint = endpoint
130+
return reg
131+
}
132+
133+
// SetDefaultMetadata sets the default metadata for service registration.
134+
// It will be merged with service's original metadata when registering.
135+
func (reg *Registry) SetDefaultMetadata(metadata map[string]string) *Registry {
136+
reg.defaultMetadata = metadata
137+
return reg
138+
}

contrib/registry/nacos/nacos_register.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,28 @@ import (
2020
func (reg *Registry) Register(ctx context.Context, service gsvc.Service) (registered gsvc.Service, err error) {
2121
metadata := map[string]string{}
2222
endpoints := service.GetEndpoints()
23+
24+
// Apply default endpoint override if configured
25+
if reg.defaultEndpoint != "" {
26+
endpoints = gsvc.Endpoints{gsvc.NewEndpoint(reg.defaultEndpoint)}
27+
}
28+
2329
p := vo.BatchRegisterInstanceParam{
2430
ServiceName: service.GetName(),
2531
GroupName: reg.groupName,
2632
Instances: make([]vo.RegisterInstanceParam, 0, len(endpoints)),
2733
}
2834

35+
// Copy service metadata
2936
for k, v := range service.GetMetadata() {
3037
metadata[k] = gconv.String(v)
3138
}
3239

40+
// Apply default metadata if configured
41+
for k, v := range reg.defaultMetadata {
42+
metadata[k] = v
43+
}
44+
3345
for _, endpoint := range endpoints {
3446
p.Instances = append(p.Instances, vo.RegisterInstanceParam{
3547
Ip: endpoint.Host(),

0 commit comments

Comments
 (0)