diff --git a/common/constant/key.go b/common/constant/key.go index 849d1def0d..3160f92a1e 100644 --- a/common/constant/key.go +++ b/common/constant/key.go @@ -138,6 +138,7 @@ const ( REGISTRY_TTL_KEY = "registry.ttl" SIMPLIFIED_KEY = "simplified" NAMESPACE_KEY = "namespace" + REGISTRY_GROUP_KEY = "registry.group" ) const ( diff --git a/registry/nacos/listener.go b/registry/nacos/listener.go index e9fa3b847f..8dad195c19 100644 --- a/registry/nacos/listener.go +++ b/registry/nacos/listener.go @@ -190,7 +190,12 @@ func (nl *nacosListener) startListen() error { return perrors.New("nacos naming namingClient stopped") } serviceName := getSubscribeName(nl.listenUrl) - nl.subscribeParam = &vo.SubscribeParam{ServiceName: serviceName, SubscribeCallback: nl.Callback} + groupName := nl.listenUrl.GetParam(constant.REGISTRY_GROUP_KEY, defaultGroup) + nl.subscribeParam = &vo.SubscribeParam{ + ServiceName: serviceName, + SubscribeCallback: nl.Callback, + GroupName: groupName, + } go func() { _ = nl.namingClient.Client().Subscribe(nl.subscribeParam) }() diff --git a/registry/nacos/registry.go b/registry/nacos/registry.go index ed58fd61a9..348555c0e9 100644 --- a/registry/nacos/registry.go +++ b/registry/nacos/registry.go @@ -83,7 +83,7 @@ func appendParam(target *bytes.Buffer, url *common.URL, key string) { } } -func createRegisterParam(url *common.URL, serviceName string) vo.RegisterInstanceParam { +func createRegisterParam(url *common.URL, serviceName string, groupName string) vo.RegisterInstanceParam { category := getCategory(url) params := make(map[string]string) @@ -111,6 +111,7 @@ func createRegisterParam(url *common.URL, serviceName string) vo.RegisterInstanc Healthy: true, Ephemeral: true, ServiceName: serviceName, + GroupName: groupName, } return instance } @@ -118,7 +119,8 @@ func createRegisterParam(url *common.URL, serviceName string) vo.RegisterInstanc // Register will register the service @url to its nacos registry center func (nr *nacosRegistry) Register(url *common.URL) error { serviceName := getServiceName(url) - param := createRegisterParam(url, serviceName) + groupName := nr.URL.GetParam(constant.GROUP_KEY, defaultGroup) + param := createRegisterParam(url, serviceName, groupName) isRegistry, err := nr.namingClient.Client().RegisterInstance(param) if err != nil { return err @@ -130,7 +132,7 @@ func (nr *nacosRegistry) Register(url *common.URL) error { return nil } -func createDeregisterParam(url *common.URL, serviceName string) vo.DeregisterInstanceParam { +func createDeregisterParam(url *common.URL, serviceName string, groupName string) vo.DeregisterInstanceParam { if len(url.Ip) == 0 { url.Ip = localIP } @@ -142,13 +144,15 @@ func createDeregisterParam(url *common.URL, serviceName string) vo.DeregisterIns Ip: url.Ip, Port: uint64(port), ServiceName: serviceName, + GroupName: groupName, Ephemeral: true, } } func (nr *nacosRegistry) DeRegister(url *common.URL) error { serviceName := getServiceName(url) - param := createDeregisterParam(url, serviceName) + groupName := nr.URL.GetParam(constant.GROUP_KEY, defaultGroup) + param := createDeregisterParam(url, serviceName, groupName) isDeRegistry, err := nr.namingClient.Client().DeregisterInstance(param) if err != nil { return err @@ -183,6 +187,9 @@ func (nr *nacosRegistry) Subscribe(url *common.URL, notifyListener registry.Noti return perrors.New("nacosRegistry is not available.") } + groupName := nr.GetParam(constant.GROUP_KEY, defaultGroup) + url.SetParam(constant.REGISTRY_GROUP_KEY, groupName) // update to registry.group + listener, err := nr.subscribe(url) if err != nil { if !nr.IsAvailable() { diff --git a/registry/nacos/service_discovery.go b/registry/nacos/service_discovery.go index 68bcb6f245..58717a59a4 100644 --- a/registry/nacos/service_discovery.go +++ b/registry/nacos/service_discovery.go @@ -169,6 +169,7 @@ func (n *nacosServiceDiscovery) GetInstances(serviceName string) []registry.Serv Enable: ins.Enable, Healthy: ins.Healthy, Metadata: metadata, + GroupName: n.group, }) } return res @@ -269,6 +270,7 @@ func (n *nacosServiceDiscovery) AddListener(listener registry.ServiceInstancesCh Enable: service.Enable, Healthy: true, Metadata: metadata, + GroupName: n.group, }) } diff --git a/registry/service_instance.go b/registry/service_instance.go index 6e0ffeeb53..12cc35038e 100644 --- a/registry/service_instance.go +++ b/registry/service_instance.go @@ -90,6 +90,7 @@ type DefaultServiceInstance struct { Metadata map[string]string ServiceMetadata *common.MetadataInfo Address string + GroupName string } // GetID will return this instance's id. It should be unique.