Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ftr: Support Custom Registry GroupName on Nacos #1353

1 change: 1 addition & 0 deletions common/constant/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ const (
REGISTRY_TTL_KEY = "registry.ttl"
SIMPLIFIED_KEY = "simplified"
NAMESPACE_KEY = "namespace"
REGISTRY_GROUP_KEY = "registry.group"
)

const (
Expand Down
7 changes: 6 additions & 1 deletion registry/nacos/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}()
Expand Down
15 changes: 11 additions & 4 deletions registry/nacos/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -111,14 +111,16 @@ func createRegisterParam(url *common.URL, serviceName string) vo.RegisterInstanc
Healthy: true,
Ephemeral: true,
ServiceName: serviceName,
GroupName: groupName,
}
return instance
}

// 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
Expand All @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 2 additions & 0 deletions registry/nacos/service_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -269,6 +270,7 @@ func (n *nacosServiceDiscovery) AddListener(listener registry.ServiceInstancesCh
Enable: service.Enable,
Healthy: true,
Metadata: metadata,
GroupName: n.group,
})
}

Expand Down
1 change: 1 addition & 0 deletions registry/service_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down