This is an automated email from the ASF dual-hosted git repository.
alexstocks pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/dubbo-go.git
The following commit(s) were added to refs/heads/develop by this push:
new e608f8ff1 Fix: the issue where adding weight to the nacos option fails
(#2843)
e608f8ff1 is described below
commit e608f8ff1d906efb64b34a0872d6708e9cbfd1b4
Author: 1kasa <[email protected]>
AuthorDate: Sat Apr 26 11:07:39 2025 +0800
Fix: the issue where adding weight to the nacos option fails (#2843)
* fix: weight option failed
---
common/constant/key.go | 7 +++++++
registry/nacos/registry.go | 13 ++++++++++++-
registry/nacos/service_discovery.go | 18 +++++++++++++++++-
registry/nacos/service_discovery_test.go | 1 +
4 files changed, 37 insertions(+), 2 deletions(-)
diff --git a/common/constant/key.go b/common/constant/key.go
index 08685de28..fca26aef0 100644
--- a/common/constant/key.go
+++ b/common/constant/key.go
@@ -468,3 +468,10 @@ const (
TripleGoInterfaceName = "XXX_TRIPLE_GO_INTERFACE_NAME"
TripleGoMethodName = "XXX_TRIPLE_GO_METHOD_NAME"
)
+
+// Weight constants for Nacos instance registration
+const (
+ DefaultNacosWeight = 1.0 // Default weight if not specified or
invalid
+ MinNacosWeight = 0.0 // Minimum allowed weight (Nacos range
starts at 0)
+ MaxNacosWeight = 10000.0 // Maximum allowed weight (Nacos range
ends at 10000)
+)
diff --git a/registry/nacos/registry.go b/registry/nacos/registry.go
index da0359f0c..d482c063b 100644
--- a/registry/nacos/registry.go
+++ b/registry/nacos/registry.go
@@ -99,11 +99,22 @@ func createRegisterParam(url *common.URL, serviceName
string, groupName string)
params[constant.MethodsKey] = strings.Join(url.Methods, ",")
common.HandleRegisterIPAndPort(url)
port, _ := strconv.Atoi(url.Port)
+
+ weightStr := url.GetParam(constant.WeightKey, "1.0")
+ weight, err := strconv.ParseFloat(weightStr, 64)
+ if err != nil || weight <= constant.MinNacosWeight {
+ logger.Warnf("Invalid weight value %q, using default 1.0. err:
%v", weightStr, err)
+ weight = constant.DefaultNacosWeight
+ } else if weight > constant.MaxNacosWeight {
+ logger.Warnf("Weight %f exceeds Nacos maximum 10000, setting to
10000", weight)
+ weight = constant.MaxNacosWeight
+ }
+
instance := vo.RegisterInstanceParam{
Ip: url.Ip,
Port: uint64(port),
Metadata: params,
- Weight: 1,
+ Weight: weight,
Enable: true,
Healthy: true,
Ephemeral: true,
diff --git a/registry/nacos/service_discovery.go
b/registry/nacos/service_discovery.go
index 59a4ea027..b216be0a7 100644
--- a/registry/nacos/service_discovery.go
+++ b/registry/nacos/service_discovery.go
@@ -20,6 +20,7 @@ package nacos
import (
"fmt"
"regexp"
+ "strconv"
"sync"
)
@@ -65,6 +66,9 @@ type nacosServiceDiscovery struct {
// cache registry instances
registryInstances []registry.ServiceInstance
+ // registryURL stores the URL used for registration, used to fetch
dynamic config like weight
+ registryURL *common.URL
+
instanceListenerMap map[string]*gxset.HashSet
listenerLock sync.Mutex
}
@@ -310,6 +314,17 @@ func (n *nacosServiceDiscovery)
toRegisterInstance(instance registry.ServiceInst
if metadata == nil {
metadata = make(map[string]string, 1)
}
+
+ weightStr :=
n.registryURL.GetParam(constant.RegistryKey+"."+constant.WeightKey, "1.0")
+ weight, err := strconv.ParseFloat(weightStr, 64)
+ if err != nil || weight <= constant.MinNacosWeight {
+ logger.Warnf("Invalid weight value %q, using default 1.0. err:
%v", weightStr, err)
+ weight = constant.DefaultNacosWeight
+ } else if weight > constant.MaxNacosWeight {
+ logger.Warnf("Weight %f exceeds Nacos maximum 10000, setting to
10000", weight)
+ weight = constant.MaxNacosWeight
+ }
+
metadata[idKey] = instance.GetID()
return vo.RegisterInstanceParam{
ServiceName: instance.GetServiceName(),
@@ -317,7 +332,7 @@ func (n *nacosServiceDiscovery) toRegisterInstance(instance
registry.ServiceInst
Port: uint64(instance.GetPort()),
Metadata: metadata,
// We must specify the weight since Java nacos namingClient
will ignore the instance whose weight is 0
- Weight: 1,
+ Weight: weight,
Enable: instance.IsEnable(),
Healthy: instance.IsHealthy(),
GroupName: n.group,
@@ -364,6 +379,7 @@ func newNacosServiceDiscovery(url *common.URL)
(registry.ServiceDiscovery, error
namingClient: client,
descriptor: descriptor,
registryInstances: []registry.ServiceInstance{},
+ registryURL: url,
instanceListenerMap: make(map[string]*gxset.HashSet),
}
return newInstance, nil
diff --git a/registry/nacos/service_discovery_test.go
b/registry/nacos/service_discovery_test.go
index 9f6a335b9..1d51a4a02 100644
--- a/registry/nacos/service_discovery_test.go
+++ b/registry/nacos/service_discovery_test.go
@@ -143,6 +143,7 @@ func newMockNacosServiceDiscovery(url *common.URL)
(registry.ServiceDiscovery, e
namingClient: client,
descriptor: descriptor,
registryInstances: []registry.ServiceInstance{},
+ registryURL: url,
instanceListenerMap: make(map[string]*gxset.HashSet),
}
return newInstance, nil